原有的根据地址访问控制权限.有些麻烦.所以写了个struts2 的 注解权限处理..
对于页面根据权限是否显示某个按钮.可以通过struts2 访问静态方法去实现.这里就不写了.
代码如下:
@Security:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Security {
String name();
String value();
}
利用struts2 的拦截器作为注解处理器:
先写个拦截器:
public class AuthorityInterceptor extends AbstractInterceptor{
private static final long serialVersionUID = 6137804398980420490L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String actionMethod = invocation.getProxy().getMethod();
Method method = invocation.getAction().getClass().getMethod(actionMethod);
Security security = method.isAnnotationPresent(Security.class) == false ? null
: method.getAnnotation(Security.class);
boolean sec = true;
if(security != null){
String name = security.name();
String value = security.value();
System.out.println("security-name==:"+name);
System.out.println("security-value==:"+value);
//这里根据name 和value 去查询数据库或者session,也可以是缓存里的用户权限信息..;
sec = false;
}
return sec ? invocation.invoke() : "securityError";//如果没有权限,跳至权限提示页面,这个可以是全局跳转
}
}
再配置一个拦截器栈:
<package name="seamsju" namespace="/user" extends="struts-default"> <interceptors> <interceptor name="authorityInterceptor" class="com.yuhongtech.filter.AuthorityInterceptor"></interceptor> <interceptor-stack name="myInterceptorStack"> <interceptor-ref name="authorityInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <action name="*html" class="com.yuhongtech.action.user.OrderAction" method="{1}"> <result>/user/orderlist.jsp</result> <result name="input">/user2/login.jsp</result> <interceptor-ref name="myInterceptorStack"></interceptor-ref> </action> </package>
好啦,东西处理完啦..可以用了:
/**
* 订单查询
* @return
*/
@Security(name="Order",value="show")
public String listorder(){}
只要在对应的action 的method 上加上相应的@Security 注解就行了..