文章目录
1. 参考博客
地址:传送门
2. 但是大佬的博客有以下几个问题
2.1 主要实现方面都是过时的代码
2.1.1 过时代码:WebMvcConfigurerAdapter
@SpringBootApplication
@MapperScan("com.wangzaiplus.test.mapper")
public class TestApplication extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
/**
* 跨域
* @return
*/
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
final CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
return new CorsFilter(urlBasedCorsConfigurationSource);
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 接口幂等性拦截器
registry.addInterceptor(apiIdempotentInterceptor());
super.addInterceptors(registry);
}
@Bean
public ApiIdempotentInterceptor apiIdempotentInterceptor() {
return new ApiIdempotentInterceptor();
}
}
2.1.2 推荐写法
@Configuration
@Slf4j
public class CorsWebConfig implements WebMvcConfigurer{
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
System.out.println("跨域................................");
registry.addMapping("/**")
.allowedOrigins("*", "null")
.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
.maxAge(3600)
.allowCredentials(true);
}
};
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 接口幂等性拦截器
registry.addInterceptor(apiIdempotentInterceptor());
WebMvcConfigurer.super.addInterceptors(registry);
}
@Bean
public ApiIdempotentInterceptor apiIdempotentInterceptor() {
return new ApiIdempotentInterceptor();
}
}
2.2 JedisUtil不推荐
我这里直接用的是StringRedisTemplate
,可以把常用的操作封装成service,使用起来比较方便
2.3 jemeter图很模糊,对不懂jemeter的人误解很深
推荐postman虽然postman不能模拟并发请求,但针对与redis+token其实并不需要测试并发请求
因为token只有一次有效,postman发两次请求同样能达到效果
2.4 其实token机制不能完全做到幂等性
我理解的只是防止一个token多次点击而已,但是多次点击如果多个token其实这个机制就废了,没有什么用