DEV Community

JL
JL

Posted on

OAuth 2.0 - Resource Server

Generate a spring boot project and add 3 dependencies:

Spring Web WEB
Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.

Spring Boot DevTools DEVELOPER TOOLS
Provides fast application restarts, LiveReload, and configurations for enhanced development experience.

OAuth2 Resource Server SECURITY
Spring Boot integration for Spring Security's OAuth2 resource server features.

Image description

Import the Maven project into Spring Tool Suite (I am using v4 based on Eclipse). The pom.xml has a dependency which enforces security: meaning all requests to the resource server will need to be authenticated and authorised.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
        </dependency>
Enter fullscreen mode Exit fullscreen mode

E.g.
Create a restful endpoint in the resource server project, and try to access it without auth:

package com.idprovidersanbox.ws.api.ResourceServer.controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UsersController {

    @GetMapping("/status/check")
    public String status() {
        return "Working...";
    }
}

Enter fullscreen mode Exit fullscreen mode

You will get a 401 unauthorised error:
Image description

Top comments (0)