@Override public <T extends Annotation> T getMethodAnnotation(Class<T> annotationType) { return HandlerMethod.this.getMethodAnnotation(annotationType); }
@Override public <T extends Annotation> T getMethodAnnotation(Class<T> annotationType) { return HandlerMethod.this.getMethodAnnotation(annotationType); }
/** * Determine whether the given {@code @InitBinder} method should be used * to initialize the given {@link WebDataBinder} instance. By default we * check the specified attribute names in the annotation value, if any. */ protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder dataBinder) { InitBinder ann = initBinderMethod.getMethodAnnotation(InitBinder.class); Assert.state(ann != null, "No InitBinder annotation"); String[] names = ann.value(); return (ObjectUtils.isEmpty(names) || ObjectUtils.containsElement(names, dataBinder.getObjectName())); }
/** * Determine whether the given {@code @InitBinder} method should be used * to initialize the given {@link WebDataBinder} instance. By default we * check the specified attribute names in the annotation value, if any. */ protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder dataBinder) { InitBinder ann = initBinderMethod.getMethodAnnotation(InitBinder.class); Assert.state(ann != null, "No InitBinder annotation"); String[] names = ann.value(); return (ObjectUtils.isEmpty(names) || ObjectUtils.containsElement(names, dataBinder.getObjectName())); }
@Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { logger.error("WebExceptionResolver:{}", ex); // if json boolean isJson = false; HandlerMethod method = (HandlerMethod)handler; ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class); if (responseBody != null) { isJson = true; } // error result ReturnT<String> errorResult = new ReturnT<String>(ReturnT.FAIL_CODE, ex.toString().replaceAll("\n", "<br/>")); // response ModelAndView mv = new ModelAndView(); if (isJson) { try { response.setContentType("application/json;charset=utf-8"); response.getWriter().print(JacksonUtil.writeValueAsString(errorResult)); } catch (IOException e) { logger.error(e.getMessage(), e); } return mv; } else { mv.addObject("exceptionMsg", errorResult.getMsg()); mv.setViewName("/common/common.exception"); return mv; } }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (!(handler instanceof HandlerMethod)) { return super.preHandle(request, response, handler); } if (!ifLogin(request)) { HandlerMethod method = (HandlerMethod)handler; PermessionLimit permission = method.getMethodAnnotation(PermessionLimit.class); if (permission == null || permission.limit()) { response.sendRedirect(request.getContextPath() + "/toLogin"); //request.getRequestDispatcher("/toLogin").forward(request, response); return false; } } return super.preHandle(request, response, handler); }
@Override public List<Map<String, String>> getAllUrl(String p1) { RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); //获取 url与类和方法的对应信息 Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); List<Map<String, String>> urlList = new ArrayList<>(); for (Map.Entry<RequestMappingInfo, HandlerMethod> entry : map.entrySet()) { RequestMappingInfo info = entry.getKey(); HandlerMethod handlerMethod = map.get(info); RequiresPermissions permissions = handlerMethod.getMethodAnnotation(RequiresPermissions.class); String perms = ""; if (null != permissions) { perms = StringUtils.join(permissions.value(), ","); } Set<String> patterns = info.getPatternsCondition().getPatterns(); for (String url : patterns) { Map<String, String> urlMap = new HashMap<>(); urlMap.put("url", url.replaceFirst("\\/", "")); urlMap.put("perms", perms); urlList.add(urlMap); } } return urlList; }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod method = ((HandlerMethod) handler); TwoFactor factor = method.getMethodAnnotation(TwoFactor.class); if (factor == null || factor.ignore()) { return true; } String userId = Authentication.current() .map(Authentication::getUser) .map(User::getId) .orElse(null); TwoFactorValidator validator = validatorManager.getValidator(userId, factor.value(), factor.provider()); if (!validator.expired()) { return true; } String code = request.getParameter(factor.parameter()); if (code == null) { code = request.getHeader(factor.parameter()); } if (StringUtils.isEmpty(code)) { throw new NeedTwoFactorException(factor.message(), factor.provider()); } else if (!validator.verify(code, factor.timeout())) { throw new NeedTwoFactorException("验证码错误", factor.provider()); } } return super.preHandle(request, response, handler); } }
AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class); if(accessLimit == null) { return true;
public class JsonViewInterceptor extends HandlerInterceptorAdapter { @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { HandlerMethod handlerMethod = (HandlerMethod) handler; JsonView jsonViewAnnotation = handlerMethod.getMethodAnnotation(JsonView.class); if(jsonViewAnnotation != null) modelAndView.addObject("json.JsonView", jsonViewAnnotation.value()); } }
@Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { logger.error("WebExceptionResolver:{}", ex); // if json boolean isJson = false; HandlerMethod method = (HandlerMethod)handler; ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class); if (responseBody != null) { isJson = true; } // error result ReturnT<String> errorResult = new ReturnT<String>(ReturnT.FAIL.getCode(), ex.toString().replaceAll("\n", "<br/>")); // response ModelAndView mv = new ModelAndView(); if (isJson) { try { response.setContentType("application/json;charset=utf-8"); response.getWriter().print(JacksonUtil.writeValueAsString(errorResult)); } catch (IOException e) { logger.error(e.getMessage(), e); } return mv; } else { mv.addObject("exceptionMsg", errorResult.getMsg()); mv.setViewName("/common/common.exception"); return mv; } }
ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class); if (responseBody != null) { isJson = true;
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (!(handler instanceof HandlerMethod)) { return super.preHandle(request, response, handler); } // if need login boolean needLogin = true; boolean needAdminuser = false; HandlerMethod method = (HandlerMethod)handler; PermessionLimit permission = method.getMethodAnnotation(PermessionLimit.class); if (permission!=null) { needLogin = permission.limit(); needAdminuser = permission.superUser(); } // if pass if (needLogin) { XxlApiUser loginUser = loginService.ifLogin(request); if (loginUser == null) { response.sendRedirect(request.getContextPath() + "/toLogin"); //request.getRequestDispatcher("/toLogin").forward(request, response); return false; } if (needAdminuser && loginUser.getType()!=1) { throw new RuntimeException("权限拦截"); } request.setAttribute(LoginService.LOGIN_IDENTITY, loginUser); } return super.preHandle(request, response, handler); }
private < T extends Annotation > T getHandlerAnnotation ( HandlerMethod handlerMethod , Class< T > clazz ) { T annotation = handlerMethod.getMethodAnnotation( clazz ); if ( Objects.nonNull( annotation ) ) { return annotation; } return handlerMethod.getBeanType().getAnnotation( clazz ); }
/** * Determine whether the given {@code @InitBinder} method should be used * to initialize the given {@link WebDataBinder} instance. By default we * check the specified attribute names in the annotation value, if any. */ protected boolean isBinderMethodApplicable(HandlerMethod initBinderMethod, WebDataBinder dataBinder) { InitBinder ann = initBinderMethod.getMethodAnnotation(InitBinder.class); Assert.state(ann != null, "No InitBinder annotation"); String[] names = ann.value(); return (ObjectUtils.isEmpty(names) || ObjectUtils.containsElement(names, dataBinder.getObjectName())); }
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; if ((handlerMethod.getBeanType().isAnnotationPresent(LoginRequired.class) || handlerMethod.getMethodAnnotation(LoginRequired.class) != null) && userHolder.getUser() == null) { throw ShepherException.createNoLoginException(); } } return true; }
@Override public void execute(RequestMappingContext context) { HandlerMethod handlerMethod = context.getHandlerMethod(); boolean isHidden = false; ApiOperation methodAnnotation = handlerMethod.getMethodAnnotation(ApiOperation.class); if (null != methodAnnotation) { isHidden = methodAnnotation.hidden(); } context.put("isHidden", isHidden); } }
private void evaluateResponseStatus() { ResponseStatus annotation = getMethodAnnotation(ResponseStatus.class); if (annotation == null) { annotation = AnnotatedElementUtils.findMergedAnnotation(getBeanType(), ResponseStatus.class); } if (annotation != null) { this.responseStatus = annotation.code(); this.responseStatusReason = annotation.reason(); } }