简体   繁体   English

如何在Spring中使用OAuth2和JWT令牌代表特定用户调用受保护的资源?

[英]How to call a protected resource on behalf of a specific user using OAuth2 and JWT token in Spring?

So we have an authentication server where the UI application gets the access token and then it communicate with API server, it's all good. 因此,我们有一个身份验证服务器,UI应用程序在其中获取访问令牌,然后与API服务器通信,这一切都很好。 Now we are building a third application which needs SSO to authenticate the same user and that is fine too. 现在,我们正在构建第三个需要SSO来验证同一用户的应用程序,这也很好。

However, there are scenarios where this third application needs to use some resources on the API server which, from my understanding, we need to get a token from auth server using client-id/secret and then send the request with the access token. 但是,在某些情况下,第三个应用程序需要使用API​​服务器上的一些资源,据我所知,我们需要使用client-id / secret从auth服务器获取令牌,然后发送带有访问令牌的请求。 This seems ok too, however I am not sure how API server is going to authorise that token ( a hint on this would be great ). 这似乎也可以,但是我不确定API服务器将如何授权该令牌( 对此的提示会很棒 )。

But the main problem is we want this request to be sent on behalf of the user. 但是主要的问题是我们希望该请求代表用户发送。 This is because API server needs to audit all user's activities. 这是因为API服务器需要审核所有用户的活动。 How can we achieve this using Spring Boot/OAuth2 and JWT Token? 我们如何使用Spring Boot / OAuth2和JWT Token实现这一目标?

I went through documentation and I know about @EnableOauth2Sso @EnableAuthorisationServer etc. but this scenario is not clear and I'm not even sure it's been implemented in Spring or not. 我浏览了文档,并且了解@ EnableOauth2Sso @EnableAuthorisationServer等。但是这种情况尚不清楚,我甚至不确定它是否在Spring中实现。

If there is no implementation for this scenario, what do you recommend? 如果没有针对此方案的实现,那么您建议什么? Any experience you have had on this, can you please share? 您对此有任何经验,可以分享一下吗?

Your API server plays the role of a Resource Server. 您的API服务器扮演资源服务器的角色。 There is an annotation designed for that purpose: @EnableResourceServer . 为此目的设计了一个注释: @EnableResourceServer Your client app then will consume this resource using the handy OAuth2RestTemplate . 然后,您的客户端应用程序将使用方便的OAuth2RestTemplate消耗此资源。

There are two approaches to properly configure the Resource Server and get this working: 有两种方法可以正确配置资源服务器并使之正常工作:

  1. Have the public key directly in your resource server app: this way when the client app try to use a token provided by the authorization server to get a resource from the Resource Server, this will verify if the token is valid by itself. 直接在您的资源服务器应用程序中拥有公钥:以这种方式,当客户端应用程序尝试使用授权服务器提供的令牌从资源服务器获取资源时,这将验证令牌本身是否有效。
  2. Configure the resource server to ask the authorization server if a given access token is valid and depending of the response it will allow or decline to get the resource. 配置资源服务器以询问授权服务器给定的访问令牌是否有效,并取决于响应将允许还是拒绝获取资源。

I have posted a sample app on github using the first approach. 我使用第一种方法在github上发布了一个示例应用程序 There you can see the interaction between the Authorization Server, the Client and the Resource Server, as well as all the configurations you need for this implementation. 在这里,您可以看到授权服务器,客户端和资源服务器之间的交互,以及此实现所需的所有配置。 Hope it helps you. 希望对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当用户使用jwt,Oauth2和spring security进行首次请求时,如何在服务层上获取jwt令牌字符串? - How to get jwt token string on service layer when user request first time using jwt, Oauth2, spring security? Spring Boot OAuth2 资源服务器:库如何在没有公钥的情况下验证 JWT 令牌? - Spring Boot OAuth2 Resource Server: How library can verify JWT token without public key? 如何撤销用户作为管理员用户的访问令牌和刷新令牌? 在 Oauth2 中使用 JWT 时 - How to revoke the access token and refresh token of the user as an admin user? while using JWT in Oauth2 Spring OAuth2 + JWT,如何将外部访问令牌映射到本地用户 - Spring OAuth2 + JWT, how to map external access token to local user 如何在 Spring Boot OAuth2 中的 SecurityContextHolder 中获取 JWT 令牌? - How to get JWT token in SecurityContextHolder in Spring Boot OAuth2? Spring Security OAuth2 JWT 匿名令牌 - Spring Security OAuth2 JWT anonymous token Spring Webflux SecurityValidate OAuth2 JWT 令牌 - Spring Webflux SecurityValidate OAuth2 JWT Token 如何从 Spring 启动 Java 客户端调用 Oauth2 保护端点使用 WebClient “serverWebExchange 不能为空” - How to call Oauth2 protected endpoint from a Spring Boot Java Client using WebClient “serverWebExchange cannot be null” Spring OAuth2 - 如何使用我生成的调用 Web 服务的 JWT 令牌来保护 Spring Boot REST API? - Spring OAuth2 - How to secure spring boot REST API using JWT token which i have generated calling webservice? 如何从Servlet调用Spring OAuth2资源服务器URI - how to call spring oauth2 resource server uri from servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM