【发布时间】:2020-11-04 03:06:27
【问题描述】:
我之前看到有人发布过有关此问题的帖子。问题是我希望 /api/v1/auth/ 控制器中的所有内容都不会通过 JWT 过滤器链。
这就是我的安全配置的样子
@Configuration
@EnableWebSecurity
class SecurityConfig() : WebSecurityConfigurerAdapter() {
@Autowired
lateinit var tokenService: TokenService
override fun configure(web: WebSecurity) {
web.ignoring().antMatchers(
"/v2/api-docs",
"/configuration/ui",
"/swagger-resources/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**",
"/api/v1/auth/**",
"/api/v1/auth/request",
"/api/v1/auth/verify",
"/api/v1/auth/verify_hack",
"/api/v1/auth/refresh_token",
"/messages",
"/index.html"
)
}
override fun configure(http: HttpSecurity) {
http.cors().and().csrf()
.disable()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/api/v1/auth/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilterBefore(JwtFilter(tokenService), UsernamePasswordAuthenticationFilter::class.java)
}
}
如果我在 /api/v1/auth 下点击一个端点并且 DONT 添加一个授权标头,它似乎绕过了 jwt 过滤器类,如果我确实添加了一个授权标头对于它总是进入 JWT 过滤器类的任何请求,我需要它完全忽略它,这是 configure 方法应该做的。
您可能会问为什么不直接发送 Auth 标头,我特别需要它用于 api/v1/auth/refresh_token 端点
这是最新最好的 Spring Boot 2.3.0。
TLDR 我到底如何让安全配置真正忽略路径
JWT 过滤器
class JwtFilter(private val tokenService: TokenService) : GenericFilterBean() {
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) {
val token = TokenUtil.extractToken(request as HttpServletRequest)
if (token != null && token.isNotEmpty()) {
try {
tokenService.getClaims(token)
} catch (e: SignatureException) {
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid JWT Signature")
} catch (e: MalformedJwtException) {
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid JWT token")
} catch (e: ExpiredJwtException) {
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Expired JWT token")
} catch (e: UnsupportedJwtException) {
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Unsupported JWT exception")
} catch (e: IllegalArgumentException) {
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Jwt claims string is empty")
}
} else {
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Missing auth token")
}
chain.doFilter(request, response)
}
}
【问题讨论】:
标签: spring spring-boot spring-security