官术网_书友最值得收藏!

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.

Read more about OAuth and grant types at this link: http://oauth.net

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:

  1. 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>
  1. 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.
In our example, both, authorization server and resource server, are the same. But in practice, these two will be running.
        @EnableResourceServer
@EnableAuthorizationServer
@SpringBootApplication
public class Application {
  1. 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
  1. 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.

  1. 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.

主站蜘蛛池模板: 油尖旺区| 海淀区| 镇坪县| 台北市| 逊克县| 永城市| 江陵县| 莱阳市| 鄂托克前旗| 札达县| 比如县| 张家川| 汤原县| 九寨沟县| 高雄市| 定边县| 禹城市| 醴陵市| 武冈市| 河津市| 抚宁县| 西青区| 防城港市| 全南县| 沙河市| 浏阳市| 尚义县| 伊川县| 武平县| 灵台县| 金坛市| 樟树市| 小金县| 黎城县| 凤庆县| 博湖县| 闸北区| 上高县| 襄樊市| 广东省| 平罗县|