我创建了一个Spring Boot应用程序,其中有授权和资源服务器,这是我的主类:
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
public class OauthServerApplication {
public static void main(String[] args) {
SpringApplication.run(OauthServerApplication.class, args);
}
}
这是我的application.yml:
security:
user:
name: guest
password: guest123
oauth2:
client:
client-id: trustedclient
client-secret: trustedclient123
authorized-grant-types: authorization_code,refresh_token,password
scope: openid
要生成访问令牌,我只执行这个url (POST):
http://trustedclient:trustedclient123@localhost:8080/oauth/token?username=guest&password=guest123&grant_type=password
它返回:
{
"access_token": "f2e722b7-3807-4a27-9281-5b28b7bd3d0d",
"token_type": "bearer",
"refresh_token": "f96d472c-8259-42e2-b939-4963dfeeb086",
"scope": "openid"
}
现在我需要知道如何验证令牌是否正确,有什么帮助吗?
发布于 2017-06-17 19:16:41
您有多种可能性,您可以:
1)将令牌存储在TokenStore
中,并在资源服务器的授权服务器上打开安全的验证令牌端点。
2)如果授权服务器和资源服务器可以共享一个DataSource,(在您的例子中很容易,因为两者都在同一个应用程序中)。它们都可以使用指向同一数据库的JdbcTokenStore
,并且资源服务器可以直接检查该令牌存储中令牌的有效性。请参阅本教程:Spring REST API + OAuth2 + AngularJS
3)您可以通过JwtTokenStore
和JwtAccessTokenConverter
使用带签名的JWT令牌。请参阅本教程:Using JWT with Spring Security OAuth
这两个教程都基于以下github存储库:https://github.com/Baeldung/spring-security-oauth
发布于 2021-12-14 16:21:06
为了验证令牌,我通常在Authorization头中使用access token向/user发出请求。
在Spring Oauth服务器中,我添加了以下端点。
@GetMapping("/user")
public Principal user(Principal user) {
return user;
}
这将是发送给它的请求。
GET http://localhost:8443/v1/auth/user
Authorization: Bearer some-access-token
这还有一个额外的好处,那就是返回通常需要的user对象。所以,一举两得。
https://stackoverflow.com/questions/41854531
复制相似问题