Coder Social home page Coder Social logo

bingqiao / spring-webflux-security-demo Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 3.0 75 KB

Demo app for Spring Security and WebClient

Home Page: https://medium.com/@bingqiao/spring-security-webflux-reactive-or-servlet-ced7af343778

License: MIT License

Java 100.00%
spring-webflux jwt spring-boot oauth2 resource-server spring-boot-netty reactive servlet example

spring-webflux-security-demo's Introduction

Spring Webflux Security Demo

Four projects are included here to demo how to implement some typical use cases of OAuth2 via
Spring Security 5 for both Reactive and Servlet stacks.

A legacy Spring Boot service using Spring Security OAuth2 has the following features:

  • Acting as a resource server protected by JWT Bearer token issued by authorization server A
  • Holding a client_credentials OAuth2RestTemplate to access external resource servers
    protected by JWT Bearer token issued by authorization server B
  • Extension to DefaultAccessTokenConverter to add custom authorities to authentication object
  • Method security to allow use of method access-control annotations such as PreAuthorize
  • Parsing hateoas responses

It wasn't an easy path migrating this service to Spring Security 5. But after a lot of searching,
experimenting, and frustrating, I now have two sets of projects that work for all above.

The major mistake I made was to mix Servlet and Reactive stacks in Spring. Spring WebClient
can be used in both stacks but which stack to use has implications on what and how to configure
your Spring Beans.

The two servlet projects are as follows:

  • resourceserver-tomcat
  • resourceserver-webclient-tomcat

The two reactive projects are as follows:

  • resourceserver
  • resourceserver-webclient

The matrix below shows differences setting up those two sets of projects.

Reactive Servlet
Dependencies Exclude spring-boot-starter-tomcat from spring-boot-starter-web
Security Configuration
  • Apply @EnableWebFluxSecurity
  • Configure @Bean SecurityWebFilterChain that takes ServerHttpSecurity
    • Apply @Configuration
    • @Override WebSecurityConfigurerAdapter.configure
    Method Security Apply @EnableReactiveMethodSecurity Apply @EnableGlobalMethodSecurity
    WebClient
    • Configure @Bean WebClient
    • Configurate @Bean ReactiveOAuth2AuthorizedClientManager that
      uses injected ReactiveClientRegistrationRepository and ReactiveOAuth2AuthorizedClientService
    • Configure @Bean WebClient
    • Configure @Bean ReactiveOAuth2AuthorizedClientManager
      that instantiates InMemoryReactiveClientRegistrationRepository
      and InMemoryReactiveOAuth2AuthorizedClientService

    resourceserver

    This project only implements Resource Server protected by JWT. The following is the how
    JWT issuer can be configured in application.yml.

    spring:
      security:
        oauth2:
          resourceserver:
            jwt:
              issuer-uri: ${AUTH_SERVER_B}
    

    resourceserver-webclient

    This project implements Resource Server, client_credentials WebClient, Method Security,
    custom JwtGrantedAuthoritiesConverter and JwtBearerTokenAuthenticationConverter.

    The following is how both site authentication (as Resource Server) and OAuth2 client can be configured in application.yml.

    spring:
      security:
        oauth2:
          resourceserver:
            jwt:
              # the following is for site authentication
              issuer-uri: ${AUTH_SERVER_A}
          # the following is for webclient. the authorization server is the same
          # as the one configured for external resourceserver
          client:
            registration:
              custom:
                authorization-grant-type: client_credentials
                client-id: ${CLIENT_ID_FROM_AUTH_SERVER_B}
                client-secret: ${CLIENT_SECRET_FROM_AUTH_SERVER_B}
                scope: demo-external
                provider: customprovider
            provider:
              customprovider:
                token-uri: ${TOKEN_URI_OF_AUTH_SERVER_FOR_B}
    

    spring-webflux-security-demo's People

    Contributors

    bingqiao avatar

    Stargazers

     avatar  avatar  avatar  avatar  avatar  avatar

    Watchers

     avatar  avatar  avatar

    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.