Last active
December 15, 2020 16:38
-
-
Save bvulaj/acf814188a3ac6e9ec7b775cd6691f98 to your computer and use it in GitHub Desktop.
Multiple Oauth2 Providers in Spring Security
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
facebook.client.client-id= | |
facebook.client.client-secret= | |
facebook.client.access-token-uri=https://graph.facebook.com/oauth/access_token | |
facebook.client.user-authorization-uri=https://www.facebook.com/dialog/oauth | |
facebook.client.token-name=oauth_token | |
facebook.client.authentication-scheme=query | |
facebook.client.client-authentication-scheme=form | |
facebook.client.scope=public_profile,email | |
facebook.resource.user-info-uri=https://graph.facebook.com/me | |
google.client.client-id= | |
google.client.client-secret= | |
google.client.access-token-uri=https://www.googleapis.com/oauth2/v4/token | |
google.client.user-authorization-uri=https://accounts.google.com/o/oauth2/v2/auth | |
google.client.scope=openid,email,profile | |
google.client.client-authentication-scheme=form | |
google.client.authentication-scheme=query | |
google.resource.user-info-uri=https://www.googleapis.com/oauth2/v3/userinfo | |
google.resource.prefer-token-info=true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Autowired | |
private FacebookPrincipalExtractor facebookPrincipalExtractor; | |
@Autowired | |
private FacebookAuthoritiesExtractor facebookAuthoritiesExtractor; | |
@Autowired | |
private GooglePrincipalExtractor googlePrincipalExtractor; | |
@Autowired | |
private GoogleAuthoritiesExtractor googleAuthoritiesExtractor; | |
@Bean | |
@ConfigurationProperties("facebook.client") | |
public AuthorizationCodeResourceDetails facebook() { | |
return new AuthorizationCodeResourceDetails(); | |
} | |
@Bean | |
@ConfigurationProperties("facebook.resource") | |
public ResourceServerProperties facebookResource() { | |
return new ResourceServerProperties(); | |
} | |
@Bean | |
@ConfigurationProperties("google.client") | |
public AuthorizationCodeResourceDetails google() { | |
return new AuthorizationCodeResourceDetails(); | |
} | |
@Bean | |
@ConfigurationProperties("google.resource") | |
public ResourceServerProperties googleResource() { | |
return new ResourceServerProperties(); | |
} | |
@Bean | |
public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) { | |
FilterRegistrationBean registration = new FilterRegistrationBean(); | |
registration.setFilter(filter); | |
registration.setOrder(-100); | |
return registration; | |
} | |
private Filter ssoFilter() { | |
CompositeFilter filter = new CompositeFilter(); | |
List<Filter> filters = new ArrayList<>(); | |
SimpleUrlAuthenticationSuccessHandler successHandler = new SimpleUrlAuthenticationSuccessHandler(); | |
successHandler.setAlwaysUseDefaultTargetUrl(true); | |
successHandler.setDefaultTargetUrl("/user"); | |
OAuth2ClientAuthenticationProcessingFilter facebookFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/facebook"); | |
OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(facebook(), oauth2ClientContext); | |
facebookFilter.setRestTemplate(facebookTemplate); | |
UserInfoTokenServices facebookTokenServices = new UserInfoTokenServices(facebookResource().getUserInfoUri(), facebook().getClientId()); | |
facebookTokenServices.setRestTemplate(facebookTemplate); | |
facebookTokenServices.setPrincipalExtractor(facebookPrincipalExtractor); | |
facebookTokenServices.setAuthoritiesExtractor(facebookAuthoritiesExtractor); | |
facebookFilter.setTokenServices(facebookTokenServices); | |
facebookFilter.setAuthenticationSuccessHandler(successHandler); | |
filters.add(facebookFilter); | |
OAuth2ClientAuthenticationProcessingFilter googleFilter = new OAuth2ClientAuthenticationProcessingFilter("/login/google"); | |
OAuth2RestTemplate googleTemplate = new OAuth2RestTemplate(google(), oauth2ClientContext); | |
googleFilter.setRestTemplate(googleTemplate); | |
UserInfoTokenServices googleTokenServices = new UserInfoTokenServices(googleResource().getUserInfoUri(), google().getClientId()); | |
googleTokenServices.setRestTemplate(googleTemplate); | |
googleTokenServices.setPrincipalExtractor(googlePrincipalExtractor); | |
googleTokenServices.setAuthoritiesExtractor(googleAuthoritiesExtractor); | |
googleFilter.setTokenServices(googleTokenServices); | |
googleFilter.setAuthenticationSuccessHandler(successHandler); | |
filters.add(googleFilter); | |
// | |
filter.setFilters(filters); | |
return filter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear Brandon Vulaj,
Thanks for your tutorial, how do i use ssoFilter?