【发布时间】:2023-12-31 20:55:01
【问题描述】:
我的工作设置包括:
- 授权服务 (AS)
- 资源服务 (RS)
- 依赖方 (RP)
实现基于带有 Spring Security (OAuth2) 的 Spring Boot。我有以下工作 2LA 流程:
- RP 能够使用
client_secret和grant_type=client_credentials向AS 发送访问令牌请求。 - AS 以访问令牌响应 RP。
- RP 能够使用所述访问令牌向 RS 发出授权请求。
- RS 能够使用 AS 上的
/check_token端点验证访问令牌。
问题我需要对我的 AS 进行哪些更改,使其在上述第 1 步中接受基于 JWT 的访问令牌请求?
请注意,我不需要基于 JWT 的访问令牌。只有 RP 向 AS 请求访问令牌的初始请求应该是基于 JWT 的请求。
相关问题:How to use Spring OAuth2 JWT Token?
澄清我想知道 code 需要写什么才能使用 Spring Security OAuth2 库从 RP 接受 JWT。 RP的公钥在哪里添加到AS,RP的私钥在哪里添加到OAuth2 rest模板?
RP OAuth2 客户端配置:
@Configuration
@EnableOAuth2Client
open class OAuth2ClientConfiguration {
val tokenUrl = "http://localhost:8180/oauth/token"
@Bean
open fun resource(): OAuth2ProtectedResourceDetails {
return ClientCredentialsResourceDetails().apply {
clientId = "demo-rp"
clientSecret = "rp-secret"
grantType = "client_credentials"
scope = listOf("quotes")
accessTokenUri = tokenUrl
}
}
@Bean
open fun restTemplate(): OAuth2RestOperations {
return OAuth2RestTemplate(
resource(),
DefaultOAuth2ClientContext(DefaultAccessTokenRequest()))
}
}
AS OAuth2 配置:
@Configuration
@EnableAuthorizationServer
open class OAuth2Configuration : AuthorizationServerConfigurerAdapter() {
@Autowired
val authenticationManager: AuthenticationManager? = null
override fun configure(security: AuthorizationServerSecurityConfigurer) {
// @formatter:off
security
.tokenKeyAccess("isAnonymous() || hasAuthority('ROLE_TRUSTED_CLIENT')")
.checkTokenAccess("hasAuthority('ROLE_TRUSTED_CLIENT')")
// @formatter:on
}
override fun configure(clients: ClientDetailsServiceConfigurer) {
// @formatter:off
clients.inMemory()
.withClient("demo-rp")
.authorizedGrantTypes("client_credentials")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.scopes("quotes")
.secret("rp-secret")
.accessTokenValiditySeconds(60)
.and()
.withClient("demo-rs")
.authorizedGrantTypes("client_credentials")
.authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
.secret("rs-secret")
.accessTokenValiditySeconds(60)
// @formatter:on
}
override fun configure(endpoints: AuthorizationServerEndpointsConfigurer) {
// @formatter:off
endpoints
.authenticationManager(authenticationManager)
// @formatter:on
}
}
【问题讨论】:
标签: spring oauth spring-security spring-boot jwt