本文整理匯總了Java中org.springframework.web.method.HandlerMethod.getMethodAnnotation方法的典型用法代碼示例。如果您正苦於以下問題:Java HandlerMethod.getMethodAnnotation方法的具體用法?Java HandlerMethod.getMethodAnnotation怎麽用?Java HandlerMethod.getMethodAnnotation使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.web.method.HandlerMethod
的用法示例。
在下文中一共展示了HandlerMethod.getMethodAnnotation方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: preHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!supports(handler)) {
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
Class<?> handlerClass = handlerMethod.getMethod().getDeclaringClass();
LoginIgnored annotation = handlerMethod.getMethodAnnotation(LoginIgnored.class);
if (annotation == null) {
annotation = AnnotationUtils.findAnnotation(handlerClass, LoginIgnored.class);
}
if (annotation != null) {
return true;
}
return doLogin(request, response);
}
示例2: preHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@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);
}
示例3: resolveException
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
logger.error("WebExceptionResolver:{}", ex);
ModelAndView mv = new ModelAndView();
HandlerMethod method = (HandlerMethod)handler;
ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class);
if (responseBody != null) {
response.setContentType("application/json;charset=UTF-8");
mv.setViewName("/common/common.result");
} else {
mv.addObject("exceptionMsg", ex.toString().replaceAll("\n", "<br/>"));
mv.setViewName("/common/common.exception");
}
return mv;
}
示例4: isRequestSupport
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
/**
* 請求是否支持訪問日誌記錄
* @param request
* @return
*/
protected boolean isRequestSupport(HttpServletRequest request) {
try {
if(!this.isAsyncDispatch(request) && !this.isAsyncStarted(request)){
HandlerExecutionChain handlerExecutionChain = this.getSpringMvcHandlerMethodMapping().getHandler(request);
if(handlerExecutionChain != null && handlerExecutionChain.getHandler() != null && handlerExecutionChain.getHandler() instanceof HandlerMethod){
HandlerMethod handlerMethod = (HandlerMethod) handlerExecutionChain.getHandler();
HttpAccessLogging httpAccessLogging = handlerMethod.getMethodAnnotation(HttpAccessLogging.class);
return httpAccessLogging != null;
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return false;
}
示例5: preHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return super.preHandle(request, response, handler);
}
if (!isLogin(request)) {
HandlerMethod method = (HandlerMethod)handler;
Permession permission = method.getMethodAnnotation(Permession.class);
if (null == permission || permission.permession()) {
throw new Exception("登陸失效");
}
}
return super.preHandle(request, response, handler);
}
示例6: resolveException
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
logger.error("WebExceptionResolver:{}", ex);
ModelAndView mv = new ModelAndView();
HandlerMethod method = (HandlerMethod)handler;
ResponseBody responseBody = method.getMethodAnnotation(ResponseBody.class);
if (responseBody != null) {
response.setContentType("application/json;charset=UTF-8");
mv.addObject("result", JacksonUtil.writeValueAsString(new ReturnT<String>(500, ex.toString().replaceAll("\n", "<br/>"))));
mv.setViewName("/common/common.result");
} else {
mv.addObject("exceptionMsg", ex.toString().replaceAll("\n", "<br/>"));
mv.setViewName("/common/common.exception");
}
return mv;
}
示例7: postHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
/**
* 用於清理@CsrfToken保證隻能請求成功一次
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
// 非控製器請求直接跳出
if (!(handler instanceof HandlerMethod)) {
return;
}
CsrfToken csrfToken = handlerMethod.getMethodAnnotation(CsrfToken.class);
if (csrfToken == null || !csrfToken.remove()) {
return;
}
csrfTokenRepository.saveToken(null, request, response);
}
示例8: preHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// permission checking
User currentUser = ApplicationContext.getCurrentUser();
if (handler instanceof HandlerMethod && currentUser != null) {
HandlerMethod method = (HandlerMethod) handler;
Role role = method.getMethodAnnotation(Role.class);
// TODO: check permission for API
if (role != null) {
}
}
// pass pre-flight requests in Cross-origin in AJAX
if (!request.getMethod().equalsIgnoreCase("OPTIONS")) {
String uri = request.getRequestURI();
logger.info("Request {}", uri);
Object user = request.getSession().getAttribute(Constant.SESSION_USER_KEY);
if (user == null) {
return false;
}
}
return true;
}
示例9: preHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Object handlerBean = handlerMethod.getBean();
if (handlerBean instanceof ControllerBase) {
List<Authorize> authorizes = new ArrayList<>();
Authorize authorize = handlerMethod.getMethodAnnotation(Authorize.class);
if (authorize != null) {
authorizes.add(authorize);
}
authorize = handlerMethod.getBeanType().getAnnotation(Authorize.class);
if (authorize != null) {
authorizes.add(authorize);
}
if (authorizes.size() > 0) {
userContextProcessor.process(
authorizes.stream().map(Authorize::permission).reduce((p1, p2) -> p1 + p2).get(),
request, response);
}
}
}
return true;
}
示例10: preHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod handlerMethod = (HandlerMethod) handler;
// 非控製器請求直接跳出
if (!(handler instanceof HandlerMethod)) {
return true;
}
CsrfToken csrfToken = handlerMethod.getMethodAnnotation(CsrfToken.class);
// 判斷是否含有@CsrfToken注解
if (null == csrfToken) {
return true;
}
// create、remove同時為true時異常
if (csrfToken.create() && csrfToken.remove()) {
logger.error("CsrfToken attr create and remove can Not at the same time to true!");
return renderError(request, response, "CsrfToken attr create and remove can Not at the same time to true!");
}
// 創建
if (csrfToken.create()) {
CsrfTokenBean token = csrfTokenRepository.generateToken(request);
csrfTokenRepository.saveToken(token, request, response);
request.setAttribute(token.getParameterName(), token);
return true;
}
// 校驗,並且清除
CsrfTokenBean tokenBean = csrfTokenRepository.loadToken(request);
if (tokenBean == null) {
return renderError(request, response, "CsrfToken is null!");
}
String actualToken = request.getHeader(tokenBean.getHeaderName());
if (actualToken == null) {
actualToken = request.getParameter(tokenBean.getParameterName());
}
if (!tokenBean.getToken().equals(actualToken)) {
return renderError(request, response, "CsrfToken not eq!");
}
return true;
}
示例11: preHandle
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
@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;
}
示例12: getAnnotation
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
private Auth getAnnotation(HandlerMethod handlerMethod) {
Auth annotation = handlerMethod.getMethodAnnotation(Auth.class);
Class<?> beanType = handlerMethod.getBeanType();
if (beanType.isAnnotationPresent(Auth.class)) {
annotation = beanType.getAnnotation(Auth.class);
}
return annotation;
}
示例13: isAjax
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
/**
* 判斷是否ajax請求
* spring ajax 返回含有 ResponseBody 或者 RestController注解
* @param handlerMethod HandlerMethod
* @return 是否ajax請求
*/
public static boolean isAjax(HandlerMethod handlerMethod) {
ResponseBody responseBody = handlerMethod.getMethodAnnotation(ResponseBody.class);
if (null != responseBody) {
return true;
}
RestController restAnnotation = handlerMethod.getBeanType().getAnnotation(RestController.class);
if (null != restAnnotation) {
return true;
}
return false;
}
示例14: getHandlerAnnotation
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
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 );
}
示例15: doVerify
import org.springframework.web.method.HandlerMethod; //導入方法依賴的package包/類
private void doVerify(HttpServletRequest request, Object handler) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handler == null) {
return;
}
// check token is provided from http header
String tokenPayload = request.getHeader(TOKEN_HEADER_PARAM);
if (Strings.isNullOrEmpty(tokenPayload)) {
tokenPayload = request.getParameter(TOKEN_URL_PARAM);
}
// the trick token return super user
if (Objects.equals(tokenPayload, "welcometoflowci")) {
currentUser.set(superUser);
return;
}
if (Strings.isNullOrEmpty(tokenPayload)) {
throw new AuthenticationException("Invalid request: request without token");
}
// find annotation
WebSecurity securityAnnotation = handlerMethod.getMethodAnnotation(WebSecurity.class);
Claims token = tokenGenerator.extract(tokenPayload);
if (token.getExpiration().getTime() < new Date().getTime()) {
throw new TokenExpiredException();
}
User user = userService.findByToken(tokenPayload);
if (user == null) {
throw new AuthenticationException("Invalid request: request token invalid");
}
// set current user and return if method without security annotation
if (securityAnnotation == null) {
currentUser.set(user);
return;
}
// find action for request
Action action = userSecurityService.getAction(securityAnnotation.action());
if (!userSecurityService.canAccess(user, action)) {
throw new AccessDeniedException(user.getEmail(), action.getName());
}
currentUser.set(user);
}