Springboot OIDC Integration
Springboot OIDC Integration
Table of Contents
Springboot OIDC Integration Guide............................................................................................................1
1. Create and configure an OIDC ClientID and Secret in OIDC provider system................................1
2. Add Maven Dependencies to enable OIDC (Using Okta OIDC client):...........................................1
OR Add Maven Dependencies to enable OIDC (Using Generic Spring Security Framework OIDC
client): 2
3. Application.yml(or application.properties) config :.......................................................................2
Using OKTA Client:..................................................................................................................................2
Using Generic Spring Security Framework OIDC client :........................................................................2
4. Java Code Changes :........................................................................................................................3
5. Getting User Attributes from OIDC ID Token :................................................................................3
6. Restrict access to a resource/endpoint based on Scope/Group or UserID :..................................4
- Endpoint level access control inside controller object:..................................................................4
- Global level access control in main class:.......................................................................................4
References...................................................................................................................................................4
Save the ClientID and ClientSecret which will be needed for further configuration.
OR Add Maven Dependencies to enable OIDC (Using Generic Spring Security Framework
OIDC client):
scopes:
- profile
- email
- openid
security:
oauth2:
client:
clientId: < clientId>
clientSecret: <clientSecret>
redirectUri: /authorization-code/callback(put specific to your app)
provider:
okta:
authorization-uri: https://{yourOktaDomain}/oauth2/default/v1/authorize
token-uri: https://{yourOktaDomain}/oauth2/default/v1/token
user-info-uri: https://{yourOktaDomain}/oauth2/default/v1/userinfo
jwk-set-uri: https://{yourOktaDomain}/oauth2/default/v1/keys
4. Java Code Changes : Java code changes are required to add fine-grained authorization
policies and to retrieve user details from the OIDC token.
@SpringBootApplication
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class CodeFlowExampleApplication {
/**
* The default Spring logout behavior redirects a user back to
{code}/login?logout{code}, so you will likely want
* to change that. The easiest way to do this is by extending from {@link
WebSecurityConfigurerAdapter}.
*/
@Configuration
static class WebConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
// allow antonymous access to the root page
.antMatchers("/").permitAll()
// all other requests
.anyRequest().authenticated()
// enable OAuth2/OIDC
.and().oauth2Client()
.and().oauth2Login();
}
}
5. Getting User Attributes from OIDC ID Token : An app can retrieve User
Attributes from an instance of class OAuth2AuthenticationToken as shown in below code
snippet.
@Controller
public class ExampleController {
@GetMapping("/profile")
@PreAuthorize("hasAuthority('SCOPE_profile')")
public ModelAndView userDetails(OAuth2AuthenticationToken authentication)
{
return new ModelAndView("userProfile" ,
Collections.singletonMap("details",
authentication.getPrincipal().getAttributes()));
}
}
@GetMapping("/profile")
@PreAuthorize("hasAuthority('SCOPE_profile')")
public ModelAndView userDetails(OAuth2AuthenticationToken authentication)
{
return new ModelAndView("userProfile" ,
Collections.singletonMap("details",
authentication.getPrincipal().getAttributes()));
}
- Global level access control in main class: Springboot supports adding complex
authorization rules in main java class inside WebSecurityConfigurerAdapter (explained
in step 4 above) config as shown in below example
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
protected void configure(final HttpSecurity http) throws Exception {
http.antMatcher("/**")
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/restricted").hasAuthority("SCOPE_custom")
.anyRequest().authenticated()
.and().oauth2Login(); // <-- THIS WAS CHANGED
}
}
Note : Find more details about expression based access control at documentation
provided by Spring framework at
https://docs.spring.io/spring-security/site/docs/5.0.7.RELEASE/reference/html/el-
access.html
References
(n.d.). Retrieved from https://developer.okta.com/blog/2019/06/20/spring-preauthorize