Git Product home page Git Product logo

spring-boot-security-oauth2-minimal's Introduction

OAuth2 with JWT

Introduction

I've created this project to understand how to deal with clients accessing protected resource servers using a central authorization server. Apart from general OAuth 2.0 and JWT theory, I wanted to study how the following components interoperate:

  • Spring Boot 1.3.0.RELEASE (with Spring 4.2.3.RELEASE)
  • Spring Security 4.0.3.RELEASE
  • Spring Security OAuth2 2.0.8.RELEASE
  • Spring Security JWT 1.0.3.RELEASE

Spring Boot can be pretty magical, especially with regard to auto-configuration. While I'm a fan of convention-over-configuration development (I've used Grails for years), that doesn't mean it always makes sense. At least not at first, to newcomers like me.

I found that some of the documentation and examples tend to gloss over "minor details" which makes it hard to grasp why you need to do something, or when the framework takes care of it for you. It took me quite a few hours of trial-and-error, tracing execution paths, reading auto-configuration classes, failing with out-of-date examples, etc.

References:

Running the example

You can import each application in IntelliJ IDEA and run them from there using the Gradle bootRun task. Alternatively, you can open three terminal sessions (or command prompts) and run gradle bootRun from each folder.

Note: You need to start the authorization server first, because the resource server contacts it during startup to obtain the public key used to verify JWT signatures.

Open a browser to http://localhost:8080/client/. The client uses a RestTemplate to access a protected resource (running on http://localhost:8082/api/) and discovers it is not authorized. It then redirects automatically to the /oauth/authorize endpoint to start an authorization code flow. In the process it authenticates itself as a 'confidential' client.

The authorization server redirects to its login page. You can use one of the following username/password combinations to login:

  • user:password (has USER role)
  • admin:password (has ADMIN and USER role)

After a successful login you are redirected to the /oauth/confirm_access endpoint where you need to approve all the grants requested by the client.

The authorization server now redirects back to the client application on a pre-approved URI, with an authorization code.

The client then accesses the authorization server on the /oauth/token endpoint to exchange the authorization code with an access token (including a refresh token).

This token is actually a JSON Web Token (JWT). If you want to see what's in it, visit jwt.io and paste it in the Encoded section. You can find the token in the JSON outputted by the client as details.tokenValue.

The client now retries the request to the resource server. The resource server accepts the JWT token and checks the signature using the authorization server's public key. There's no communication necessary between the resource server and the authorization server; that is one of the nice things about JWT. The JWT token also describes the user's roles, which are checked against the authorization requirements of the resource.

The client receives the resource (a JSON representation of the user principal) and dumps it to the browser (where you can also see the JSON Web Token).

spring-boot-security-oauth2-minimal's People

Contributors

dynamind avatar galcyurio avatar lmiguelmh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spring-boot-security-oauth2-minimal's Issues

License?

Hello! First of all thanks for sharing your job.

I'd like to use your project as starting point for an academic project that will be licensed under MIT License.

My concern is that your code doesn't define a license, so I'm assuming that's copyrighted.

Could you enlighten that, please?

Thanks in advance

No service of type StyledTextOutputFactory available in ProjectScopeServices.

It looks like the spring boot gradle plugin 1.3.0.RELEASE is not compatible with Gradle 3 as it is stated in this issue spring-gradle-plugins/dependency-management-plugin#87.

gradle tasks --stacktrace

...
Caused by: org.gradle.internal.service.UnknownServiceException: No service of type StyledTextOutputFactory available in ProjectScopeServices.
        at org.gradle.internal.service.DefaultServiceRegistry.getServiceProvider(DefaultServiceRegistry.java:436)
        ...

The easy fix is to use 1.4.0.RELEASE.

Spring Boot 1.5 support

First off, thanks for this great minimal sample, I only wish I'd found it sooner! There are so many samples out there that use Spring's OAuth support in a slightly different way, but this one is nice and concise.

I wanted to try it out with a more recent dependency set (spring-boot-starter-security:1.5.10.RELEASE, spring-security-oauth:2.0.14.RELEASE and spring-security-jwt:1.0.9.RELEASE) and ran into a little snag which took me a little while to figure out.

It seems the default order of the OAuth2 resource filter has changed in Spring Boot 1.5, which effectively means that the OAuth2AuthenticationProcessingFilter will not end up being invoked, so the JWT will not be checked. Subsequently this results in a 403 error being returned by the resource server in this sample (see spring-attic/spring-security-oauth#993 for more info).

This can be easily fixed by changing back to the old filter order:

security.oauth2.resource.filter-order=3

Just putting this here in case anyone comes across the same issue.

Is authorization server must be STATE?

Hi,Firstly thanks for your demo code which let me understand OAuth 2.0 clearly.But what makes confused is whether the authorization should be a STATE app.

I have a STATELESS web application(Actually It is generated by Jhipster).The config code is here.The app is already configured as a OAuth Server,and the code is here.

When I used a Client to get the access token,It doesn't to redirect to the login page.It just to appear dialog from chrome to input the username and password.If the username and password is matched, the Client can call the resource server's api.

What I want to achieve is chrome can redirect to he authorization server login page when I use a client to get a access token .Is it can achieved?

Thanks for you code ,and thanks for your reading my poor English question.If it is convenient,you can contact me by email:[email protected]

Failed to send request to Resource server with JWT Token

I'm trying to use a trusted client example with Password Grant Type and i was able to receive the JWT Token.

Question now is how should use the JWT token to send request to Resource App.

I had tried to use details.tokenValue as Bearer token but some such

Resource Request, highlighted is the JWT Token
image

In OAuthApp

                            // Trusted client: similar to confidential client but also allowed to handle user password
                    .withClient("trusted").secret("secret")
                    .authorities("ROLE_TRUSTED_CLIENT")
                    .authorizedGrantTypes("client_credentials", "password", "authorization_code", "refresh_token")
                    .scopes("read", "write")
                    .redirectUris("http://localhost:8080/client/")

In Client app

config:
    oauth2:
        clientID: trusted --default is confidential 
        clientSecret: secret

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.