{"id":4704,"date":"2018-06-21T10:40:47","date_gmt":"2018-06-21T05:10:47","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4704"},"modified":"2023-01-20T18:55:40","modified_gmt":"2023-01-20T13:25:40","slug":"spring-security-with-oauth2","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/","title":{"rendered":"Spring Security with OAuth2"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><strong>Spring Security<\/strong> provides comprehensive security services for <em>J2EE &#8211; based enterprise software applications.<\/em> It is powerful, flexible and pluggable.<br \/>\nIt is not Proxy server, firewall, OS level Security, Intrusion Detection System,\u00a0 and JVM Security.<\/p>\n<p><strong>OAuth<\/strong> is open authorization protocol, which allows accessing resources of the resource owner by enabling the client applications on HTTP services such as Gmail, GitHub, etc.<\/p>\n<blockquote><p>The OAuth 2.0 framework enables a third-party application to obtain limited access to an HTTP service, either on behalf of a resource owner by orchestrating an approval interaction between resource owner and HTTP service, or by allowing the third-party application to obtain access on its own behalf.<\/p><\/blockquote>\n<p><strong>OAuth2 Roles :<\/strong> There are four roles which can applied on OAuth2.<\/p>\n<ol>\n<li><strong style=\"font-size: 1rem;\">Resource Owner :<\/strong><span style=\"font-size: 1rem;\">\u00a0The owner of the resource &#8211; this is pretty self-explanatory \ud83d\ude42<\/span><\/li>\n<li><strong>Resource Server :<\/strong>\u00a0It serves resources that are protected by the OAuth2 token.<\/li>\n<li><strong>Client :<\/strong>\u00a0The application accessing the resource server.<\/li>\n<li><strong style=\"font-size: 1rem;\">Authorization Server :<\/strong><span style=\"font-size: 1rem;\"><span style=\"font-size: 1rem;\">\u00a0 The server issuing access tokens to the client after successfully authenticating the resource owner and obtaining authorization.<\/span><\/span><\/li>\n<\/ol>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-4750\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Oauth-architecture-300x167.png\" alt=\"\" width=\"596\" height=\"332\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Oauth-architecture-300x167.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Oauth-architecture-768x426.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Oauth-architecture-1024x569.png 1024w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Oauth-architecture.png 1140w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/p>\n<p><strong>OAuth2 Tokens :<\/strong> Tokens are implementation specific random strings, generated by the authorization server.<\/p>\n<ul>\n<li><strong style=\"font-size: 1rem;\">Access Token :<\/strong><span style=\"font-size: 1rem;\"> Sent with each request, usually valid for about an hour only.<\/span><\/li>\n<li><strong style=\"font-size: 1rem;\">Refresh Token :<\/strong><span style=\"font-size: 1rem;\"> It is used to get new access token, not sent with each request, usually lives longer than access token.<\/span><\/li>\n<\/ul>\n<h4>Now, let&#8217;s implement the project for Spring Security with OAuth2 :<\/h4>\n<p>First create a <strong>maven project<\/strong> here.,in eclipse IDE which will looks like :<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-4721\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/image-178x300.png\" alt=\"\" width=\"298\" height=\"502\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/image-178x300.png 178w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/image.png 301w\" sizes=\"(max-width: 298px) 100vw, 298px\" \/><\/p>\n<h2 style=\"text-align: center;\">Resource Server<\/h2>\n<pre class=\"lang:default decode:true \" title=\"Resource Server\">package com.security.oauth.security;\r\n\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.security.config.annotation.web.builders.HttpSecurity;\r\nimport org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;\r\nimport org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;\r\nimport org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;\r\nimport org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler;\r\n\r\n@Configuration\r\n@EnableResourceServer \r\n\/*@EnableResourceServer enables a Spring Security filter that authenticates requests using an incoming OAuth2 token.*\/\r\npublic class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {\r\n\t\/*class ResourceServerConfigurerAdapter implements ResourceServerConfigurer providing methods to adjust the access rules and paths that are protected by OAuth2 security.*\/\r\n\tprivate static final String RESOURCE_ID = \"my_rest_api\";\r\n\t\r\n\t@Override\r\n\tpublic void configure(ResourceServerSecurityConfigurer resources) {\r\n\t\tresources.resourceId(RESOURCE_ID).stateless(false);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void configure(HttpSecurity http) throws Exception {\r\n\t\thttp.\r\n\t\tanonymous().disable()\r\n\t\t.requestMatchers().antMatchers(\"\/user\/**\")\r\n\t\t.and().authorizeRequests()\r\n\t\t.antMatchers(\"\/user\/**\").access(\"hasRole('ADMIN')\")\r\n\t\t.and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());\r\n\t}\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center;\">Authorization Server<\/h2>\n<pre class=\"lang:default decode:true \" title=\"Authorization Server responsible for verifying credentials.\">package com.security.oauth.security;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.beans.factory.annotation.Qualifier;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.security.authentication.AuthenticationManager;\r\nimport org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;\r\nimport org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;\r\nimport org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;\r\nimport org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;\r\nimport org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;\r\nimport org.springframework.security.oauth2.provider.approval.UserApprovalHandler;\r\nimport org.springframework.security.oauth2.provider.token.TokenStore;\r\n\r\n@Configuration\r\n@EnableAuthorizationServer\r\n\/*@EnableAuthorizationServer enables an Authorization Server (i.e. an AuthorizationEndpoint and a TokenEndpoint) in the current application context.*\/\r\npublic class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {\r\n\/*class AuthorizationServerConfigurerAdapter implements AuthorizationServerConfigurer which provides all the necessary methods to configure an Authorization server.*\/\r\n\tprivate static String REALM=\"MY_OAUTH_REALM\";\r\n\t\r\n\t@Autowired\r\n\tprivate TokenStore tokenStore;\r\n\r\n\t@Autowired\r\n\tprivate UserApprovalHandler userApprovalHandler;\r\n\r\n\t@Autowired\r\n\t@Qualifier(\"authenticationManagerBean\")\r\n\tprivate AuthenticationManager authenticationManager;\r\n\r\n\t@Override\r\n\tpublic void configure(ClientDetailsServiceConfigurer clients) throws Exception {\r\n\r\n\t\tclients.inMemory()\r\n\t        .withClient(\"my-trusted-client\")\r\n            .authorizedGrantTypes(\"password\", \"authorization_code\", \"refresh_token\", \"implicit\")\r\n            .authorities(\"ROLE_CLIENT\", \"ROLE_TRUSTED_CLIENT\")\r\n            .scopes(\"read\", \"write\", \"trust\")\r\n            .secret(\"secret\")\r\n            .accessTokenValiditySeconds(120).\/\/Access token is only valid for 2 minutes.\r\n            refreshTokenValiditySeconds(600);\/\/Refresh token is only valid for 10 minutes.\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {\r\n\t\tendpoints.tokenStore(tokenStore).userApprovalHandler(userApprovalHandler)\r\n\t\t\t\t.authenticationManager(authenticationManager);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {\r\n\t\toauthServer.realm(REALM+\"\/client\");\r\n\t}\r\n\r\n}<\/pre>\n<h2 style=\"text-align: center;\">Security Configuration<\/h2>\n<pre class=\"lang:default decode:true \" title=\"Security Configuration\">package com.security.oauth.security;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.security.authentication.AuthenticationManager;\r\nimport org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;\r\nimport org.springframework.security.config.annotation.web.builders.HttpSecurity;\r\nimport org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;\r\nimport org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;\r\nimport org.springframework.security.crypto.password.NoOpPasswordEncoder;\r\nimport org.springframework.security.oauth2.provider.ClientDetailsService;\r\nimport org.springframework.security.oauth2.provider.approval.ApprovalStore;\r\nimport org.springframework.security.oauth2.provider.approval.TokenApprovalStore;\r\nimport org.springframework.security.oauth2.provider.approval.TokenStoreUserApprovalHandler;\r\nimport org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;\r\nimport org.springframework.security.oauth2.provider.token.TokenStore;\r\nimport org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;\r\n\r\n@Configuration\r\n@EnableWebSecurity\r\npublic class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {\r\n\r\n\t@Autowired\r\n\tprivate ClientDetailsService clientDetailsService;\r\n\t\r\n\t@Autowired\r\n    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {\r\n        auth.inMemoryAuthentication()\r\n        .withUser(\"bill\").password(\"abc123\").roles(\"ADMIN\").and()\r\n        .withUser(\"bob\").password(\"abc123\").roles(\"USER\");\r\n    }\r\n\t\r\n\r\n    @Override\r\n    protected void configure(HttpSecurity http) throws Exception {\r\n\t\thttp\r\n\t\t.csrf().disable()\r\n\t\t.anonymous().disable()\r\n\t  \t.authorizeRequests()\r\n\t  \t.antMatchers(\"\/oauth\/token\").permitAll();\r\n    }\r\n\r\n    @Override\r\n    @Bean\r\n    public AuthenticationManager authenticationManagerBean() throws Exception {\r\n        return super.authenticationManagerBean();\r\n    }\r\n\r\n\r\n\t@Bean\r\n\tpublic TokenStore tokenStore() {\r\n\t\treturn new InMemoryTokenStore();\r\n\t}\r\n\r\n\t@Bean\r\n\t@Autowired\r\n\tpublic TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore){\r\n\t\tTokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();\r\n\t\thandler.setTokenStore(tokenStore);\r\n\t\thandler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));\r\n\t\thandler.setClientDetailsService(clientDetailsService);\r\n\t\treturn handler;\r\n\t}\r\n\t\r\n\t@Bean\r\n\t@Autowired\r\n\tpublic ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {\r\n\t\tTokenApprovalStore store = new TokenApprovalStore();\r\n\t\tstore.setTokenStore(tokenStore);\r\n\t\treturn store;\r\n\t}\r\n\t\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2 style=\"text-align: center;\">Method Security Configuration<\/h2>\n<pre class=\"lang:default decode:true \" title=\"Enable Global method security\">package com.security.oauth.security;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.context.annotation.Configuration;\r\nimport org.springframework.security.access.expression.method.MethodSecurityExpressionHandler;\r\nimport org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;\r\nimport org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration;\r\nimport org.springframework.security.oauth2.provider.expression.OAuth2MethodSecurityExpressionHandler;\r\n\r\n@Configuration\r\n@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)\r\npublic class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {\r\n    @SuppressWarnings(\"unused\")\r\n\t@Autowired\r\n    private OAuth2SecurityConfiguration securityConfig;\r\n    \r\n    @Override\r\n    protected MethodSecurityExpressionHandler createExpressionHandler() {\r\n        return new OAuth2MethodSecurityExpressionHandler();\r\n    }\r\n}\r\n<\/pre>\n<h2 style=\"text-align: center;\">Controller<\/h2>\n<pre class=\"lang:default decode:true \" title=\"Controller\">package com.security.oauth.controller;\r\n \r\nimport java.util.List;\r\n \r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.http.HttpHeaders;\r\nimport org.springframework.http.HttpStatus;\r\nimport org.springframework.http.MediaType;\r\nimport org.springframework.http.ResponseEntity;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.RequestBody;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.RequestMethod;\r\nimport org.springframework.web.bind.annotation.RestController;\r\nimport org.springframework.web.util.UriComponentsBuilder;\r\n\r\nimport com.security.oauth.model.User;\r\nimport com.security.oauth.service.UserService;\r\n \r\n@RestController\r\npublic class OAuth2RestController {\r\n \r\n    @Autowired\r\n    UserService userService;  \/\/Service which will do all data retrieval\/manipulation work\r\n \r\n     \r\n    \/\/-------------------Retrieve All Users--------------------------------------------------------\r\n     \r\n    @RequestMapping(value = \"\/user\/\", method = RequestMethod.GET)\r\n    public ResponseEntity&lt;List&lt;User&gt;&gt; listAllUsers() {\r\n        List&lt;User&gt; users = userService.findAllUsers();\r\n        if(users.isEmpty()){\r\n            return new ResponseEntity&lt;List&lt;User&gt;&gt;(users, HttpStatus.NO_CONTENT);\/\/You many decide to return HttpStatus.NOT_FOUND\r\n        }\r\n        return new ResponseEntity&lt;List&lt;User&gt;&gt;(users, HttpStatus.OK);\r\n    }\r\n \r\n \r\n    \/\/-------------------Retrieve Single User--------------------------------------------------------\r\n     \r\n    @RequestMapping(value = \"\/user\/{id}\", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE})\r\n    public ResponseEntity&lt;User&gt; getUser(@PathVariable(\"id\") long id) {\r\n        System.out.println(\"Fetching User with id \" + id);\r\n        User user = userService.findById(id);\r\n        if (user == null) {\r\n            System.out.println(\"User with id \" + id + \" not found\");\r\n            return new ResponseEntity&lt;User&gt;(HttpStatus.NOT_FOUND);\r\n        }\r\n        return new ResponseEntity&lt;User&gt;(user, HttpStatus.OK);\r\n    }\r\n \r\n     \r\n     \r\n    \/\/-------------------Create a User--------------------------------------------------------\r\n     \r\n    @RequestMapping(value = \"\/user\/\", method = RequestMethod.POST)\r\n    public ResponseEntity&lt;Void&gt; createUser(@RequestBody User user, UriComponentsBuilder ucBuilder) {\r\n        System.out.println(\"Creating User \" + user.getName());\r\n \r\n        if (userService.isUserExist(user)) {\r\n            System.out.println(\"A User with name \" + user.getName() + \" already exist\");\r\n            return new ResponseEntity&lt;Void&gt;(HttpStatus.CONFLICT);\r\n        }\r\n \r\n        userService.saveUser(user);\r\n \r\n        HttpHeaders headers = new HttpHeaders();\r\n        headers.setLocation(ucBuilder.path(\"\/user\/{id}\").buildAndExpand(user.getId()).toUri());\r\n        return new ResponseEntity&lt;Void&gt;(headers, HttpStatus.CREATED);\r\n    }\r\n \r\n     \r\n    \/\/------------------- Update a User --------------------------------------------------------\r\n     \r\n    @RequestMapping(value = \"\/user\/{id}\", method = RequestMethod.PUT)\r\n    public ResponseEntity&lt;User&gt; updateUser(@PathVariable(\"id\") long id, @RequestBody User user) {\r\n        System.out.println(\"Updating User \" + id);\r\n         \r\n        User currentUser = userService.findById(id);\r\n         \r\n        if (currentUser==null) {\r\n            System.out.println(\"User with id \" + id + \" not found\");\r\n            return new ResponseEntity&lt;User&gt;(HttpStatus.NOT_FOUND);\r\n        }\r\n \r\n        currentUser.setName(user.getName());\r\n        currentUser.setAge(user.getAge());\r\n        currentUser.setSalary(user.getSalary());\r\n         \r\n        userService.updateUser(currentUser);\r\n        return new ResponseEntity&lt;User&gt;(currentUser, HttpStatus.OK);\r\n    }\r\n \r\n    \/\/------------------- Delete a User --------------------------------------------------------\r\n     \r\n    @RequestMapping(value = \"\/user\/{id}\", method = RequestMethod.DELETE)\r\n    public ResponseEntity&lt;User&gt; deleteUser(@PathVariable(\"id\") long id) {\r\n        System.out.println(\"Fetching &amp; Deleting User with id \" + id);\r\n \r\n        User user = userService.findById(id);\r\n        if (user == null) {\r\n            System.out.println(\"Unable to delete. User with id \" + id + \" not found\");\r\n            return new ResponseEntity&lt;User&gt;(HttpStatus.NOT_FOUND);\r\n        }\r\n \r\n        userService.deleteUserById(id);\r\n        return new ResponseEntity&lt;User&gt;(HttpStatus.NO_CONTENT);\r\n    }\r\n \r\n     \r\n    \/\/------------------- Delete All Users --------------------------------------------------------\r\n     \r\n    @RequestMapping(value = \"\/user\/\", method = RequestMethod.DELETE)\r\n    public ResponseEntity&lt;User&gt; deleteAllUsers() {\r\n        System.out.println(\"Deleting All Users\");\r\n \r\n        userService.deleteAllUsers();\r\n        return new ResponseEntity&lt;User&gt;(HttpStatus.NO_CONTENT);\r\n    }\r\n \r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h2>Running the application :<\/h2>\n<ul>\n<li>You can either run the application by hitting the REST API by Postman.<\/li>\n<li>You can also run this application by SpringRestClient class present in src\/test\/java, which is internally hitting the REST API by RestTemplateclass.<\/li>\n<\/ul>\n<h3>You can also download this example :\u00a0 \u00a0 \u00a0 \u00a0<a href=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/SpringSecurityOAuth2.zip\"><img decoding=\"async\" class=\"alignnone wp-image-4726\" src=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/unnamed-300x62.png\" alt=\"\" width=\"184\" height=\"38\" srcset=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/unnamed-300x62.png 300w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/unnamed-768x159.png 768w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/unnamed-624x129.png 624w, https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/unnamed.png 800w\" sizes=\"(max-width: 184px) 100vw, 184px\" \/><\/a><\/h3>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Spring Security provides comprehensive security services for J2EE &#8211; based enterprise software applications. It is powerful, flexible and pluggable. It is not Proxy server, firewall, OS level Security, Intrusion Detection System,\u00a0 and JVM Security. OAuth is open authorization protocol, which allows accessing resources of the resource owner by enabling the client applications on HTTP [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4749,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[256,71],"tags":[285,286,284],"class_list":["post-4704","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-application","category-mobile","tag-oauth2","tag-restclient","tag-spring-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Security with OAuth2 - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Security with OAuth2 - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"&nbsp; Spring Security provides comprehensive security services for J2EE &#8211; based enterprise software applications. It is powerful, flexible and pluggable. It is not Proxy server, firewall, OS level Security, Intrusion Detection System,\u00a0 and JVM Security. OAuth is open authorization protocol, which allows accessing resources of the resource owner by enabling the client applications on HTTP [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-21T05:10:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-20T13:25:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Spring-security-with-OAuth-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1140\" \/>\n\t<meta property=\"og:image:height\" content=\"633\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Spring Security with OAuth2\",\"datePublished\":\"2018-06-21T05:10:47+00:00\",\"dateModified\":\"2023-01-20T13:25:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/\"},\"wordCount\":304,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/Spring-security-with-OAuth-2.png\",\"keywords\":[\"OAuth2\",\"RestClient\",\"Spring Security\"],\"articleSection\":[\"Java Application\",\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/\",\"name\":\"Spring Security with OAuth2 - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/Spring-security-with-OAuth-2.png\",\"datePublished\":\"2018-06-21T05:10:47+00:00\",\"dateModified\":\"2023-01-20T13:25:40+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/Spring-security-with-OAuth-2.png\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/06\\\/Spring-security-with-OAuth-2.png\",\"width\":1140,\"height\":633},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/spring-security-with-oauth2\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring Security with OAuth2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"AI, Software Development & Digital Engineering Insights Blog | InnovationM\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"https:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Security with OAuth2 - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/","og_locale":"en_US","og_type":"article","og_title":"Spring Security with OAuth2 - InnovationM - Blog","og_description":"&nbsp; Spring Security provides comprehensive security services for J2EE &#8211; based enterprise software applications. It is powerful, flexible and pluggable. It is not Proxy server, firewall, OS level Security, Intrusion Detection System,\u00a0 and JVM Security. OAuth is open authorization protocol, which allows accessing resources of the resource owner by enabling the client applications on HTTP [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-06-21T05:10:47+00:00","article_modified_time":"2023-01-20T13:25:40+00:00","og_image":[{"width":1140,"height":633,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Spring-security-with-OAuth-2.png","type":"image\/png"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Spring Security with OAuth2","datePublished":"2018-06-21T05:10:47+00:00","dateModified":"2023-01-20T13:25:40+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/"},"wordCount":304,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Spring-security-with-OAuth-2.png","keywords":["OAuth2","RestClient","Spring Security"],"articleSection":["Java Application","Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/","url":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/","name":"Spring Security with OAuth2 - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Spring-security-with-OAuth-2.png","datePublished":"2018-06-21T05:10:47+00:00","dateModified":"2023-01-20T13:25:40+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Spring-security-with-OAuth-2.png","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/06\/Spring-security-with-OAuth-2.png","width":1140,"height":633},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/spring-security-with-oauth2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Spring Security with OAuth2"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"AI, Software Development & Digital Engineering Insights Blog | InnovationM","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["https:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4704"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4704\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4749"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}