Spring boot 2.0.3安全性Oauth2自动配置
我正在使用OAuth2和微服务,我创建了一个微服务来生成授权令牌和另一个微服务作为客户端 . 生成令牌是有效的,但是当我尝试在客户端服务上使用此生成的令牌进行身份验证时,它无法正常工作 .
生成令牌的微服务:localhost:9999
使用url生成的令牌: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);
}
}
application.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
1 回答
我试图复制你的用例:我开发了一个具有spring cloud安全性和ResourceServer的AuthServer . 我看到使用token-info-uri策略的问题是spring为了检查令牌使用RemoteTokenServices并且它使用OAuth2RestTemplate来检索令牌信息说如果你想使用你的配置你已插入这种配置如下:
支付对
@EnableOAuth2Client
和OAuth2RestTemplate
bean定义的争用 . spring使用这些配置来有效并刷新令牌 .但是,这样任何资源服务器都必须是客户端应用程序,根据我的经验,它不能扩展 . 我个人的建议是使用user-info-uri策略 . 在这种情况下,spring将使用特殊 endpoints 来退出用户invormation . 您的资源服务器中的配置非常简单,您只需定义
@EnableResourceServer
就像您的yaml中的示例一样,您只能配置资源部分,如下所示唯一的另一个开发是在您的auth服务器中,必须在 endpoints 中公开用户信息,如下所示:
我多次使用这种方法,并且我注意到它的效果非常好并且可以扩展,因为在资源服务器中,您没有像客户端应用程序那样定义它 .
我希望它可能有用
附:
在您的配置中,您忘记了http协议: