Skip to content

Add sample or instructions for Google OAuth #6

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions github/src/main/java/com/example/SocialApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,33 @@ ClientResources facebook() {
return new ClientResources();
}

@Bean
@ConfigurationProperties("google")
ClientResources google() {
return new ClientResources();
}

private Filter ssoFilter() {
CompositeFilter filter = new CompositeFilter();
List<Filter> filters = new ArrayList<>();
filters.add(ssoFilter(facebook(), "/login/facebook"));
filters.add(ssoFilter(github(), "/login/github"));
filters.add(ssoFilter(google(), "/login/google"));
filter.setFilters(filters);
return filter;
}

private Filter ssoFilter(ClientResources client, String path) {
OAuth2ClientAuthenticationProcessingFilter facebookFilter = new OAuth2ClientAuthenticationProcessingFilter(
OAuth2ClientAuthenticationProcessingFilter oAuth2Filter = new OAuth2ClientAuthenticationProcessingFilter(
path);
OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(client.getClient(),
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client.getClient(),
oauth2ClientContext);
facebookFilter.setRestTemplate(facebookTemplate);
facebookFilter.setTokenServices(new UserInfoTokenServices(
client.getResource().getUserInfoUri(), client.getClient().getClientId()));
return facebookFilter;
oAuth2Filter.setRestTemplate(oAuth2RestTemplate);
UserInfoTokenServices userInfoTokenServices = new UserInfoTokenServices(
client.getResource().getUserInfoUri(), client.getClient().getClientId());
userInfoTokenServices.setRestTemplate(oAuth2RestTemplate);
oAuth2Filter.setTokenServices(userInfoTokenServices);
return oAuth2Filter;
}

private Filter csrfHeaderFilter() {
Expand Down
13 changes: 13 additions & 0 deletions github/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ github:
resource:
userInfoUri: https://api.github.com/user

google:
client:
clientId: 1071160065194-3556m2cmnlacgqo9qfjb2089f0aetb31.apps.googleusercontent.com
clientSecret: Hmw1APyVk7tLVcTTONh3pOhm
accessTokenUri: https://www.googleapis.com/oauth2/v3/token
userAuthorizationUri: https://accounts.google.com/o/oauth2/auth
authenticationScheme: query
scope:
- email
- profile
resource:
userInfoUri: https://www.googleapis.com/oauth2/v2/userinfo

logging:
level:
org.springframework.security: DEBUG
Expand Down
3 changes: 3 additions & 0 deletions github/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ <h1>Login</h1>
<div>
With Github: <a href="/login/github">click here</a>
</div>
<div>
With Google: <a href="/login/google">click here</a>
</div>
</div>
<div class="container" ng-show="home.authenticated">
Logged in as: <span ng-bind="home.user"></span>
Expand Down