權限控制是保護系統安全運行很重要的一扇門。在web應用里,僅僅隱藏url是不夠的。由于web應用是以請求/響應為單位的,我 們的權限控制的粒度只有達到這個程度才能讓全國人民放心。在java web開發的世界里,MVC框架的使用再平常不過,大都是將請求攔截后,控制器根據配置文件將請求轉給某個函數來處理。下面看看在struts2中我們可 以用的幾種方案:
1、在每個函數里進行權限校驗
這主意實在是簡單,缺點我就不說了~太多了~
2、在每個請求對應的Action的配置項里配置參數,用以標示訪問此Action需要的權限,再用攔截器處理
以 前我這么做過,比方案1好很多,不過這注定你無法實現ZeroConfig。在ROR的促進下,約定優于配置漸漸深入人心。本人就極其反感大量的配置文 件。但是由于權限配置提到XML里配置,最大的好處就是我不必重新編譯代碼就能改變權限關聯。不過情況下遇到需求變更,你會有一種寧可去改代碼的沖動。
3、結合Java的Annotation和Struts2的攔截器控制權限
下面是上午沒事寫的一個示例:訪問login.jsp,登錄,功能有eat和drink,用戶登陸后只能訪問已授權的功能鏈接。
基本思想:對每個Action方法進行注解,并注入一個資源字符串,部署一個攔截器,在每個請求之前攔截一下,通過反射拿到所調用的方法及其注解,依此來進行權限校驗。
優點:
簡單、可行性高
不修改MVC框架配置文件
不影響Action內的業務邏輯
注解的原則之一就是不影響代碼的運行,這也實現了本方案的可插拔性、獨立性高
更高的可配置性
缺點:
不知道對性能影響如何
代碼基本上都貼到下面了,不想細講了,有興趣的可以留言討論,覺得我火星的就不要拍磚了,有需要eclipse工程源碼的發郵件問我要shoru#163.com。
(1)Annotation相關
Access
1package com.shoru.access;
2
3import java.lang.annotation.ElementType;
4import java.lang.annotation.Retention;
5import java.lang.annotation.RetentionPolicy;
6import java.lang.annotation.Target;
7
8/** *//**
9 * 訪問控制注解
10 * 該注解保留到運行時,針對方法使用,默認為BLOCK
11 * @author Shoru
12 * @version 0.1
13 */
14@Retention(RetentionPolicy.RUNTIME)
15@Target(ElementType.METHOD)
16public @interface Access {
17 String[] value() default { AccessOption.BLOCK };
18}
AccessOption
1package com.shoru.access;
2
3/** *//**
4 * 訪問控制接口,定義默認的控制常量
5 * @author Shoru
6 * @version 0.1
7 */
8public interface AccessOption {
9 /** *//**
10 * 攔截訪問
11 */
12 public static String BLOCK="block";
13 /** *//**
14 * 通過訪問
15 */
16 public static String PASS="pass";
17 /** *//**
18 * 要求登錄
19 */
20 public static String LOGIN="login";
21}
UserAccessOption
1package com.shoru.access;
2
3
4/** *//**
5 * 用戶自定義控制接口,繼承自AccessOption
6 * 可將系統權限全部定義到此處,格式為:權限名=資源名
7 * @author Shoru
8 * @see AccessOption
9 */
10public interface UserAccessOption extends AccessOption {
11 public static String EAT = "eat";
12 public static String DRINK = "drink";
13}
(2)Action類
AccessAction
1package com.shoru.access.action;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import com.opensymphony.xwork2.Action;
7import com.opensymphony.xwork2.ActionContext;
8import com.shoru.access.Access;
9import com.shoru.access.UserAccessOption;
10
11public class AccessAction implements Action {
12 @Access
13 public String execute() throws Exception {
14 return SUCCESS;
15 }
16
17 @Access(UserAccessOption.PASS)
18 public String index() throws Exception {
19 /**//*
20 * 此處模擬權限的獲取
21 */
22 List<String> accessPoints = new ArrayList<String>();
23 /**//*
24 * 賦予eat權限
25 */
26 accessPoints.add("eat");
27 ActionContext.getContext().getSession().put("access", accessPoints);
28 return SUCCESS;
29 }
30
31 @Access(UserAccessOption.DRINK)
32 public String drink() throws Exception {
33 return SUCCESS;
34 }
35
36 @Access( { UserAccessOption.EAT })
37 public String eat() throws Exception {
38 return SUCCESS;
39 }
40}
(3)攔截器
AccessInterceptor
1package com.shoru.access.interceptor;
2
3import java.lang.annotation.Annotation;
4import java.lang.reflect.Method;
5import java.util.List;
6
7import com.opensymphony.xwork2.ActionContext;
8import com.opensymphony.xwork2.ActionInvocation;
9import com.opensymphony.xwork2.interceptor.Interceptor;
10import com.opensymphony.xwork2.util.AnnotationUtils;
11import com.shoru.access.Access;
12import com.shoru.access.AccessOption;
13
14public class AccessInterceptor implements Interceptor {
15
16 private static final long serialVersionUID = -1066389312400000758L;
17
18 List<String> accessPoints = null;
19
20 public void init() {
21
22 }
23
24 public void destroy() {
25 accessPoints = null;
26 }
27
28 public String intercept(ActionInvocation invocation) throws Exception {
29 if (accessPoints == null) {
30 /**//*
31 * 獲取權限列表
32 */
33 accessPoints = (List<String>) ActionContext.getContext()
34 .getSession().get("access");
35 }
36 /**//*
37 * 獲取此次調用的方法名
38 */
39 String method = invocation.getProxy().getMethod();
40 /**//*
41 * 獲取所有已注解方法
42 */
43 List<Method> methods = AnnotationUtils.findAnnotatedMethods(invocation
44 .getAction().getClass(), Access.class);
45 /**//*
46 * 迭代所有已注解方法
47 */
48 for (Method m : methods) {
49 if (m.getName().equals(method)) {
50 /**//*
51 * 獲取被調用方法的注解
52 */
53 Annotation annotation = m.getAnnotation(Access.class);
54 /**//*
55 * 放過不需要校驗權限列表的請求,e.g.登錄、驗證碼
56 */
57 for (String s : ((Access) annotation).value()) {
58 if (s.equals(AccessOption.PASS)) {
59 return invocation.invoke();
60 }
61 }
62 /**//*
63 * 權限列表為空,返回登錄
64 */
65 if (accessPoints == null) {
66 return AccessOption.LOGIN;
67 }
68 /**//*
69 * 迭代方法注解里的值,判斷是否存在于權限列表中
70 */
71 for (String s : ((Access) annotation).value()) {
72 if (accessPoints.indexOf(s) != -1) {
73 /**//*
74 * 權限校驗通過
75 */
76 return invocation.invoke();
77 }
78 }
79 }
80 }
81 /**//*
82 * 沒有對方法進行注解或者權限校驗不通過,攔截此次請求
83 */
84 return AccessOption.BLOCK;
85 }
86}
(4)struts.xml
struts.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5<struts>
6 <package name="access" extends="struts-default">
7 <interceptors>
8 <!-- 權限攔截器 -->
9 <interceptor name="access"
10 class="com.shoru.access.interceptor.AccessInterceptor"></interceptor>
11 <interceptor-stack name="my-default">
12 <interceptor-ref name="access"></interceptor-ref>
13 <interceptor-ref name="defaultStack"></interceptor-ref>
14 </interceptor-stack>
15 </interceptors>
16 <default-interceptor-ref name="my-default"></default-interceptor-ref>
17 <global-results>
18 <result name="block">/login.jsp</result>
19 <result name="login">/login.jsp</result>
20 </global-results>
21 <!-- Zero Config All Actions -->
22 <action name="*" class="com.shoru.access.action.AccessAction"
23 method="{1}">
24 <result>/{1}.jsp</result>
25 </action>
26
文章列表