Spring Security - Github Login
Let us create an example Spring Security OAuth 2.0 application
Register Client App
You need to register your application with a OAuth 2.0 provider before you use OAuth 2.0 in your application.
Login to github.com, then go to Settings > Developer settings. Press New Auth APP to add a new OAuth App
Register a new OAuth application. Set Authorization callback URL to be http://localhost:8080/login/oauth2/code/github
Authorization callback URL is actually the redirect URL. This redirect URL is used for sending authorization code from Authorization Server to the Client. By default, Spring Boot configures this redirect URI as /login/oauth2/code/{registrationId}. Some examples are
- http://localhost:8080/login/oauth2/code/google
- http://localhost:8080/login/oauth2/code/facebook
- http://localhost:8080/login/oauth2/code/github
After registration, you will get a client ID and client secret for the client app.
Maven Dependency
Add oauth2-client maven dependency to Spring Boot Application
1 | <dependency> |
application.yml
Add client registration for Github. Spring Security will configure a ClientRegistration for us.
application.yml
1 | spring: |
You can specify scopes for Github Client App. This page shows all the available scopes you can use: https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/
Security configuration
1 |
|
SecurityConfiguration class customize OAuth 2.0 login. It sets the loginPage(“/login.html”).
login.html
This is the login page.
static/login.html
1 |
|
Here the login URL is /oauth2/authorization/github. This is the Authorization Endpoint. It can be customized in OAuth2 configuration.
Controller
Controller that displays user info
1 |
|
Three endpoints are created here.
- “/“ prints principal name. Here you need
@AuthenticationPrincipal
annotation to get OAuth2User from Authenticaiton. - “/getAuthenticaiont” prints the whole Authentication Object.
- “/getEmails” demonstrate how to get additional resource by attaching access token to the Http Request.
Github API provides an endpoint(https://api.github.com/user/emails) to get user’s emails. You need to have user:email scope to access the email information. see https://developer.github.com/v3/users/emails/#list-email-addresses-for-the-authenticated-user for more information on this Github API endpoint
Test
Open http://localhost:8080 and login with Github.
You will receive index page that displays user name. open http://localhost:8080/getEmails to get defailed user emails from Github.
Source Code for Github OAuth 2.0 login: https://github.com/xinghua24/SpringBootExamples/tree/master/OAuth2Github
Source Code for Google OAuth 2.0 login: https://github.com/xinghua24/SpringBootExamples/tree/master/OAuth2Google
Google OAuth 2.0 login is very similar to Github OAuth 2.0 login. The biggest difference is you register the OAuth 2.0 Client App at Google API Console instead.