gpt4 book ai didi

java - Spring boot 2.0.3 + 安全性 + Oauth2 自动配置

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:01:28 30 4
gpt4 key购买 nike

Spring boot 2.0.3 + 安全性 + Oauth2 自动配置

我正在使用 OAuth2 和微服务,我创建了一个微服务来生成授权 token 和另一个微服务作为客户端。 token 的生成工作正常,但是当我尝试在客户端服务上使用这个生成的 token 进行身份验证时,它不起作用。

生成 token 的微服务:localhost:9999

使用 url 生成的 token :estrutura:estruturasecret@localhost:9999/oauth/token

   [
{
"key":"grant_type",
"value":"password"
},
{
"key":"username",
"value":"matheus"
},
{
"key":"password",
"value":"teste"
},
{
"key":"client_id",
"value":"estrutura"
}
]

返回:

 {
"access_token": "2e4c26b3-0fcf-493e-a255-6216b98811c5",
"token_type": "bearer",
"refresh_token": "5e33740a-ccb9-4ec1-94be-3a4643b8097a",
"expires_in": 42479,
"scope": "read write"
}

客户微服务:localhost:9090

@SpringBootApplication
@EnableResourceServer
public class ClientServer {
public static void main(String[] args) {
SpringApplication.run(ClientServer.class, args);
}
}

应用程序.yml:

server:
port: 9090
servlet:
context-path: /client
spring:
application:
name: client-server
security:
oauth2:
client:
client-id: estrutura
client-secret: estruturasecret
access-token-uri: localhost:9999/oauth/token
user-authorization-uri: localhost:9999/oauth/authorize
resource:
token-info-uri: localhost:9999/oauth/check_token
logging:
level:
org.springframework.security: DEBUG

错误:

<error>invalid_token</error>
<error_description>Invalid access token: 2e4c26b3-0fcf-493e-a255-6216b98811c5</error_description>

日志:

2018-06-26 11:24:42.641 DEBUG 18658 --- [nio-9090-exec-2] o.s.security.web.FilterChainProxy        : /alunos at position 5 of 11 in additional filter chain; firing Filter: 'OAuth2AuthenticationProcessingFilter'
2018-06-26 11:24:42.641 DEBUG 18658 --- [nio-9090-exec-2] p.a.OAuth2AuthenticationProcessingFilter : Authentication request failed: error="invalid_token", error_description="Invalid access token: 2e4c26b3-0fcf-493e-a255-6216b98811c5"
2018-06-26 11:24:42.645 DEBUG 18658 --- [nio-9090-exec-2] o.s.s.w.header.writers.HstsHeaderWriter : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@1fa75b
2018-06-26 11:24:42.647 DEBUG 18658 --- [nio-9090-exec-2] s.s.o.p.e.DefaultOAuth2ExceptionRenderer : Written [error="invalid_token", error_description="Invalid access token: 2e4c26b3-0fcf-493e-a255-6216b98811c5"] as "application/xml;charset=UTF-8" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@30d6fa45]
2018-06-26 11:24:42.647 DEBUG 18658 --- [nio-9090-exec-2] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed

文档: https://docs.spring.io/spring-security-oauth2-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security-oauth2-authorization-server

最佳答案

我试图复制您的用例:我开发了一个具有 spring cloud security 和 ResourceServer 的 AuthServer。我看到使用 token-info-uri 策略的问题是 spring 为了检查 token 使用 RemoteTokenServices 并且它使用 OAuth2RestTemplate 来检索 token 信息说如果你想使用你的配置你已经插入这种如下配置:

@EnableOAuth2Client
@EnableResourceServer
@SpringBootApplication
public class HelloOauthServiceApplication {

public static void main(String[] args) {
SpringApplication.run(HelloOauthServiceApplication.class, args);
}

@Bean
public OAuth2RestTemplate oAuth2RestTemplate(OAuth2ProtectedResourceDetails resource){
return new OAuth2RestTemplate(resource);
}
}

注意 @EnableOAuth2ClientOAuth2RestTemplate bean 定义。 spring 使用这些配置来验证和刷新您的 token 。

然而,以这种方式,任何资源服务器都必须也是客户端应用程序,并且根据我的经验,它无法扩展。我个人的建议是使用 user-info-uri 策略。在这种情况下,spring 将使用一个特殊的端点来检索用户信息。在您的资源服务器中配置非常简单,您只定义了 @EnableResourceServer 就像您在 yaml 中的示例一样,您可以仅配置资源部分,如下所示

security:
oauth2:
resource:
user-info-uri: http://localhost:9090/account/userInfo.json
preferTokenInfo: false

唯一的额外开发是在您的身份验证服务器中,它必须在如下端点中公开用户信息:

@RestController
@RequestMapping("/account")
class UserRestFullEndPoint {

@GetMapping("/userInfo")
public Principal userInfo(Principal principal){
return principal;
}
}

我多次使用这种方法并且我注意到它工作得很好并且可以扩展,因为在您的资源服务器中您没有像客户端应用程序那样定义它。

希望对你有用。

PS:在你的配置中你忘记了 http 协议(protocol):

server:
port: 9090
servlet:
context-path: /client
spring:
application:
name: client-server
security:
oauth2:
client:
client-id: estrutura
client-secret: estruturasecret
access-token-uri: http://localhost:9999/oauth/token
user-authorization-uri: http://localhost:9999/oauth/authorize
resource:
token-info-uri: http://localhost:9999/oauth/check_token
logging:
level:
org.springframework.security: DEBUG

关于java - Spring boot 2.0.3 + 安全性 + Oauth2 自动配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51048179/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com