{"id":42664477,"url":"https://github.com/ahunigel/spring-security-oauth2-test","last_synced_at":"2026-01-29T10:01:06.914Z","repository":{"id":47024763,"uuid":"143366264","full_name":"ahunigel/spring-security-oauth2-test","owner":"ahunigel","description":"spring-security-oauth2-test","archived":false,"fork":false,"pushed_at":"2025-05-20T01:05:44.000Z","size":395,"stargazers_count":6,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-20T02:23:26.527Z","etag":null,"topics":["integration-test","integration-testing","spring-security","spring-security-oauth2","spring-security-test"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ahunigel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-08-03T02:16:21.000Z","updated_at":"2025-05-20T01:05:42.000Z","dependencies_parsed_at":"2023-11-28T03:31:00.955Z","dependency_job_id":"f6190ec6-1aba-48ab-970c-56d5f7f370c5","html_url":"https://github.com/ahunigel/spring-security-oauth2-test","commit_stats":null,"previous_names":[],"tags_count":61,"template":false,"template_full_name":null,"purl":"pkg:github/ahunigel/spring-security-oauth2-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahunigel%2Fspring-security-oauth2-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahunigel%2Fspring-security-oauth2-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahunigel%2Fspring-security-oauth2-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahunigel%2Fspring-security-oauth2-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ahunigel","download_url":"https://codeload.github.com/ahunigel/spring-security-oauth2-test/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ahunigel%2Fspring-security-oauth2-test/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28875445,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-29T09:47:23.353Z","status":"ssl_error","status_checked_at":"2026-01-29T09:47:19.357Z","response_time":59,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["integration-test","integration-testing","spring-security","spring-security-oauth2","spring-security-test"],"created_at":"2026-01-29T10:01:06.228Z","updated_at":"2026-01-29T10:01:06.908Z","avatar_url":"https://github.com/ahunigel.png","language":"Java","readme":"# spring-security-oauth2-test\n[![](https://jitpack.io/v/ahunigel/spring-security-oauth2-test.svg)](https://jitpack.io/#ahunigel/spring-security-oauth2-test)\n\nThis library is helpful for integration test based on spring security, especially oauth2 for resource server, works \nwith `MockMvc`.\n\nIt enhanced spring-security-test by mock an OAuth2 client or on behalf of user.\n\nAttach Map-based claims to mocked user as authentication details, the claims can be extracted from bearer jwt token.\n\n_Note: Most code came from the open network. I refactor and enhanced the code, then we have this java-library._\n\n## Features\n- `@WithMockOAuth2Client`\n- `@WithMockOAuth2User`\n    - mock an oauth2 user, attach claims to OAuth2Authentication details\n- `@AttachClaims`\n    - attach Map-based claims to current authentication, should work with `@WithMockUser`\n- `@WithMockUserAndClaims`\n    - enhanced `@WithMockUser`, attach Map-based claims as authentication details\n    - equal to `@WithMockUser` + `@AttachClaims`\n- `@WithToken`\n    - add `bearer` token to request header to extract a `PreAuthenticatedAuthenticationToken`,\n    load existing OAuth2Authentication from SecurityContext\n    - require `@MockTokenServices` on test class\n- `@ResourcesNonStateless`\n    - allow non token-based authentication to access oauth2 resources\n\n## How to use\n\n### Step 1. Add the JitPack repository to your build file\n```groovy\nallprojects {\n    repositories {\n        ...\n        maven { url 'https://jitpack.io' }\n    }\n}\n```\n## Step 2. Add the dependency\n```groovy\ndependencies {\n    implementation 'com.github.ahunigel:spring-security-oauth2-test:{version}'\n}\n```\n_Refer to https://jitpack.io/#ahunigel/spring-security-oauth2-test for details._\n\n## Step 3. Write tests\n```java\n@WithMockOAuth2User(\n    client = @WithMockOAuth2Client(\n      clientId = \"custom-client\",\n      scope = {\"custom-scope\", \"other-scope\"},\n      authorities = {\"custom-authority\", \"ROLE_CUSTOM_CLIENT\"}),\n    user = @WithMockUser(\n      username = \"custom-username\",\n      authorities = {\"custom-user-authority\"}),\n    claims = @AttachClaims({\n      @Claim(name = \"user_id\", value = \"6\", type = Long.class),\n      @Claim(name = \"role_id\", value = \"1\"),\n      @Claim(name = \"is_social_user\", value = \"false\")\n    })\n)\n```\nor\n```java\n@AttachClaims(value = {\n    @Claim(name = \"user_id\", value = \"6\", type = Long.class),\n    @Claim(name = \"role_id\", value = \"1\"),\n    @Claim(name = \"is_social_user\", value = \"false\")},\n    claims = {\"email:ahunigel@gmail.com\", \"user_name=ahunigel\"}\n)\n@WithMockUser()\n```\nor\n```java\n@WithMockUserAndClaims(\n    @AttachClaims(value = {\n        @Claim(name = \"user_id\", value = \"6\", type = Long.class),\n        @Claim(name = \"role_id\", value = \"1\"),\n        @Claim(name = \"is_social_user\", value = \"false\")},\n        claims = {\"email:ahunigel@gmail.com\", \"user_name=ahunigel\"}\n    )\n)\n```\n\n## References\n- [How to get @WebMvcTest work with OAuth?](https://stackoverflow.com/questions/48540152/how-to-get-webmvctest-work-with-oauth)\n- [Faking OAuth2 Single Sign-on in Spring](http://engineering.pivotal.io/post/faking_oauth_sso/)\n- [@WithSecurityContext](https://docs.spring.io/spring-security/site/docs/current/reference/html/test-method.html#test-method-withsecuritycontext)\n- [Spring MVC Test Integration](https://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html)\n- [OAuth2 Autoconfig](https://docs.spring.io/spring-security-oauth2-boot/docs/current/reference/htmlsingle/)\n- [Retrieve User Information in Spring Security](https://www.baeldung.com/get-user-in-spring-security)\n- [Spring Security OAuth](https://projects.spring.io/spring-security-oauth/docs/Home.html)\n- [Use @WithMockUser with @SpringBootTest inside an oAuth2 Resource Server Application](https://stackoverflow.com/questions/41824885/use-withmockuser-with-springboottest-inside-an-oauth2-resource-server-applic)\n- [Spring Cloud Supported Versions](https://github.com/spring-cloud/spring-cloud-release/wiki/Supported-Versions)\n\n## See Also\n- [springkit-bom](https://github.com/ahunigel/springkit-bom)\n- [test-toolkit](https://github.com/ahunigel/test-toolkit)\n- [spring-test-toolkit](https://github.com/ahunigel/spring-test-toolkit)\n- [spring-security-oauth2-test](https://github.com/ahunigel/spring-security-oauth2-test)\n- [spring-toolkit](https://github.com/ahunigel/spring-toolkit)\n\n## TODOs\n\n- Attach claims for @WithMockOAuth2Client/@WithMockOAuth2User via @AttachClaims \n- Migrate Spring Security OAuth 2.x application to Spring Security 5.2\n- Add support for `RestTemplate`\n- Add unit test\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahunigel%2Fspring-security-oauth2-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahunigel%2Fspring-security-oauth2-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahunigel%2Fspring-security-oauth2-test/lists"}