Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How-to: Use Spring Authorization server behind Spring Cloud Gateway #564

Open
rcbandit111 opened this issue Jan 2, 2022 · 19 comments
Open
Assignees
Labels
type: documentation A documentation update

Comments

@rcbandit111
Copy link

Publish a guide on How-to: Implement the core services behind Spring Cloud Gateway:

@rcbandit111 rcbandit111 added the type: enhancement A general enhancement label Jan 2, 2022
@sjohnr
Copy link
Member

sjohnr commented Jan 3, 2022

Related #499

@rcbandit111
Copy link
Author

When I can expect this guide?

@rcbandit111
Copy link
Author

rcbandit111 commented Jan 27, 2022

Kind remind guys. I will appreciate it if someone write me some configuration steps before official documentation is ready.

@jgrandja
Copy link
Collaborator

jgrandja commented Jan 27, 2022

@rcbandit111 It will be a while before we get to this as there are a few higher priority tasks we need to work on first.

If someone in the community can help that would be great. cc @spencergibb

@rcbandit111
Copy link
Author

@jgrandja Thank you for the response. I will wait further information.

@jgrandja jgrandja changed the title How to: Use Spring Authorization server behind Spring Cloud Gateway How-to: Use Spring Authorization server behind Spring Cloud Gateway Feb 8, 2022
@jacko9et
Copy link

No additional configuration is required if it is only used behind a gateway. However, in the case of load balancing, spring session support is required, because the server uses session to associate authentication actions.

@salutonmondo
Copy link

salutonmondo commented Jul 22, 2022

Is this suit your needs #807

@sjohnr
Copy link
Member

sjohnr commented Jul 22, 2022

@salutonmondo I believe this issue is addressing using Spring Authorization Server behind Spring Cloud Gateway, whereas the sample you submitted treats SAS as an identity provider but does not route the requests to it. When we look more deeply at this issue, we'll have to think about what use cases make sense for putting SAS behind the gateway. One that I can think of, for example, is rate limiting to protect the authorization server or specific endpoints from malicious clients.

@uniquejava
Copy link

@salutonmondo Thanks for your demo, I added some comments there in your #807, could you shed some light?

@sjohnr sjohnr self-assigned this Jan 3, 2023
@zhoyq-work
Copy link

use database to share data between auth servers, this can reslove load balancing problem.

@zhenhe
Copy link

zhenhe commented Mar 7, 2023

any update recently?

@maradanasai
Copy link

Hi, any update?

@uniquejava
Copy link

I once needed this, after some research, I found what I really need is how to use spring cloud gateway as resource server and verify token from this single point. Finally I managed to do this, and I don't think I need the auth server sit behind spring cloud gateway now. Here is my current demo architecture.

image

The benefit: I just verify token from spring cloud gateway and pass the resolved user id as http header to my downstream micro-services and all my microservices now don't even have a dependency on spring security.

I once used spring cloud gateway as oauth2 client, --- almost each tutorial on the internet do this, and I think that is too academic/complex and not feasible at all in a real large application.

@zhenhe
Copy link

zhenhe commented Mar 15, 2023

I once needed this, after some research, I found what I really need is how to use spring cloud gateway as resource server and verify token from this single point. Finally I managed to do this, and I don't think I need the auth server sit behind spring cloud gateway now. Here is my current demo architecture.

image

The benefit: I just verify token from spring cloud gateway and pass the resolved user id as http header to my downstream micro-services and all my microservices now don't even have a dependency on spring security.

I once used spring cloud gateway as oauth2 client, --- almost each tutorial on the internet do this, and I think that is too academic/complex and not feasible at all in a real large application.

good advice,how scg do both verify token and then pass request ? use filter?

@uniquejava
Copy link

@zhenhe Yes, a single UserHeaderFilter will do the job, and here is an example: https://stackoverflow.com/a/71485398/2497876

@dangfsix
Copy link

I once needed this, after some research, I found what I really need is how to use spring cloud gateway as resource server and verify token from this single point. Finally I managed to do this, and I don't think I need the auth server sit behind spring cloud gateway now. Here is my current demo architecture.

image

The benefit: I just verify token from spring cloud gateway and pass the resolved user id as http header to my downstream micro-services and all my microservices now don't even have a dependency on spring security.

I once used spring cloud gateway as oauth2 client, --- almost each tutorial on the internet do this, and I think that is too academic/complex and not feasible at all in a real large application.

Hi. Can you make a sample repo? Many thanks.

@jgrandja jgrandja added type: documentation A documentation update and removed type: enhancement A general enhancement labels May 27, 2023
@Crain-32
Copy link

Hey @jgrandja, I've encountered this issue myself at work, and would be willing to write up the documentation if I could get some help getting it working in a Spring Preferred way.

I know it was declined, but since I haven't contributed to Spring Authorization before, I have written documentation for Validation in Spring Boot.

The issues I'm seeing are that Spring Authorization Server currently holds the client information within a Session Context. This is fine (and the preferred security option), however for the Token to be passed through the Gateway that Session either needs to be shared between the Authorization Server and the Gateway (Spring Session likely works, just unsure the proper configuration/dependencies), or some changes to the flow so that the gateway has the Session -> Token link instead.

The flow works as expected when the Authorization Server is the Identity Provider and the Gateway is the client. But that isn't always possible.

Some thoughts I've had are that you put the Authorization Server into the Gateway Application, so that way the Gateway/Authorization are in the same place. This works for when you're using a 3rd Party Identity Provider. However isn't really a preferred "Microservice" pattern. For example in the flow I'm aiming for (which is vague enough I can share) I would like to do the following.

  • Unauthenticated User hits Spring Gateway
  • Redirect /login, routes to Spring Authorization Server
  • Authorization Server flows through one of several Identity Providers
  • On getting the Access Token, Adjust Claims (Documented already here, and is why I'm not just having the Gateway be a direct client to the Provider)
  • Access/Refresh Token gets passed to the Gateway, which will convert the Session Cookie into the Token (Using the TokenRelay Filter)
  • Request is forwarded to the proper location with the Access Token
  • Token Refresh is handled by the Gateway.

I'd also be fine if the flow was using Spring Session to Distribute the information between the Authorization Server and Gateway.

If you'd like, I can share a repository with an example setup. Being Eureka, a Gateway, Authorization Server, and Dummy API.

@Crain-32
Copy link

Example Setup can be found here.

@Crain-32
Copy link

Minor update.
I've attempted Distribution of information with Spring Session and a backer like Redis. It didn't work.

I've attempted using a ProxyExchange Approach instead of the typical links, it appears without some level of Special handling in the gateway putting the Authorization Server behind it just isn't possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
Status: Planning
Development

No branches or pull requests