|
1 | 1 | # spring-security-oauth2-test |
2 | | -spring-security-oauth2-test |
| 2 | +[](https://jitpack.io/#ahunigel/spring-security-oauth2-test) |
| 3 | + |
| 4 | +This library is helpful for integration test based on spring security, especially oauth2 for resource server, works |
| 5 | +with `MockMvc`. |
| 6 | + |
| 7 | +It enhanced spring-security-test by mock an OAuth2 client or on behalf of user. |
| 8 | + |
| 9 | +Attach Map-based claims to mocked user as authentication details, the claims can be extracted from bearer jwt token. |
| 10 | + |
| 11 | +_Note: Most code came from the open network. I refactor and enhanced the code, then we have this java-library._ |
| 12 | + |
| 13 | +## Features |
| 14 | +- @WithMockOAuth2Client |
| 15 | +- @WithMockOAuth2User |
| 16 | + - mock an oauth2 user, attach claims to OAuth2Authentication details |
| 17 | +- @AttachClaims |
| 18 | + - attach Map-based claims to current authentication, should work with @WithMockUser |
| 19 | +- @WithMockUserAndClaims |
| 20 | + - enhanced @WithMockUser, attach Map-based claims as authentication details |
| 21 | + - equal to @WithMockUser + @AttachClaims |
| 22 | + |
| 23 | +## How to use |
| 24 | + |
| 25 | +### Step 1. Add the JitPack repository to your build file |
| 26 | +```groovy |
| 27 | +allprojects { |
| 28 | + repositories { |
| 29 | + ... |
| 30 | + maven { url 'https://jitpack.io' } |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | +## Step 2. Add the dependency |
| 35 | +```groovy |
| 36 | +dependencies { |
| 37 | + implementation 'com.github.ahunigel:spring-security-oauth2-test:master-SNAPSHOT' |
| 38 | +} |
| 39 | +``` |
| 40 | +## Step 3. Write test |
| 41 | +```java |
| 42 | +@WithMockOAuth2User( |
| 43 | + client = @WithMockOAuth2Client( |
| 44 | + clientId = "custom-client", |
| 45 | + scope = {"custom-scope", "other-scope"}, |
| 46 | + authorities = {"custom-authority", "ROLE_CUSTOM_CLIENT"}), |
| 47 | + user = @WithMockUser( |
| 48 | + username = "custom-username", |
| 49 | + authorities = {"custom-user-authority"}), |
| 50 | + claims = { |
| 51 | + @Claim(name = "user_id", value = "6", type = Long.class), |
| 52 | + @Claim(name = "role_id", value = "1"), |
| 53 | + @Claim(name = "is_social_user", value = "false") |
| 54 | + }) |
| 55 | +``` |
| 56 | +or |
| 57 | +```java |
| 58 | +@AttachClaims(value = { |
| 59 | + @Claim(name = "user_id", value = "6", type = Long.class), |
| 60 | + @Claim(name = "role_id", value = "1"), |
| 61 | + @Claim(name = "is_social_user", value = "false") |
| 62 | +}, claims = {ROLE_NAME, "ADMIN"}) |
| 63 | +``` |
| 64 | +Refer to https://jitpack.io/#ahunigel/spring-security-oauth2-test for details. |
| 65 | + |
| 66 | +## TODOs |
| 67 | + |
| 68 | +1. Mock full oauth2 process, add `bearer` token to request header to extract a `PreAuthenticatedAuthenticationToken` |
| 69 | + |
| 70 | +2. For oauth2 request, add ability to set ResourceServerSecurityConfigurer.stateless to false, maybe an |
| 71 | +annotation like `@ResourceStateLess(false)` |
| 72 | + |
| 73 | +3. Add support for `RestTemplate` |
0 commit comments