zoukankan      html  css  js  c++  java
  • 【Spring】5、利用自定义注解在SpringMVC中实现自定义权限检查

    转自:http://www.tuicool.com/articles/6z2uIvU

    先描述一下应用场景,基于Spring MVC的WEB程序,需要对每个Action进行权限判断,当前用户有权限则允许执行Action,无权限要出错提示。权限有很多种,比如用户管理权限、日志审计权限、系统配置权限等等,每种权限还会带参数,比如各个权限还要区分读权限还是写权限。

    想实现统一的权限检查,就要对Action进行拦截,一般是通过拦截器来做,可以实现HandlerInterceptor或者HandlerInterceptorAdapter,但是每个Action都有不同的权限检查,比如getUsers要用户管理的读权限,deleteLogs要日志审计的写权限,只定义一个拦截器很难做到,为每种权限定义一个拦截器又太乱,此时可以通过自定义注解来标明每个Action需要什么权限,然后在单一的拦截器里就可以统一检查了。

    具体这么做,先实现一个自定义注解,名叫AuthCheck:

    package com.test.web;
    
    import java.lang.annotation.Documented;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.RetentionPolicy;
    
    @Documented
    @Target(ElementType.METHOD)
    @Inherited
    @Retention(RetentionPolicy.RUNTIME)
    public @interface AuthCheck {
        /**
         * 权限类型
         * @return
         */
        String type() default "";
        /**
         * 是否需要写权限
         * @return
         */
        boolean write() default false;
    }

    这个注解里包含2个属性,分别用于标定权限的类型与读写要求。然后为需要检查权限的Action加注解,此处以getUsers和deleteLogs为例,前者要求对用户管理有读权限,后者要求对日志审计有写权限,注意@AuthCheck的用法:

    @AuthCheck(type = "user", write = false)
    @RequestMapping(value = "/getUsers", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse getUsers(@RequestBody GetUsersRequest request) {
        //具体实现,略
    }
    
    @AuthCheck(type = "log", write = true)
    @RequestMapping(value = "/deleteLogs", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse deleteLogs(@RequestBody DeleteLogsRequest request) {
        //具体实现,略
    }

    最后要实现拦截器:

    package com.test.web;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.method.HandlerMethod;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.ModelAndView;
    
    /**
     * 全局拦截器
     */
    public class ActionInterceptor implements HandlerInterceptor {
        /**
         * 前置拦截,用于检查身份与权限
         */
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            //从传入的handler中检查是否有AuthCheck的声明
            HandlerMethod method = (HandlerMethod)handler;
            AuthCheck auth = method.getMethodAnnotation(AuthCheck.class);
            
            //找到了,取出定义的权限属性,结合身份信息进行检查
            if(auth != null) {
                String type = auth.type();
                boolean write = auth.write();
                
                //根据type与write,结合session/cookie等身份信息进行检查
                //如果权限检查不通过,可以输出特定信息、进行跳转等操作
                //并且一定要return false,表示被拦截的方法不用继续执行了
            }
          
            //检查通过,返回true,方法会继续执行
            return true;
        }
        
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView model) throws Exception {
        }
        
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
        }
    }

    拦截器要生效,还要配置一下:

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <mvc:exclude-mapping path="/js/**" />
            <mvc:exclude-mapping path="/css/**" />
            <bean class="com.test.web.ActionInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>

    OK,搞定收工。

  • 相关阅读:
    关于xutils中的BitmapUtil实现简单的缓存和下载
    自定义spinner
    Dart格式化输出
    使用 dsc_extractor 导出 dyld_shared_cache_arm64
    iOS11 获取手机已安装应用列表
    恢复二进制文件中的block符号表
    iOS 反反注入 修改__RESTRICT,__restrict工具
    Node的调试
    UVa10131
    UVa116
  • 原文地址:https://www.cnblogs.com/wangzhongqiu/p/6626252.html
Copyright © 2011-2022 走看看