个人随笔
目录
springboot2.0实现注解控制链接是否需要判断权限
2020-09-11 22:38:03

有时候,我们有的链接需要控制权限,判断用户是否有权限,若是没有权限则提示用户无权限访问,如果每个请求方法都自己写一套,那么实在是太麻烦了,还会漏,但是这里也不应该放在拦截器中,因为拦截器中判断了是否登录,只有登录了才会有权限判断这一步。
这里,自定义一个权限注解,开发人员若是觉得某一个请求需要判断权限,那么加上该注解即可,若是不需要判断权限,则不需要加上该注解。
实现逻辑如下(AOP方式)
一、自定义一个权限注解

  1. /**
  2. * 若是方法上加了这个注解,就表明该方法是需要判断权限的,只能用在Controller上面 @AuthCheck
  3. * @author lwh
  4. */
  5. @Target({ElementType.METHOD})//只能用在方法上面
  6. @Retention(RetentionPolicy.RUNTIME)
  7. public @interface AuthCheck {
  8. }

二、写一个AOP,通过反射的方式来判断请求的方法是否有权限注解

  1. /*
  2. * 使用AOP统一处理权限
  3. * @author forever
  4. */
  5. @Aspect
  6. @Component
  7. @Order(1)//AOP执行的顺序,数字越小越先执行
  8. public class AuthCheckAspect {
  9. private static final Logger logger = LoggerFactory.getLogger(AuthCheckAspect.class);
  10. private String result2= "{\"user_auth\":\"no\"}";
  11. @Pointcut("execution(public * com.xxx.xxx.xxx.web.*.*(..))")
  12. public void webLog() {
  13. }
  14. @Around("webLog()")
  15. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  16. Method realMethod = getControllerMethod(pjp);
  17. AuthCheck authCheck = realMethod.getDeclaredAnnotation(AuthCheck.class);
  18. if(authCheck!=null) {
  19. ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
  20. HttpServletRequest request = attributes.getRequest();
  21. String httpUrl = httpUrl(request);
  22. logger.info("权限AOP判断到方法上有注解webLog");
  23. //判断用户是否有权限.有则继续执行,没有就抛出异常,表明没有权限访问
  24. Boolean result = checkUserAuth(httpUrl,request.getSession());
  25. if(!result) {
  26. logger.error("用户没有权限访问");
  27. //XMLHttpRequest这个表示异步
  28. String requestType = request.getHeader("X-Requested-With");
  29. //HttpServletResponse response = attributes.getResponse();
  30. if(requestType!=null) {
  31. //这里直接返回,因为这个就相当于是controller返回
  32. return result2;
  33. }else {
  34. //这里相当于重定向到auth请求
  35. return "auth";
  36. }
  37. }
  38. }
  39. return pjp.proceed();
  40. }
  41. private Boolean checkUserAuth(String httpUrl, HttpSession session) {
  42. logger.info("判断请求:http_method:"+httpUrl);
  43. //这里用户肯定 是已经登录,的,不然会在上一步拦截,将会到不了这里
  44. User user =(User) session.getAttribute(Const.USER_KEY);
  45. if(user!=null) {
  46. //String id = user.getUser_id();
  47. //封装权限KEY
  48. //logger.info("用户菜单权限对应的key是:"+key);
  49. //从session中获取,菜单链接对应的权限,map中是权限链接对应的权限名称
  50. @SuppressWarnings("unchecked")
  51. Map<String,String> map = (Map<String, String>) session.getAttribute(Const.AUTH_KEY);
  52. if(map!=null&&map.size()>0) {
  53. //根据请求菜单,查看是否有权限
  54. String auth_sys_name = map.get(httpUrl);
  55. if(StringUtils.isNotBlank(auth_sys_name)) {
  56. logger.info("用户有访问:"+auth_sys_name+"的权限");
  57. return true;
  58. }
  59. }
  60. }
  61. logger.info("用户没有访问:"+httpUrl+"的权限");
  62. return false;
  63. }
  64. private String httpUrl(HttpServletRequest request) {
  65. String requestURI = request.getRequestURI();
  66. return requestURI;
  67. }
  68. private Method getControllerMethod(ProceedingJoinPoint pjp) throws NoSuchMethodException, SecurityException {
  69. Signature signature = pjp.getSignature();
  70. MethodSignature methodSignature = (MethodSignature)signature;
  71. Method targetMethod = methodSignature.getMethod();
  72. String name = signature.getName();
  73. logger.info("进入权限AOP的方法名称:"+name);
  74. Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
  75. return realMethod;
  76. }
  77. }
 408

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2