Skip to content

A set of sample applications to demonstrate integration of OIDC, X509 exchange etc via Keycloak using Spring security libraries

Notifications You must be signed in to change notification settings

winster/springsecurity-keycloak

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting Started

Sample application to demonstrate OAuth grant type client-credentials

CALLEE - A Resource Server

  • callee has an endpoint which has to be secured - in OAuth2.0 terms, callee is a Resource Server
  • callee will check for bearer token for all of its resources (by default all) - This is achieved by the libraries in classpath and spring.security.oauth2.resourceserver.jwt.jwk-set-uri

CALLER - A Client

  • caller has a background job calling the endpoint of callee - in OAuth2.0 terms, caller is a Client
  • Create a realm in Keycloak from realm-export.json. Regenerate secret from the credentials tab and copy the value to spring.security.oauth2.client.registration.keycloak.client-secret
  • Take a look at WebclientConfig.java for the webclient Bean and authorizedClientManager bean
  • Now run both applications (using mvn spring-boot:run or from IDE or as you wish)

WEB - OIDC based browser flow

Learnings

Client Credentials flow

  • DefaultReactiveOAuth2AuthorizedClientManager is not suitable for scheduled/background tasks as it is meant for httpservlet context and it will lead to IllegalArgumentException: serverWebExchange cannot be null. So AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager should be used
  • If Client is using https protocol with Auth Server, then Resource Server should also use https while validating the token against Auth Server. Otherwise iss claim check will fail.
  • But previous statement is not valid for authorization_code grant type. I have used https uri for all endpoints except jwkset and it worked. jwkset was excluded as I could not find a way to use a custom JwtDecoder. See it working in web module
  • Spring Oauth Jose library 5.3.3 supports both JwtDecoder and ReactiveJwtDecoder. If you want to use webclient use the latter
  • With access token lifespan at 1 minute, causes the Client to request for new token every time. Client's scheduled job runs every 10 seconds. Changing the lifespan to 5 minutes seems to resolve the issue. OTOH, explicit logout of sessions from Auth Server does not seem to have any effect on Client or Resource Server. Yet to explore further...

Browser flow-x509

  • At least to me, Keycloak documentation was not sufficient to start. Learning is that when Keycloak says add some execution in Browser flow, it means that the user agent first visits your web application and then redirects to keycloak for auth. And Keycloak can be behind a reverse proxy
  • If you want to enable client certificate based authentication based on http path (say customer X wants username/password login form whereas customer Y wants x509 certificate based auth), it is not possible (please correct me if I'm wrong) to do so in HAProxy.
  • Apache httpd could be a good candidate (https://httpd.apache.org/docs/current/mod/core.html#locationmatch and https://httpd.apache.org/docs/current/mod/mod_ssl.html#SSLVerifyClient)

References

About

A set of sample applications to demonstrate integration of OIDC, X509 exchange etc via Keycloak using Spring security libraries

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published