Code Monkey home page Code Monkey logo

spring-security-oauth2-test's Introduction

spring-security-oauth2-test

This library is helpful for integration test based on spring security, especially oauth2 for resource server, works with MockMvc.

It enhanced spring-security-test by mock an OAuth2 client or on behalf of user.

Attach Map-based claims to mocked user as authentication details, the claims can be extracted from bearer jwt token.

Note: Most code came from the open network. I refactor and enhanced the code, then we have this java-library.

Features

  • @WithMockOAuth2Client
  • @WithMockOAuth2User
    • mock an oauth2 user, attach claims to OAuth2Authentication details
  • @AttachClaims
    • attach Map-based claims to current authentication, should work with @WithMockUser
  • @WithMockUserAndClaims
    • enhanced @WithMockUser, attach Map-based claims as authentication details
    • equal to @WithMockUser + @AttachClaims
  • @WithToken
    • add bearer token to request header to extract a PreAuthenticatedAuthenticationToken, load existing OAuth2Authentication from SecurityContext
    • require @MockTokenServices on test class
  • @ResourcesNonStateless
    • allow non token-based authentication to access oauth2 resources

How to use

Step 1. Add the JitPack repository to your build file

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Step 2. Add the dependency

dependencies {
    implementation 'com.github.ahunigel:spring-security-oauth2-test:{version}'
}

Refer to https://jitpack.io/#ahunigel/spring-security-oauth2-test for details.

Step 3. Write tests

@WithMockOAuth2User(
    client = @WithMockOAuth2Client(
      clientId = "custom-client",
      scope = {"custom-scope", "other-scope"},
      authorities = {"custom-authority", "ROLE_CUSTOM_CLIENT"}),
    user = @WithMockUser(
      username = "custom-username",
      authorities = {"custom-user-authority"}),
    claims = @AttachClaims({
      @Claim(name = "user_id", value = "6", type = Long.class),
      @Claim(name = "role_id", value = "1"),
      @Claim(name = "is_social_user", value = "false")
    })
}

or

@AttachClaims(value = {
    @Claim(name = "user_id", value = "6", type = Long.class),
    @Claim(name = "role_id", value = "1"),
    @Claim(name = "is_social_user", value = "false")},
    claims = {"email:[email protected]", "user_name=ahunigel"}
)
@WithMockUser()

or

@WithMockUserAndClaims(
    @AttachClaims(value = {
        @Claim(name = "user_id", value = "6", type = Long.class),
        @Claim(name = "role_id", value = "1"),
        @Claim(name = "is_social_user", value = "false")},
        claims = {"email:[email protected]", "user_name=ahunigel"}
    )
)

References

See Also

TODOs

  • Attach claims for @WithMockOAuth2Client/@WithMockOAuth2User via @AttachClaims
  • Migrate Spring Security OAuth 2.x application to Spring Security 5.2
  • Add support for RestTemplate
  • Add unit test

spring-security-oauth2-test's People

Contributors

ahunigel avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

spring-security-oauth2-test's Issues

Project dependencies do not parse correctly in Maven

The project cannot be used from Maven, because the converted build.gradle looks like this:

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>2.5.+</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>2020.0.+</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

and 2.5.+, 2020.0.+, are not valid artifact versions in Maven

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.