【发布时间】:2021-07-28 20:40:22
【问题描述】:
我的项目中有一个 oauth 流程。
我在前端检索一个 jwt 令牌并将其添加到授权标头中的每个请求中。
现在我需要验证所述令牌并在我的后端验证签名,这是一个 kotlin spring boot 应用程序。
我知道如何使用 jjwt 库验证令牌,但我不明白验证在哪里完成。
我有一个证书来验证令牌,只是想让带有有效令牌的请求得到处理。
我在网上看到有些人使用他们添加到 SecurityConfiguration 中的 OncePerRequestFilter 来执行此操作,但我不明白发生了什么以及它是如何工作的。
我尝试在线搜索教程,但其中许多都创建了一个既是授权服务器又是资源服务器的后端。我只想让后端成为资源服务器,如果令牌有效,则检查证书并处理请求。我该怎么做?
现在这是我的 SecurityConfiguration :
package com.renaulttrucks.transfertprotocolbackend.security.config
import org.springframework.beans.factory.annotation.Value
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {
@Value("\${security.enabled}")
val securityEnabled : Boolean? = false
@Throws(Exception::class)
override fun configure(http: HttpSecurity) {
if(!securityEnabled!!) {
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/**").permitAll()
.and()
.csrf().disable()
.formLogin().disable()
} else {
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/**").permitAll()
.and()
.csrf().disable()
.formLogin().disable()
}
}
}
【问题讨论】:
-
查看 spring-security 的示例:github.com/spring-projects/spring-security-samples/tree/main/…
标签: spring spring-boot spring-security