Snailclimb / spring-security-jwt-guide

从零入门 !Spring Security With JWT(含权限验证)后端部分代码。
1.37k stars 437 forks source link

关于配置context-paht为/api的配置 #30

Open carryxiong opened 3 years ago

carryxiong commented 3 years ago

你这样配置了这个context-path为api,然后白名单里面又设置/api/auth/login放行,但是这个配置不会生效。 springsecurity默认匹配url的时候,不会携带项目名,也就是你的/api。 这里问题很大,我调试了很久才找到问题。不明白你测试的时候怎么会通过的,你登录的时候接口不会报错吗。

shaohan0228 commented 3 years ago

我也是,我默认context-path为/时,无法达到同样的效果 结果发现spring security做匹配的时候,是不会携带context-path做匹配的

// 登录接口
.antMatchers(HttpMethod.POST, SecurityConstants.LOGIN_WHITELIST).permitAll()
// 指定路径下的资源需要验证了的用户才能访问
.antMatchers(SecurityConstants.FILTER_ALL).authenticated()
.antMatchers(HttpMethod.DELETE, SecurityConstants.FILTER_ALL).hasRole("ADMIN")
// 其他都放行了
.anyRequest().permitAll()

这一段,实际上只有.anyRequest().permitAll()生效了

carryxiong commented 3 years ago

我也是,我默认context-path为/时,无法达到同样的效果 结果发现spring security做匹配的时候,是不会携带context-path做匹配的

// 登录接口
.antMatchers(HttpMethod.POST, SecurityConstants.LOGIN_WHITELIST).permitAll()
// 指定路径下的资源需要验证了的用户才能访问
.antMatchers(SecurityConstants.FILTER_ALL).authenticated()
.antMatchers(HttpMethod.DELETE, SecurityConstants.FILTER_ALL).hasRole("ADMIN")
// 其他都放行了
.anyRequest().permitAll()

这一段,实际上只有.anyRequest().permitAll()生效了

是的,统统失败了,这个还是我调试了很久,找到匹配源码才发现/api并没有参与匹配,所以这个demo问题很大,就觉得作者调试的时候为啥这么严重的bug没发现。

Snailclimb commented 3 years ago

我脑子瓦特了!哈哈哈!已经修改了~感谢两位老哥 @carryxiong @shaohan0228 🤟