问题背景:新项目使用Springboot框架,鉴权使用了Jwt

处理cors:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer { @Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("*")
.allowCredentials(true);
}
}

使用自定义Jwt鉴权:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired
JwtAuthenticationEntryPoint unauthorizedHandler;
@Autowired
RestAuthenticationAccessDeniedHandler accessDeniedHandler;
@Autowired
CustomUserDetailsServiceImpl CustomUserDetailsService;
@Autowired
JwtAuthenticationTokenFilter authenticationTokenFilter;
@Autowired
private MyPermissionEvaluator myPermissionEvaluator; @Autowired
public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder
// 设置UserDetailsService
.userDetailsService(CustomUserDetailsService)
// 使用BCrypt进行密码的hash
.passwordEncoder(passwordEncoder());
} /**
* 装载BCrypt密码编码器
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
} @Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.exceptionHandling().accessDeniedHandler(accessDeniedHandler).and()
.cors().and()//乌龙在这行,一开始没有配置这个
//如果您使用的是Spring Security,请确保在Spring Security级别启用CORS,以允许它利用Spring MVC级别定义的配置。
//乌龙在这行,一开始没有配置这个
//乌龙在这行,一开始没有配置这个
// 由于使用的是JWT,我们这里不需要csrf
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// 基于token,所以不需要session
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .authorizeRequests() // 对于获取token的rest api要允许匿名访问
.antMatchers("/admin/login",
"/admin/register","/error/**").permitAll()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated(); // 禁用缓存
httpSecurity.headers().cacheControl(); // 添加JWT filter
httpSecurity
.addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
} @Override
public void configure(WebSecurity web) {
web
.ignoring().antMatchers(HttpMethod.OPTIONS, "/**").and()
.ignoring()
.antMatchers(
"/favicon.ico",
"/**/*.css",
"/**/*.js",
"/**/*.png",
"/**/*.gif",
"/**/*.ttf"
);
} @Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
} /**
* 注入自定义PermissionEvaluator
*/
@Bean
public DefaultWebSecurityExpressionHandler webSecurityExpressionHandler(){
DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
handler.setPermissionEvaluator(myPermissionEvaluator);
return handler;
} }

然后就出问题了:登录后使用token Jwt鉴权正常,前端跨域正常,但是当token失效后接口200 但是拿不到数据

这个样子 This request has no response data available

用postman请求正常:报token失效

由于之前解决了跨域问题就没考虑跨域,怀疑的方向是Jwt拦截后没有处理返回或者dochain断掉了

后来看了下浏览器的console发现token失效后出现了跨域问题,

搜索发现security配置里少了一个配置:.cors().and()

如果使用的是Spring Security,请确保在Spring Security级别启用CORS,以允许它利用Spring MVC级别定义的配置。

好吧,security没有启用cors

fixed 一切正常

最新文章

  1. 关于试用jquery的jsonp实现ajax跨域请求数据的问题
  2. python 标准库
  3. Fedora 20 创建桌面快捷方式
  4. Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
  5. 咏南CS多层插件式开发框架支持最新的DELPHI XE7
  6. iOS: 学习笔记, Swift与C指针交互(译)
  7. Java基础知识强化之集合框架笔记21:数据结构之 数组 和 链表
  8. H5混合开发二维码扫描以及调用本地摄像头
  9. CSS中的选择器之类选择器和id选择器
  10. 分析DuxCms之AdminUserModel
  11. 爬虫学习笔记(1)-- 利用Python从网页抓取数据
  12. firefox support.mozilla.org 的管理员没有正确配置好此网站。为避免您的信息失窃,Firefox 并未与此网站建立连接。
  13. WiFi-ESP8266入门http(2-1)文件系统-复杂结构的网页
  14. H5 页面在微信端的分享
  15. WINDOWS 命令行 串口 COM 发送数据
  16. 认识Thymeleaf:简单表达式和标签 基础信息
  17. 系统管理员常用的Linux命令
  18. Unity3D Mecanim :Body Mask的使用、 角色Retargeting原理分析、Apply RootMotion
  19. 利用三层判断sql数据库中编码是否已经存在(个人拙作,不喜勿喷)
  20. iOS #import和@class 区别

热门文章

  1. [SaSS] Using Object like style to create class dynamiclly
  2. JavaScript中的变量提升和严格模式
  3. SIGAI机器学习第九集 数据降维2
  4. 004_FreeRTOS创建与删除任务
  5. linux中fork--子进程是从哪里开始运行
  6. nodejs基础(回调函数、模块、事件、文件读写、目录的创建与删除)
  7. Hdu5178
  8. Java微信公众号开发梳理
  9. Python实现进度条的效果
  10. java课后实验性问题6