Notes on the use of token-free to set the necessary permissions to intercept and interception

Let me talk about principles like this do: a note to a configuration method in the head, in the spring instantiation will be injected into the comment section of the form to the method in place to intercept the judge has no current method of injecting a specified class notes .

1. annotation declare a class (class does not require any logical operation)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TokenNotValidation{
}


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Token{
}
 

2. make a judgment in your token interceptor class

public class intercept extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
TokenNotValidation annotation
= ((HandlerMethod) handler).getMethodAnnotation(TokenNotValidation.class); Token token = ((HandlerMethod) handler).getMethodAnnotation(Token.class); // 如果有@TokenNotValidation ,则不验证token if (annotation != null) { return true; } // or permission to do serious, authenticates the IF (! Annotation = null ) { // logic to determine the start permission ............. return to true ; } } }

 

3. Finally, add a @TokenNotValidation in the way you do not need to intercept the head, or in places that require authentication plus @Token on it!

 

Guess you like

Origin www.cnblogs.com/mh-study/p/11540973.html