Spring Security - How to configure an OAuth2 client

You can view the GitHub repository here: spring-security-oauth2-client-example.

This article is under construction

pom.xml

[FETCH]https://raw.githubusercontent.com/TwiN/spring-security-oauth2-client-example/master/pom.xml[/FETCH]

application.properties

To get your own client id and client secret, you’ll need to get one from the provider’s website. For instance, you can create one for Google using Google’s developer console.

[FETCH]https://raw.githubusercontent.com/TwiN/spring-security-oauth2-client-example/master/minimal-sample/src/main/resources/application.properties[/FETCH]

Note that for Google, you only need the two following properties:

spring.security.oauth2.client.registration.google.client-id=
spring.security.oauth2.client.registration.google.client-secret=

but I decided to include all properties for the sake of this example.

Security Configuration

[FETCH]https://raw.githubusercontent.com/TwiN/spring-security-oauth2-client-example/master/minimal-sample/src/main/java/org/twinnation/springsecurityoauth2clientexample/config/SecurityConfiguration.java[/FETCH]

Controller

[FETCH]https://raw.githubusercontent.com/TwiN/spring-security-oauth2-client-example/master/minimal-sample/src/main/java/org/twinnation/springsecurityoauth2clientexample/controller/ExampleController.java[/FETCH]

Ugh, parsing Java with HTML code is a pain, there might be some parsing errors. Go here to get the correct source

And obviously, we need the main class

Application

[FETCH]https://raw.githubusercontent.com/TwiN/spring-security-oauth2-client-example/master/minimal-sample/src/main/java/org/twinnation/springsecurityoauth2clientexample/SpringSecurityOAuth2ClientExampleApplication.java[/FETCH]