{"id":24522774,"url":"https://github.com/gbzarelli/spring-security-oauth2-resttemplate","last_synced_at":"2025-03-15T13:22:52.490Z","repository":{"id":180990197,"uuid":"608164885","full_name":"gbzarelli/spring-security-oauth2-resttemplate","owner":"gbzarelli","description":"Sample RestTemplate Client with Spring Security Oauth2 with client_credentials flow","archived":false,"fork":false,"pushed_at":"2023-03-01T21:10:28.000Z","size":70,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T03:35:31.796Z","etag":null,"topics":["client-credentials","java","oauth2","spring","spring-boot","spring-boot-security-oauth2"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gbzarelli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-03-01T13:06:17.000Z","updated_at":"2024-07-30T14:35:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"c2bc5a98-9fb0-4660-8f17-05b222206daf","html_url":"https://github.com/gbzarelli/spring-security-oauth2-resttemplate","commit_stats":null,"previous_names":["gbzarelli/spring-security-oauth2-resttemplate"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbzarelli%2Fspring-security-oauth2-resttemplate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbzarelli%2Fspring-security-oauth2-resttemplate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbzarelli%2Fspring-security-oauth2-resttemplate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gbzarelli%2Fspring-security-oauth2-resttemplate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gbzarelli","download_url":"https://codeload.github.com/gbzarelli/spring-security-oauth2-resttemplate/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243733642,"owners_count":20339086,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["client-credentials","java","oauth2","spring","spring-boot","spring-boot-security-oauth2"],"created_at":"2025-01-22T03:35:35.115Z","updated_at":"2025-03-15T13:22:52.472Z","avatar_url":"https://github.com/gbzarelli.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sample RestTemplate Client with Spring Security Oauth2 with client_credentials flow\n\nThis sample works with `Spring Security Oauth2 5` integrated in Spring Boot RestTemplate to make\nclient requests with Oauth2 client credentials flow. The goal is manage request tokens and expirations time.\n\n## Caution!!\n\nThis lib inject the Spring Security context configurations, but, you can remove-it just \nadd the exclusions in `spring-boot-starter-oauth2-client` like that:\n\n```xml \n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-starter-oauth2-client\u003c/artifactId\u003e\n    \u003cexclusions\u003e\n        \u003cexclusion\u003e\n            \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n            \u003cartifactId\u003espring-boot-starter\u003c/artifactId\u003e\n        \u003c/exclusion\u003e\n        \u003cexclusion\u003e\n            \u003cgroupId\u003eorg.springframework.security\u003c/groupId\u003e\n            \u003cartifactId\u003espring-security-config\u003c/artifactId\u003e\n        \u003c/exclusion\u003e\n    \u003c/exclusions\u003e\n\u003c/dependency\u003e\n```\n\nAfter that, just remove the [SecurityConfiguration.java](/src/main/java/br/com/helpdev/security/SecurityConfiguration.java).\n\n## Oh no! I can't use Spring Security Oaut2 5, just 2.x versions, how to implement the client credentials flow?\n\nIf you can't upgrade, you can use the **DEPRECATED** way:\n(its implementation is easier to build a RestTemplate class)\n```\n// Build o OAuth2RestTemplate:\nfinal ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();\nresourceDetails.setAccessTokenUri(\"https://your.api.url.token.com/....\");\nresourceDetails.setClientId(\"clientID\");\nresourceDetails.setClientSecret(\"clientSecret\");\nresourceDetails.setGrantType(\"client_credentials\");\n\nfinal RestTemplate oauthClient = new OAuth2RestTemplate(resourceDetails);\n```\n\n## My project uses Spring Boot version previous **2.4.x** version\n\nChange the `spring-boot-starter-oauth2-client` dependency to:\n\n```xml\n\u003cdependency\u003e\n\t\u003cgroupId\u003eorg.springframework.security\u003c/groupId\u003e\n\t\u003cartifactId\u003espring-security-oauth2-client\u003c/artifactId\u003e\n\t\u003cversion\u003e5.7.7\u003c/version\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n\t\u003cgroupId\u003eorg.springframework.security\u003c/groupId\u003e\n\t\u003cartifactId\u003espring-security-oauth2-core\u003c/artifactId\u003e\n\t\u003cversion\u003e5.7.7\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n# References\n\n- https://docs.spring.io/spring-security/site/docs/5.2.0.RELEASE/reference/html/oauth2.html\n- https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Migration-Guide\n- https://github.com/oktadev/okta-spring-boot-client-credentials-example/tree/main/client-webclient\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbzarelli%2Fspring-security-oauth2-resttemplate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgbzarelli%2Fspring-security-oauth2-resttemplate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgbzarelli%2Fspring-security-oauth2-resttemplate/lists"}