Java Spring Boot (Part 6)

OAuth2 Login

Spring Framework logo by Pivotal Software is licensed under Apache License 2.0

OAuth2 Login

OAuth2 authentication using Spring Security and GitHub.

I used spring initializr to create a new project with a dependency on Spring Web and OAuth2 Client

I made a small change to the SpringOauth2Application.java file (see above) and ran the project.
The project generated a temporary password

I accessed the running code on http://localhost:8080 and I was redirected to a login page

I entered the username "user" and the generated a temporary password and the home page was displayed

GitHub

To switch to using GitHub as an OAuth2 authentication service I would need a github.client-id and a github.client-secret.

I logged into my GitHub account and clicked on the Settings menu item

I clicked on the Developer settings menu item

I selected the existing localhost OAuth application (if it had not already existed I would have created it)

I updated the Authorization callback URL and took a note of the Client ID and a newly generated Client Secret

I added the github.client-id and a github.client-secret values to my application.properties file

Now when I tried to access http://localhost:8080 I was redirected to a GitHub page

I provided the Authentication code from my two-factor authentication app

I was redirected back to the http://localhost:8080 home page

Adding Views and Roles

I added an admin, user and index view.
I added a USER role and an ADMIN role

I added a Thymeleaf dependency

I created a SecurityFilterChain Bean to control access

SecurityConfig.java
WebConfig.java

I created a service that assigns security Roles to users (user with GitHub ID=15018162 is the only ADMIN)

CustomOAuthUserService.java
CustomOAuth2User.java

REST Controller (not used here because we are generating pages server-side only)

MyRestController.java

ModelAndView Controller

MyPageController.java

index view

index.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>My Application</title> <script th:src="@"></script> <script th:src="@"></script> <link rel="stylesheet" type="text/css" th:href="@" /> <link rel="stylesheet" type="text/css" th:href="@" /> </head> <body> <div th:replace="navbar.html :: navbar(page='index')"></div> <div> <h1> Home Page </h1> </div> </body> </html>

navbar fragment

navbar.html
<nav th:fragment="navbar (page)" class="navbar navbar-expand-md bg-dark navbar-dark"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" th:href="@">My Application</a> </div> <ul class="nav navbar-nav"> <li th:if="$"><a class="nav-link" th:href="@">Login</a></li> <li th:if="$"><a class="active nav-link" th:href="@">User</a></li> <li th:if="$"><a class="nav-link" th:href="@">User</a></li> <li th:if="$"><a class="active nav-link" th:href="@">Admin</a></li> <li th:if="$"><a class="nav-link" th:href="@">Admin</a></li> <li th:if="$"> <img th:src="$" style="height: 40px; width: 40px;" class="avatar circle"> </li> </ul> </div> </nav>
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.5</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.haddley</groupId>
	<artifactId>spring-oauth2</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-oauth2</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-oauth2-client</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>


		<dependency>
    		<groupId>org.webjars</groupId>
    		<artifactId>bootstrap</artifactId>
    		<version>5.3.2</version>
		</dependency>
		<dependency>
    		<groupId>org.webjars</groupId>
    		<artifactId>jquery</artifactId>
    		<version>3.1.1</version>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Home view

I clicked on the Login button

I entered an Authentication code

Home view with User Menu Item, Admin Menu Item and User Avatar.

User view

Admin view

user api result

admin api result