本文共 3220 字,大约阅读时间需要 10 分钟。
先描述一下应用场景,基于Spring MVC的WEB程序,需要对每个Action进行权限判断,当前用户有权限则允许执行Action,无权限要出错提示。权限有很多种,比如用户管理权限、日志审计权限、系统配置权限等等,每种权限还会带参数,比如各个权限还要区分读权限还是写权限。
想实现统一的权限检查,就要对Action进行拦截,一般是通过拦截器来做,可以实现HandlerInterceptor或者HandlerInterceptorAdapter,但是每个Action都有不同的权限检查,比如getUsers要用户管理的读权限,deleteLogs要日志审计的写权限,只定义一个拦截器很难做到,为每种权限定义一个拦截器又太乱,此时可以通过自定义注解来标明每个Action需要什么权限,然后在单一的拦截器里就可以统一检查了。
具体这么做,先实现一个自定义注解,名叫AuthCheck:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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的用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 | @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) { //具体实现,略 } |
最后要实现拦截器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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 { } } |
拦截器要生效,还要配置一下:
1 2 3 4 5 6 7 8 | < 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,搞定收工。