- Spring 5.0 Microservices(Second Edition)
- Rajesh R V
- 437字
- 2021-07-02 19:45:05
Securing microservice with OAuth2
In this section, we will see the basic Spring Boot configuration for OAuth2. When a client application requires access to a protected resource, the client sends a request to an authorization server. The authorization server validates the request, and provides an access token. This access token will be validated for every client-to-server request. The request and response sent back and forth depends on the grant type.
The resource owner password credentials grant approach will be used in the following example:

In this case, as shown in the preceding diagram, the resource owner provides the client with a username and password. The client then sends a token request to the authorization server by providing the credentials. The authorization server authorizes the client, and returns an access token. On every subsequent request, the server validates the client token.
To implement OAuth2 in our example, follow these steps:
- As the first step, update pom.xml with oauth2 dependency as follows:
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
</dependency>
<!-- below dependency is explicitly required when
testing OAuth2 with Spring Boot 2.0.0.M1 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
- Next, add two new annotations-- @EnableAuthorizationServer --and @EnableResourceServer to Application.java. The @EnableAuthorizationServer annotation creates an authorization server with an in-memory repository to store client tokens and to provide clients with a username, password, client ID, and secret. @EnableResourceServer is used to access the tokens. This enables a spring security filter that authenticates via an incoming OAuth2 token.
@EnableResourceServer
@EnableAuthorizationServer
@SpringBootApplication
public class Application {
- Add the following properties to the application.properties file:
security.user.name=guest
security.user.password=guest123
security.oauth2.client.client-id: trustedclient
security.oauth2.client.client-secret: trustedclient123
security.oauth2.client.authorized-grant-types:
authorization_code,refresh_token,password
- Add another test case to test OAuth2 as follows:
@Test
public void testOAuthService() {
ResourceOwnerPasswordResourceDetails resource =
new ResourceOwnerPasswordResourceDetails();
resource.setUsername("guest");
resource.setPassword("guest123");
resource.setAccessTokenUri("http://localhost:8080/oauth
/token");
resource.setClientId("trustedclient");
resource.setClientSecret("trustedclient123");
resource.setGrantType("password");
resource.setScope(Arrays.asList(new String[]
{"read","write","trust"}));
DefaultOAuth2ClientContext clientContext =
new DefaultOAuth2ClientContext();
OAuth2RestTemplate restTemplate =
new OAuth2RestTemplate(resource, clientContext);
Greet greet = restTemplate
.getForObject("http://localhost:8080", Greet.class);
Assert.assertEquals("Hello World!", greet.getMessage());
}
As shown in the preceding code, a special rest template, OAuth2RestTemplate, is created by passing the resource details encapsulated in a resource details object. This rest template handles the OAuth2 processes underneath. The access token URI is the endpoint for the token access.
- Rerun the application using maven install. The first two test cases will fail, and the new one will succeed. This is because the server accepts only OAuth2-enabled requests.
These are quick configurations provided by Spring Boot out of the box, but are not good enough to be production grade. We may need to customize ResourceServerConfigurer and AuthorizationServerConfigurer to make them production ready. Regardless, the approach remains the same.
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- 工程軟件開發(fā)技術(shù)基礎(chǔ)
- JavaScript 從入門到項(xiàng)目實(shí)踐(超值版)
- Three.js開發(fā)指南:基于WebGL和HTML5在網(wǎng)頁上渲染3D圖形和動(dòng)畫(原書第3版)
- Kinect for Windows SDK Programming Guide
- 用Python實(shí)現(xiàn)深度學(xué)習(xí)框架
- Linux操作系統(tǒng)基礎(chǔ)案例教程
- Visualforce Developer’s guide
- Spring+Spring MVC+MyBatis從零開始學(xué)
- IDA Pro權(quán)威指南(第2版)
- 零基礎(chǔ)學(xué)Python編程(少兒趣味版)
- IoT Projects with Bluetooth Low Energy
- Python網(wǎng)絡(luò)爬蟲實(shí)例教程(視頻講解版)
- 實(shí)戰(zhàn)Python網(wǎng)絡(luò)爬蟲
- Python計(jì)算機(jī)視覺與深度學(xué)習(xí)實(shí)戰(zhàn)