最近项目中需要做每一个接口均加token参数,web端进行验证。
我实用的是拦截器。
1、整体思路是定义好需要拦截的路径,并将使用的接口添加@ApiToken
2、符合路径并且添加了注解的接口发送请求时会进入拦截器,拦截器负责比对传入的token是否正确(暂未加密处理);
3、正确则继续,否则直接返回JSON。
1.Configuration
import cn.ac.bcc.ebap.common.interceptor.WebApiInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class AppTockenConfiguration extends WebMvcConfigurerAdapter {
@Bean
public WebApiInterceptor webApiInterceptor(){
return new WebApiInterceptor ();
}
@Override
public void addInterceptors(InterceptorRegistry registry){