【发布时间】:2020-11-09 22:23:49
【问题描述】:
我正在编写一个 API,它必须验证 JWS 是否具有有效的签名并且它没有过期 - 我的项目有几个路由,只有一个路由受到保护。
我已经创建了一个过滤器:
@Component
class JWTFilter : OncePerRequestFilter() {
override fun doFilterInternal(req: HttpServletRequest, resp: HttpServletResponse, chain: FilterChain) {
// code to validate JWT
chain.doFilter(req, resp)
}
}
还有 SecConfig:
@Configuration
@EnableWebSecurity
class SecConfig(val authRequestFilter: AuthRequestFilter) : WebSecurityConfigurerAdapter() {
override fun configure(http: HttpSecurity) {
http
.authorizeRequests()
.antMatchers("/cash")
.authenticated()
.anyRequest()
.permitAll()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
http.addFilterBefore(authRequestFilter, <WhatShouldIPutHere.class>)
}
}
我创建了 SecConfig 类来集中所有配置,我的 API 没有任何登录,我只收到 JWS 令牌,我需要确保它是有效的。
<WhatShouldIPutHere.class> 中使用的正确实现是什么?
编辑:
它有效,但如果我的应用程序中没有用户名/密码验证,那么使用 UsernamePasswordAuthenticationFilter 有什么意义?
http.addFilterBefore(authRequestFilter, UsernamePasswordAuthenticationFilter::class.java)
编辑 2:
为什么要验证每个请求?我只想验证/cash
【问题讨论】:
标签: spring-boot jwt