1. Customize annotations first

As shown below, we first define the annotation. Customizing annotations usually requires defining where and when the annotation will be used

@Target(ElementType.METHOD)// This is defined as method-level annotations
@Retention(RetentionPolicy.RUNTIME)// indicates that the annotation is used at runtime
@Documented// The @documented annotation is Documented when javadoc is generated.
public @interface Login {
}
Copy the code

As shown below.

ackage com.irving.ir.config;

import com.irving.ir.common.util.JwtUtils;
import com.irving.ir.componet.AuthorizationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/ * * *@author irving
 * @date2021/6/19 * /
@Configuration
public class WebConfig implements WebMvcConfigurer {

   
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(newAuthorizationInterceptor()); }}Copy the code

The following

package com.irving.ir.componet;


import com.irving.ir.common.annotation.Login;

import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/** * Custom token processing logic *@author irving
 * @date2021/6/19 * /

public class AuthorizationInterceptor extends HandlerInterceptorAdapter {


   
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Login annotation;

        if (handler instanceof HandlerMethod){

            annotation=((HandlerMethod)handler).getMethodAnnotation(Login.class);
        }else {
            return  true;
        }

        if (annotation==null) {return true;
        }
        // What do I need to do with the interface that intercepts this annotation. }Copy the code

The following

    @apiOperation (" Paging all messages and descending by time ")
    @RequestMapping(value = "/list/all", method = RequestMethod.GET)
    @ResponseBody
    @Login
    public CommonResult<CommonPage<Board>> listBoard(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){

        List<Board> boardList =boardService.queryAllBoard(pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(boardList),"Paging query successful");

    }
Copy the code