每個項目都會有權限管理系統
無論你是一個簡單的企業站,還是一個復雜到爆的平臺級項目,都會涉及到用戶登錄、權限管理這些必不可少的業務邏輯。有人說,企業站需要什么權限管理阿?那行吧,你那可能叫靜態頁面,就算這樣,但你肯定也會有后臺管理及登錄功能。
每個項目中都會有這些幾乎一樣的業務邏輯,我們能不能把他們做成通用的系統呢?
AOP 實現用戶權限驗證
AOP 在實際項目中運用的場景主要有權限管理(Authority Management)、事務管理(Transaction Management)、安全管理(Security)、日志管理(Logging)和調試管理(Debugging)等。
所以,權限驗證正好我們可以使用 AOP 來直接實現。具體你項目中權限怎么管理,管理的粒度是什么級別這些完全取決于項目需要,這里完全不做任何的討論。
先說思路:利用自定義注解及攔截器來在你需要的時候,進行你需要的一些權限認證。這里依然涉及到的有enum(枚舉)
、annotation(自定義注解)
及攔截器相關知識,廢話不多說,直接開寫代碼。
開始擼一下代碼
**一、建立AuthorityType.java
枚舉類
1
2
3
4
5
6
7
8
9
10
11
|
public enum AuthorityType { // 登錄和權限都驗證 默認 Validate, // 不驗證 NoValidate, // 不驗證權限 NoAuthority; } |
這個枚舉類的作用,依然是使自定義注解用起來爽到還想要。
二、新建Authority.java
自定義注解類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention (RetentionPolicy.RUNTIME) @Target (ElementType.METHOD) @Documented public @interface Authority { // 默認驗證 AuthorityType value() default AuthorityType.Validate; } |
三、再建一個AuthorityAnnotationInterceptor.java
類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
/** * 權限認證攔截器 * */ public class AuthorityAnnotationInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod hm = (HandlerMethod) handler; Class<?> clazz = hm.getBeanType(); Method m = hm.getMethod(); try { if (clazz != null && m != null ) { boolean isClzAnnotation = clazz.isAnnotationPresent(Authority. class ); boolean isMethondAnnotation = m.isAnnotationPresent(Authority. class ); Authority authority = null ; // 如果方法和類聲明中同時存在這個注解,那么方法中的會覆蓋類中的設定。 if (isMethondAnnotation) { authority = m.getAnnotation(Authority. class ); } else if (isClzAnnotation) { authority = clazz.getAnnotation(Authority. class ); } int code = - 1 ; String msg = "" ; if (authority != null ) { if (AuthorityType.NoValidate == authority.value()) { // 標記為不驗證,放行 return true ; } else if (AuthorityType.NoAuthority == authority.value()) { // 不驗證權限,驗證是否登錄 // TODO: return true ; } else { // 驗證登錄及權限 // TODO: code = 1 ; msg = "驗證成功!" ; return true ; } } // //跳轉 // String url = ""; // response.getWriter().write("<script>top.location.href='" // + url + "'</script>"); // return false; // 未通過驗證,返回提示json Map<String, Object> responseMap = new HashMap<String, Object>(); responseMap.put( "code" , code); responseMap.put( "msg" , msg); responseMap.put( "params" , "" ); responseMap.put( "rows" , "" ); String json = new Gson().toJson(responseMap); response.setCharacterEncoding( "UTF-8" ); response.setContentType( "application/json; charset=utf-8" ); response.getWriter().write(json); return false ; } } catch (Exception e) { } } return false ; } } |
這個類的目的就是在打過Authority
標簽的方法及類上,進行權限認證。我這里分了三種類型:全部驗證、只驗證登錄、不驗證用來滿足我們的業務需求。
這里的返回值可以是 JSON 串,也可以是跳轉到相應的頁面,來實現你想要的效果。
四、配置攔截器
1
2
3
4
5
6
7
|
< mvc:interceptors > <!-- 權限認證攔截器 --> < mvc:interceptor > < mvc:mapping path = "/**" /> < bean class = "cn.mayongfa.interceptor.AuthorityAnnotationInterceptor" ></ bean > </ mvc:interceptor > </ mvc:interceptors > |
在/WebContent/WEB-INF/springMVC-servlet.xml
文件下的<mvc:interceptors>
節點配置就行,這里可以配置具體要攔截的 Url。
到這里就完成了權限驗證的工作了,如何使用呢?
使用就非常簡單
因為我們的攔截器配置,然后我們在自定義注解的默認是驗證,所以,我們只需要在類名及方法名上打標簽就可以。
當然,你完全是可以在攔截器中設置默認就驗證所有請求的,接著設置不驗證的請求。
文章的具體的案例地址:SpringDemo.rar
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/mafly/p/spring_authority.html