首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何验证来自OAuth服务器的令牌?

如何验证来自OAuth服务器的令牌?
EN

Stack Overflow用户
提问于 2017-01-25 22:48:55
回答 2查看 16K关注 0票数 4

我创建了一个Spring Boot应用程序,其中有授权和资源服务器,这是我的主类:

代码语言:javascript
复制
@SpringBootApplication
@EnableResourceServer
@EnableAuthorizationServer
public class OauthServerApplication {

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

这是我的application.yml:

代码语言:javascript
复制
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):

代码语言:javascript
复制
http://trustedclient:trustedclient123@localhost:8080/oauth/token?username=guest&password=guest123&grant_type=password

它返回:

代码语言:javascript
复制
{
  "access_token": "f2e722b7-3807-4a27-9281-5b28b7bd3d0d",
  "token_type": "bearer",
  "refresh_token": "f96d472c-8259-42e2-b939-4963dfeeb086",
  "scope": "openid"
}

现在我需要知道如何验证令牌是否正确,有什么帮助吗?

EN

回答 2

Stack Overflow用户

发布于 2017-06-17 19:16:41

您有多种可能性,您可以:

1)将令牌存储在TokenStore中,并在资源服务器的授权服务器上打开安全的验证令牌端点。

2)如果授权服务器和资源服务器可以共享一个DataSource,(在您的例子中很容易,因为两者都在同一个应用程序中)。它们都可以使用指向同一数据库的JdbcTokenStore,并且资源服务器可以直接检查该令牌存储中令牌的有效性。请参阅本教程:Spring REST API + OAuth2 + AngularJS

3)您可以通过JwtTokenStoreJwtAccessTokenConverter使用带签名的JWT令牌。请参阅本教程:Using JWT with Spring Security OAuth

这两个教程都基于以下github存储库:https://github.com/Baeldung/spring-security-oauth

票数 8
EN

Stack Overflow用户

发布于 2021-12-14 16:21:06

为了验证令牌,我通常在Authorization头中使用access token向/user发出请求。

在Spring Oauth服务器中,我添加了以下端点。

代码语言:javascript
复制
@GetMapping("/user")
public Principal user(Principal user) {
    return user;
}

这将是发送给它的请求。

代码语言:javascript
复制
GET http://localhost:8443/v1/auth/user
Authorization: Bearer some-access-token

这还有一个额外的好处,那就是返回通常需要的user对象。所以,一举两得。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41854531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档