|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { | 
|---|
|  |  |  | HandlerMethod handlerMethod = (HandlerMethod) handler; | 
|---|
|  |  |  | Class<?> beanType = handlerMethod.getBeanType(); | 
|---|
|  |  |  | if (!beanType.isAnnotationPresent(LoginNoRequired.class) && !handlerMethod.hasMethodAnnotation(LoginNoRequired.class)) { | 
|---|
|  |  |  | //获取token | 
|---|
|  |  |  | Cookie[]  cookies =   request.getCookies(); | 
|---|
|  |  |  | String token = request.getHeader(Constants.HEADER_USER_TOKEN);  // 从 http 请求头中取出 token | 
|---|
|  |  |  | if(StringUtils.isBlank(token)){ | 
|---|
|  |  |  | for(Cookie c :cookies){ | 
|---|
|  |  |  | if(StringUtils.equals(c.getName(),Constants.HEADER_USER_TOKEN)){ | 
|---|
|  |  |  | token = c.getValue(); | 
|---|
|  |  |  | if(handler instanceof HandlerMethod){ | 
|---|
|  |  |  | HandlerMethod handlerMethod = (HandlerMethod) handler; | 
|---|
|  |  |  | Class<?> beanType = handlerMethod.getBeanType(); | 
|---|
|  |  |  | if (!beanType.isAnnotationPresent(LoginNoRequired.class) && !handlerMethod.hasMethodAnnotation(LoginNoRequired.class)) { | 
|---|
|  |  |  | //获取token | 
|---|
|  |  |  | Cookie[]  cookies =   request.getCookies(); | 
|---|
|  |  |  | String token = request.getHeader(Constants.HEADER_USER_TOKEN);  // 从 http 请求头中取出 token | 
|---|
|  |  |  | if(StringUtils.isBlank(token)){ | 
|---|
|  |  |  | for(Cookie c :cookies){ | 
|---|
|  |  |  | if(StringUtils.equals(c.getName(),Constants.HEADER_USER_TOKEN)){ | 
|---|
|  |  |  | token = c.getValue(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (StringUtils.isNotBlank(token)) { | 
|---|
|  |  |  | LoginUserInfo user =   checkLogin(token); | 
|---|
|  |  |  | if (handlerMethod.hasMethodAnnotation(CloudRequiredPermission.class)) { | 
|---|
|  |  |  | CloudRequiredPermission p = handlerMethod.getMethodAnnotation(CloudRequiredPermission.class); | 
|---|
|  |  |  | if(p.value()!=null && p.value().length>0){ | 
|---|
|  |  |  | boolean hasPermission = false; | 
|---|
|  |  |  | for(String s :p.value()){ | 
|---|
|  |  |  | if(user.getPermissions()!=null){ | 
|---|
|  |  |  | for(String t :user.getPermissions()){ | 
|---|
|  |  |  | if(StringUtils.equals(t,s)){ | 
|---|
|  |  |  | hasPermission = true; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | if (StringUtils.isNotBlank(token)) { | 
|---|
|  |  |  | LoginUserInfo user =   checkLogin(token); | 
|---|
|  |  |  | if (handlerMethod.hasMethodAnnotation(CloudRequiredPermission.class)) { | 
|---|
|  |  |  | CloudRequiredPermission p = handlerMethod.getMethodAnnotation(CloudRequiredPermission.class); | 
|---|
|  |  |  | if(p.value()!=null && p.value().length>0){ | 
|---|
|  |  |  | boolean hasPermission = false; | 
|---|
|  |  |  | for(String s :p.value()){ | 
|---|
|  |  |  | if(user.getPermissions()!=null){ | 
|---|
|  |  |  | for(String t :user.getPermissions()){ | 
|---|
|  |  |  | if(StringUtils.equals(t,s)){ | 
|---|
|  |  |  | hasPermission = true; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!hasPermission) { | 
|---|
|  |  |  | //没有操作权限 | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"没有该操作权限"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(!hasPermission) { | 
|---|
|  |  |  | //没有操作权限 | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"没有该操作权限"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | throw new BusinessException(ResponseStatus.NO_LOGIN.getCode(),"未登录"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|