Implementing OAuth 2.0 in a Java Spring Boot application for user authentication typically involves using the Spring Security framework. Below is a step-by-step guide with sample code to help you get started. This example uses the Authorization Code Grant flow, which is commonly used for web applications.
- Create a Spring Boot Project: Use Spring Initializer or your preferred method to create a new Spring Boot project with the following dependencies:
- Spring Web
- Spring Security
- Thymeleaf (for simplicity in this example)
- Configure OAuth 2.0 in
application.properties
orapplication.yml
: Add the configuration properties for OAuth 2.0, including the client ID, client secret, and authorization server details:propertiesCopy codespring.security.oauth2.client.registration.myapp.client-id=your-client-id spring.security.oauth2.client.registration.myapp.client-secret=your-client-secret spring.security.oauth2.client.registration.myapp.authorization-uri=https://authorization-server.com/oauth/authorize spring.security.oauth2.client.registration.myapp.token-uri=https://authorization-server.com/oauth/token spring.security.oauth2.client.registration.myapp.redirect-uri=http://localhost:8080/login/oauth2/code/myapp spring.security.oauth2.client.registration.myapp.scope=read_profile spring.security.oauth2.client.provider.myapp.authorization-uri=https://authorization-server.com/oauth/authorize spring.security.oauth2.client.provider.myapp.token-uri=https://authorization-server.com/oauth/token spring.security.oauth2.client.provider.myapp.user-info-uri=https://authorization-server.com/userinfo
Replace the placeholders with your actual client ID, client secret, and authorization server details. - Create a Controller: Create a controller to handle the login and home page. For simplicity, this example uses Thymeleaf templates:javaCopy code
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.oauth2.core.user.OAuth2User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @GetMapping("/") public String home(@AuthenticationPrincipal OAuth2User principal, Model model) { if (principal != null) { model.addAttribute("userName", principal.getAttribute("name")); } return "home"; } }
- Create Thymeleaf Template (
src/main/resources/templates/home.html
): Create a simple Thymeleaf template to display the home page:htmlCopy code<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Spring Boot OAuth2 Example</title> </head> <body> <h1>Welcome, <span th:text="${userName}"></span>!</h1> <a href="/logout">Logout</a> </body> </html>
- Run the Application: Run your Spring Boot application, and visit
http://localhost:8080
. You should be redirected to the authorization server for authentication.
This is a basic example to help you get started. Depending on your requirements and the specific OAuth 2.0 flow you want to use, you may need to customize the configuration and code accordingly. Additionally, consider adding error handling, security, and proper user experience features in a production environment.