The following steps will guide you to configure an Authorization Server and a Resource Server using Spring Security OAuth2, which prevents you from having to write an OAuth 2.0 Provider from scratch (which would be very unproductive and prone to security failures):
Create the initial project using Spring Initializr as we did for the other recipes in this book. Go to https://start.spring.io/ and define the following data:
Set up the Group as com.packt.example
Define the Artifact as auth-code-server
Add Web and Security as dependencies for this project
After creating the auth-code-server project, import it to your IDE. If you are using Eclipse, import it as a Maven project.
Open the pom.xml file and add the following dependency, as we will use the Spring Security OAuth2 project (to use an up-to-date Spring Security OAuth2 version, we have to override the version provided by Spring Boot):
Open the application.properties file and add the following content to configure the user of the auth-code-server application (you can use a different user of course, but remember to change it whenever appropriate):
As we want to protect the user's resources through OAuth 2.0, we need to create something to be protected. To do so, create the UserController.java and UserProfile.javaclasses within the com.packt.example.authcodeserver.apipackage.
Open the UserProfile.javaclass and make sure to add the following attributes (do not forget to create appropriate getters and setters for each attribute):
public class UserProfile { private String name; private String email; // getters and setters hidden for brevity }
Open the UserController.javaclass and add the @Controller annotation at the head of the class declaration as follows:
As you might notice, Spring provides us some annotations such as @Controller, @Service, and @Component. Some annotations such as @Service and @Component just defines a declared class as a Spring managed bean (to be managed by Spring which allows for dependency injection mechanism). The @Controller annotation is a specialization of @Component annotation adding semantics for a web controller that can map endpoints to Java source code.
@Controller public class UserController { }
Now, let's add the respective method that will provide the endpoint which will be protected by OAuth 2.0, as presented in the following code (import the User class from package org.springframework.security.core.userdetails):
@RequestMapping("/api/profile") public ResponseEntity<UserProfile> profile() { User user = (User) SecurityContextHolder.getContext() .getAuthentication().getPrincipal(); String email = user.getUsername() + "@mailinator.com";
UserProfile profile = new UserProfile(); profile.setName(user.getUsername()); profile.setEmail(email);
return ResponseEntity.ok(profile); }
Once we have the endpoint to be OAuth 2.0 protected, let's create the OAuth 2.0 Authorization Server configuration by creating the OAuth2AuthorizationServerclass within the com.packt.example.authcodeserver.configpackage.
Add the following annotations to OAuth2AuthorizationServer class and extend the AuthorizationServerConfigurerAdapter class which comes from the Spring Security OAuth2 project:
At the moment, the application is ready to start issuing access tokens, given the user grants permission. But to be allowed to access the user's resources (the Resource Owner profile for this recipe), we need to create the Resource Server's configuration by declaring the OAuth2ResourceServerclass within the same package as OAuth2AuthorizationServer.
Then add the following annotations at the class level for OAuth2ResourceServer as follows: