Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple Oauth2 Providers in Spring Security
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
@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");
// Facebook
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);
// Google
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
You can’t perform that action at this time.