【发布时间】:2020-04-07 19:31:32
【问题描述】:
我正在使用 Spring Boot 框架 实现 REST API。我有一个公共服务/auth/login
@PostMapping("/auth/login")
fun login(@RequestBody loginRequest: LoginRequest): String {
val token = tokenProvider.generateToken(loginRequest.username, loginRequest.password)
if (token === null) {
throw NotLoggedInError()
}
return token
}
它可用于检索安全区域/api/schemas的令牌:
@GetMapping
fun getSchemas() : ArrayList<Schema> = _schemas
我已经在自定义 WebSecurityConfigurerAdapter 对象的 configure(http: HttpSecurity?) 方法中配置了我的安全策略:
override fun configure(http: HttpSecurity?) {
http!!
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.exceptionHandling()
.defaultAuthenticationEntryPointFor(forbiddenEntryPoint(), PROTECTED_URLS)
.and()
.authenticationProvider(tokenAuth)
.addFilterBefore(restAuthenticationFilter(), AnonymousAuthenticationFilter::class.java)
.authorizeRequests()
.requestMatchers(PROTECTED_URLS).authenticated()
.and()
.csrf().disable()
.formLogin().disable()
.httpBasic().disable()
.logout().disable()
}
似乎总是调用过滤器的attemptAuthentication方法,即使是在公共区域访问的情况下。
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
class TokenAuthenticationFilter(requiresAuth: RequestMatcher) : AbstractAuthenticationProcessingFilter(requiresAuth) {
@Autowired
lateinit var tokenAuthenticationProvider: TokenAuthenticationProvider
private val BEARER = "Bearer"
override fun attemptAuthentication(request: HttpServletRequest?, response: HttpServletResponse?): Authentication {
val param: String? = request!!.getHeader("Authorization")
val token = removeStart(param!!, BEARER).trim()
val user = tokenAuthenticationProvider.getUserFromToken(token)
val auth = UsernamePasswordAuthenticationToken(user!!.username, user.password)
return authenticationManager.authenticate(auth)
}
override fun successfulAuthentication(request: HttpServletRequest?, response: HttpServletResponse?, chain: FilterChain?, authResult: Authentication?) {
super.successfulAuthentication(request, response, chain, authResult)
chain!!.doFilter(request, response)
}
override fun unsuccessfulAuthentication(request: HttpServletRequest?, response: HttpServletResponse?, failed: AuthenticationException?) {
throw NotLoggedInError()
}
}
有什么想法吗?提前感谢您的帮助。
问候。
【问题讨论】:
标签: java spring spring-boot kotlin