【发布时间】:2022-08-09 15:30:08
【问题描述】:
我有一个使用 OAuth2 和 JWT 进行身份验证的 Spring Boot 应用程序。在我的application-development.yml 中,我有以下配置:
security:
jwt:
key-store: classpath:keystore.jks
key-store-password: mySecretPassword1
key-pair-alias: clientId
key-pair-password: mySecretPassword1
在我的AuthorizationServerConfiguration.java 中,我有:
@Override
public void configure(AuthorizationServerEndpointsConfigurer authorizationServerEndpointsConfigurer) throws Exception {
authorizationServerEndpointsConfigurer
.authenticationManager(authenticationManager)
.accessTokenConverter(jwtAccessTokenConverter())
.reuseRefreshTokens(false)
.userDetailsService(this.userDetailsService)
.exceptionTranslator(webResponseExceptionTranslator)
.tokenStore(tokenStore())
.pathMapping(\"/oauth/token\", \"/login\");
}
使用它,我可以使用 Postman 登录系统。但是,每次我需要登录时,在 Postman 中,我都需要为基本身份验证和授权类型配置客户端 ID 和客户端密码,如下面的屏幕截图所示:
我需要能够在每次不提供 clientId、clientSecret 和 grant_type 的情况下登录。
我已经看到在使用云配置的微服务的情况下做了类似的事情,如下所示:
spring:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: auth-service
uri: lb://auth-service
predicates:
- Path=/auth/**
filters:
- RewritePath=/auth/(?<path>.*), /$\\{path}
- id: auth-service
uri: lb://auth-service
predicates:
- Path=/login/**
filters:
- RewritePath=/login(?<path>.*), /oauth/token$\\{path}
- AddRequestHeader=Authorization,Basic BaS!CAuthT0ken
- AddRequestParameter=grant_type,password
在这里,过滤器被添加到网关中,它工作得非常好。在不使用网关的情况下,我需要一种在 Monolith 的情况下复制相同内容的方法。我需要一种方法来替换以下代码:
- AddRequestHeader=Authorization,Basic BaS!CAuthT0ken
- AddRequestParameter=grant_type,password
我尝试在我的 AuthorizationServerConfiguration 中使用以下内容:
@Override
public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
clientDetailsServiceConfigurer.jdbc(this.dataSource)
.withClient(\"clientId\")
.authorizedGrantTypes(\"password\")
.secret(encoder().encode(\"mySecretPassword1\"))
.scopes(\"read\",\"write\");
但是我没有发现成功,因为它会导致以下错误:
{
\"error\": \"unauthorized\",
\"error_description\": \"Full authentication is required to access this resource\"
}
任何帮助,将不胜感激。谢谢。
-
Ehrm,所以您想要安全,但您不想要安全,因为您不想提供凭据?
-
我不希望客户端需要从客户端输入客户端 ID 和密码。我想在后端本身处理这个。对此有什么想法吗?
标签: spring-boot oauth-2.0 jwt basic-authentication