权限管理框架实现(1)--Struts切面处理

本文介绍了如何在Struts2框架中实现权限管理,通过自定义注解、开发抽象切面并配置Struts,实现Action级别的权限验证。核心思路包括:1) 定义权限注解;2) 创建Struts抽象切面,包含获取用户ID和IP的方法;3) 配置Struts切面以进行权限拦截。示例代码展示了具体实现,帮助读者理解整体架构。文章结尾提到后续会探讨ValueStack的使用和前端控件的自动显隐。
摘要由CSDN通过智能技术生成

要做一款权限架构,就要适用几个流行的相关框加,struts2是我们公司首先需要考虑的,考虑到侵入性,决定通过切面的方式,在每个Action前进行权限验证,基本思路是:

        1,自定义通用权限注解

        2,开发抽象切面,预留传入uid的接口

        3,配置struts切面,做权限拦截


以下源码是对上边功能的实现:

1,权限注解

/**
 * 自定义权限注解
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Authority {

    /**
     * 权限码
     * @return
     */
    String authorityCode();

}

2,sturts抽象切面

public abstract class ELInterceptor implements Interceptor {

    private IVerificationUser verificationUser;
    private ILogicUserAreaFranchiseeService logicUserAreaFranchiseeService;
    private Result result = new Result(true);

    @Override
    public void destroy() {
        // TODO Auto-generated method stub

    }

    protected final Log log = LogFactory.getLog(this.getClass());

    @Override
    public void init() {

    }

    /**
     * 拦截类并作权限验证
     * @param invocation
     * @return
     * @throws Exception
     */
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        log.info("=intercept=>Authority Intercept");
        // TODO Auto-generated method stub
        String methodName = invocation.getProxy().getMethod();
        Method currentMethod = invocation.getAction().getClass().getMethod(methodName);
        Method[] methods = invocation.getAction().getClass().getMethods();
        initAuthCode(methods);

        String isTest = (String) ServletActionContext.getRequest().getParameter("authistest");

        //如果该方法请求是需要进行验证的时候执行以下逻辑
        if (currentMethod.isAnnotationPresent(Authority.class)) {
            //取得权限验证的注解
            Authority authority = currentMethod.getAnnotation(Authority.class);

            log.info("=intercept=> get authorityCode");
            //取得当前请求的注解的authorityCode
            String authorityCode = authority.authorityCode();

             /* *
             * 然后可以在此判断当前用户是否拥有对应的权限,如果没有可以跳到指定的无权限提示页面,如果拥有则可以
             * 继续往下执行。
             **/
            boolean ispass =false;
            ispass = Boolean.parseBoolean(getFromVm(authorityCode));
            if (ispass){
                if(isTest==null ||isTest.trim().isEmpty()){
                    return invocation.invoke();
                }else{
                    return "hasauth";
                }
            } else {
                log.info("=intercept=> user not have  this authorityCode");
                writeJson("<html auth='NOAUTH'></html>");
                return "noauth";
            }
        }
        log.info("<=intercept=>Authority Intercept");
        if(isTest
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值