双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

Struts2结合注释实现简易权限管理 抛板砖引玉

xielittle xielittle 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
权限管理一直是一个比较头疼的问题。

现在实现的也只是功能权限的问题。

用户 -->角色-->权限。

一般设置权限都是用 url,可不可以转换一个方式呢,

由于Struts2实现的web系统中功能都是通过Action类中的方法体现,把Action方法当作一个资源,通过使用注释指明资源ID,可以通过资源ID找到角色,然后和用户的角色匹配。



废话不说了,直接上代码:

1.注释类

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Authentication {
String name() default "";
}

2.注释解析类

public class ParseAnnotation {
public static String parseAuthentication(Class<?> clazz, String methodName,
Class<?>... parameterTypes) throws NoSuchMethodException {
Method method = null;
try {
method = clazz.getMethod(methodName, parameterTypes);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
throw new NoSuchMethodException(methodName + "no existes");
}

if (method != null) {
Authentication authentication = method.getAnnotation(Authentication.class);
if (null != authentication)
return authentication.name();
}
return null;
}
}
/*
* 暂时限定获取方法异常时表示配置错误,换回null表示有权限,返回注释的内容则需要判断
*/



3.在Action的方法上面加注释

@Authentication(name="EDIT_ROLE_FUNC")
public String edit(){
BaseRole newBaseRole = this.privManageService.findRoleById(baseRole.getRoleId());
if (null != newBaseRole) {
newBaseRole.setRoleName(baseRole.getRoleName());
newBaseRole.setRoleDesc(baseRole.getRoleDesc());
newBaseRole.setValid(baseRole.getValid());
newBaseRole.setLastUpdDate(DateUtil.getCurdate());
this.privManageService.updateRole(newBaseRole);
this.addActionMessage(this.getText("common.msg.upd.success"));
}
else
this.addActionError(this.getText("common.msg.upd.fail"));
return Action.SUCCESS;
}

4.实现权限拦截器



public class AuthenticationInterceptor extends AbstractInterceptor {

private static final long serialVersionUID = -5480389361433900956L;

@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionProxy proxy = invocation.getProxy();
String methodName = proxy.getMethod();
Object action = proxy.getAction();
String auth = null;
try {
auth = ParseAnnotation.parseAuthentication(action.getClass(),methodName, null);

//action类的方法都是没有参数的
} catch (NoSuchMethodException e) {
e.printStackTrace();
return "noPriv";//配置全局的result
}
if (auth != null)
if (!"EDIT_ROLE_FUNC".equals(auth)) {
return "noPriv";
}//这里为了做测试,只是简单的比较,已经拿到了resource ID,去数据库里面匹配是很简单的。
return invocation.invoke();
}
}

5.测试

达到预期效果,收工!
...全文
评论 12
收藏 已收藏
分享
返回顶部
xielittle
Java EE 67,535 成员 225,843 内容