{"id":16830,"date":"2011-11-15T10:00:35","date_gmt":"2011-11-15T08:00:35","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=16830"},"modified":"2013-09-04T00:10:13","modified_gmt":"2013-09-03T21:10:13","slug":"securing-restful-web-service-with","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html","title":{"rendered":"Securing a RESTful Web Service with Spring Security 3.1, part 3"},"content":{"rendered":"<h2>1. Overview<\/h2>\n<p>This tutorial shows how to <strong>Secure a REST Service using Spring and Spring Security 3.1<\/strong> with Java based configuration. The article will focus on how to set up the Security Configuration specifically for the REST API using a Login and Cookie approach.<\/p>\n<h2>2. Spring Security in the <em>web.xml<\/em><\/h2>\n<p>The architecture of Spring Security is based entirely on Servlet Filters and, as such, comes before Spring MVC in regards to the processing of HTTP requests. Keeping this in mind, to begin with, a <strong>filter<\/strong> needs to be declared in the <em>web.xml<\/em> of the application:<\/p>\n<pre class=\" brush:xml\">&lt;filter&gt;\r\n   &lt;filter-name&gt;springSecurityFilterChain&lt;\/filter-name&gt;\r\n   &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;\/filter-class&gt;\r\n&lt;\/filter&gt;\r\n&lt;filter-mapping&gt;\r\n   &lt;filter-name&gt;springSecurityFilterChain&lt;\/filter-name&gt;\r\n   &lt;url-pattern&gt;\/*&lt;\/url-pattern&gt;\r\n&lt;\/filter-mapping&gt;<\/pre>\n<p>The filter must necessarily be named <em>\u2018springSecurityFilterChain\u2019<\/em>\u00a0 to match the default bean created by Spring Security in the container.<\/p>\n<p>Note that the defined filter is not the actual class implementing the security logic but a <em>DelegatingFilterProxy<\/em> with the purpose of delegating the Filter\u2019s methods to an internal bean. This is done so that the target bean can still benefit from the Spring context lifecycle and flexibility.<\/p>\n<p>The URL pattern used to configure the Filter is <strong><em>\/*<\/em><\/strong> even though the entire web service is mapped to <strong><em>\/api\/*<\/em><\/strong> so that the security configuration has the option to secure other possible mappings as well, if required.<\/p>\n<h2>3. The Security Configuration<\/h2>\n<pre class=\" brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;beans:beans\r\n   xmlns=\"http:\/\/www.springframework.org\/schema\/security\"\r\n   xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n   xmlns:beans=\"http:\/\/www.springframework.org\/schema\/beans\"\r\n   xmlns:sec=\"http:\/\/www.springframework.org\/schema\/security\"\r\n   xsi:schemaLocation=\"\r\n      http:\/\/www.springframework.org\/schema\/security \r\n      http:\/\/www.springframework.org\/schema\/security\/spring-security-3.1.xsd\r\n      http:\/\/www.springframework.org\/schema\/beans \r\n      http:\/\/www.springframework.org\/schema\/beans\/spring-beans-3.2.xsd\"&gt;\r\n\r\n   &lt;http entry-point-ref=\"restAuthenticationEntryPoint\"&gt;\r\n      &lt;intercept-url pattern=\"\/api\/admin\/**\" access=\"ROLE_ADMIN\"\/&gt;\r\n\r\n      &lt;form-login authentication-success-handler-ref=\"mySuccessHandler\" \/&gt;\r\n\r\n      &lt;logout \/&gt;\r\n   &lt;\/http&gt;\r\n\r\n   &lt;beans:bean id=\"mySuccessHandler\"\r\n    class=\"org.rest.security.MySavedRequestAwareAuthenticationSuccessHandler\"\/&gt;\r\n\r\n   &lt;authentication-manager alias=\"authenticationManager\"&gt;\r\n      &lt;authentication-provider&gt;\r\n         &lt;user-service&gt;\r\n            &lt;user name=\"temporary\" password=\"temporary\" authorities=\"ROLE_ADMIN\"\/&gt;\r\n            &lt;user name=\"user\" password=\"user\" authorities=\"ROLE_USER\"\/&gt;\r\n         &lt;\/user-service&gt;\r\n      &lt;\/authentication-provider&gt;\r\n   &lt;\/authentication-manager&gt;\r\n\r\n&lt;\/beans:beans&gt;<\/pre>\n<p>Most of the configuration is done using the <strong>security namespace<\/strong> \u2013 for this to be enabled, the schema locations must be defined and pointed to the correct 3.1 XSD versions. The namespace is designed so that it expresses the common uses of Spring Security while still providing hooks raw beans to accommodate more advanced scenarios.<\/p>\n<h4>3.1. The <em>&lt;http&gt;<\/em> element<\/h4>\n<p>The <em>&lt;http&gt;<\/em> element is the main container element for HTTP security configuration. In the current implementation, it only secured a single mapping: <em>\/api\/admin\/**<\/em>. Note that the mapping is <strong>relative to the root context<\/strong> of the web application, not to the <em>rest<\/em> Servlet; this is because the entire security configuration lives in the root Spring context and not in the child context of the Servlet.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h4>3.2. The Entry Point<\/h4>\n<p>In a standard web application, the authentication process may be automatically triggered when the client tries to access a secured resource without being authenticated \u2013 this is usually done by redirecting to a login page so that the user can enter credentials. However, for a <strong>REST Web Service <\/strong>this behavior doesn\u2019t make much sense \u2013 Authentication should only be done by a request to the correct URI and all other requests should simply fail with a <strong>401 UNAUTHORIZED<\/strong> status code if the user is not authenticated.<\/p>\n<p>Spring Security handles this automatic triggering of the authentication process with the concept of an <strong>Entry Point<\/strong> \u2013 this is a required part of the configuration, and can be injected via the <em>entry-point-ref<\/em> attribute of the <em>&lt;http&gt;<\/em> element. Keeping in mind that this functionality doesn\u2019t make sense in the context of the REST Service, the new custom entry point is defined to simply return 401 whenever it is triggered:<\/p>\n<pre class=\" brush:java\">@Component( \"restAuthenticationEntryPoint\" )\r\npublic class RestAuthenticationEntryPoint implements AuthenticationEntryPoint{\r\n\r\n   @Override\r\n   public void commence( HttpServletRequest request, HttpServletResponse response, \r\n    AuthenticationException authException ) throws IOException{\r\n      response.sendError( HttpServletResponse.SC_UNAUTHORIZED, \"Unauthorized\" );\r\n   }\r\n}<\/pre>\n<h4>3.3. The Login Form for REST<\/h4>\n<p>There are multiple ways to do Authentication for a REST API \u2013 one of the default Spring Security provides is <strong>Form Login<\/strong> \u2013 which uses an authentication processing filter \u2013 <em>org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter<\/em>.<\/p>\n<p>The <em>&lt;form-login&gt;<\/em> element will create this filter and will also allow us to set our custom authentication success handler on it. This can also be done manually by using the <em>&lt;custom-filter&gt;<\/em> element to register a filter at the position <em>FORM_LOGIN_FILTER<\/em> \u2013 but the namespace support is flexible enough.<\/p>\n<p>Note that for a standard web application, the <strong><em>auto-config <\/em><\/strong>attribute of the <em>&lt;http&gt; element <\/em>is shorthand syntax for some useful security configuration. While this may be appropriate for some very simple configurations, it doesn\u2019t fit and should not be used for a REST API.<\/p>\n<h4>3.4. Authentication should return 200 instead of 301<\/h4>\n<p>By default, form login will answer a successful authentication request with a <strong>301 MOVED PERMANENTLY<\/strong> status code; this makes sense in the context of an actual login form which needs to redirect after login. For a RESTful web service however, the desired response for a successful authentication should be <strong>200 OK<\/strong>.<\/p>\n<p>This is done by injecting a <strong>custom authentication success handler<\/strong> in the form login filter, to replace the default one. The new handler implements the exact same login as the default <em>org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler<\/em> with one notable difference \u2013 the redirect logic is removed:<\/p>\n<pre class=\" brush:java\">public class MySavedRequestAwareAuthenticationSuccessHandler \r\n      extends SimpleUrlAuthenticationSuccessHandler {\r\n\r\n    private RequestCache requestCache = new HttpSessionRequestCache();\r\n\r\n    @Override\r\n    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, \r\n      Authentication authentication) throws ServletException, IOException {\r\n        SavedRequest savedRequest = requestCache.getRequest(request, response);\r\n\r\n        if (savedRequest == null) {\r\n            clearAuthenticationAttributes(request);\r\n            return;\r\n        }\r\n        String targetUrlParam = getTargetUrlParameter();\r\n        if (isAlwaysUseDefaultTargetUrl() || \r\n          (targetUrlParam != null &amp;&amp; \r\n          StringUtils.hasText(request.getParameter(targetUrlParam)))) {\r\n            requestCache.removeRequest(request, response);\r\n            clearAuthenticationAttributes(request);\r\n            return;\r\n        }\r\n\r\n        clearAuthenticationAttributes(request);\r\n    }\r\n\r\n    public void setRequestCache(RequestCache requestCache) {\r\n        this.requestCache = requestCache;\r\n    }\r\n}<\/pre>\n<h4>3.5. The Authentication Manager and Provider<\/h4>\n<p>The authentication process uses an <strong>in-memory provider<\/strong> to perform authentication \u2013 this is meant to simplify the configuration as a production implementation of these artifacts is outside the scope of this post.<\/p>\n<h4>3.6 Finally \u2013 Authentication against the running REST Service<\/h4>\n<p>Now let\u2019s see how we can authenticate against the REST API \u2013 the URL for login is\u00a0<em>\/j_spring_security_check<\/em> \u2013 and a simple <em>curl<\/em> command performing login would be:<\/p>\n<pre class=\" brush:bash\">curl -i -X POST -d j_username=user -d j_password=userPass\r\nhttp:\/\/localhost:8080\/spring-security-rest\/j_spring_security_check<\/pre>\n<p>This request will return the Cookie which will then be used by any subsequent request against the REST Service.<\/p>\n<p>We can use <em>curl<\/em> to authentication and <strong>store the cookie it receives in a file<\/strong>:<\/p>\n<pre class=\" brush:bash\">curl -i -X POST -d j_username=user -d j_password=userPass -c \/opt\/cookies.txt\r\nhttp:\/\/localhost:8080\/spring-security-rest\/j_spring_security_check<\/pre>\n<p>Then <strong>we can use the cookie from the file<\/strong> to do further authenticated requests:<\/p>\n<pre class=\" brush:bash\">curl -i --header \"Accept:application\/json\" -X GET -b \/opt\/cookies.txt \r\nhttp:\/\/localhost:8080\/spring-security-rest\/api\/foos<\/pre>\n<p>This authenticated request will correctly <strong>result in a 200 OK<\/strong>:<\/p>\n<pre class=\" brush:bash\">HTTP\/1.1 200 OK\r\nServer: Apache-Coyote\/1.1\r\nContent-Type: application\/json;charset=UTF-8\r\nTransfer-Encoding: chunked\r\nDate: Wed, 24 Jul 2013 20:31:13 GMT\r\n\r\n[{\"id\":0,\"name\":\"JbidXc\"}]<\/pre>\n<h2>4. Maven and other trouble<\/h2>\n<p>The Spring <a title=\"Spring core dependencies in detail\" href=\"http:\/\/www.baeldung.com\/spring-with-maven#mvc\">core dependencies<\/a> necessary for a web application and for the REST Service have been discussed in detail. For security, we\u2019ll need to add: <em>spring-security-web<\/em> and <em>spring-security-config<\/em> \u2013 all of these have also been covered in the <a title=\"Maven artifacts for securing the REST project\" href=\"http:\/\/www.baeldung.com\/spring-security-with-maven\">Maven for Spring Security<\/a> tutorial.<\/p>\n<p>It\u2019s worth paying close attention to the way Maven will resolve the older Spring dependencies \u2013 the resolution strategy will start <a title=\"Maven resolution of Spring core artifacts - problem and solution\" href=\"http:\/\/www.baeldung.com\/spring-security-with-maven#maven_problem\">causing problems<\/a> once the security artifacts are added to the pom. To address this problem, some of the core dependencies will need to be overridden in order to keep them at the right version.<\/p>\n<h2>5. Conclusion<\/h2>\n<p>This post covered the basic security configuration and implementation for a RESTful Service using <strong>Spring Security 3.1<\/strong>, discussing the <em>web.xml<\/em>, the security configuration, the HTTP status codes for the authentication process and the Maven resolution of the security artifacts.<\/p>\n<p>The implementation of this Spring Security REST Tutorial can be found in <a title=\"Spring Security REST Tutorial\" href=\"https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/spring-security-rest#readme\" target=\"_blank\" rel=\"nofollow\">the github project<\/a> \u2013 this is an Eclipse based project, so it should be easy to import and run as it is.<br \/>\n&nbsp;<\/p>\n<div style=\"border: 1px solid #D8D8D8; background: #FAFAFA; width: 100%; padding-left: 5px;\"><b><i>Reference: <\/i><\/b><a href=\"http:\/\/www.baeldung.com\/2011\/10\/31\/securing-a-restful-web-service-with-spring-security-3-1-part-3\/\">Spring REST Service Security 3<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Eugen Paraschiv at the <a href=\"http:\/\/www.baeldung.com\/\">baeldung<\/a> blog.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. Overview This tutorial shows how to Secure a REST Service using Spring and Spring Security 3.1 with Java based configuration. The article will focus on how to set up the Security Configuration specifically for the REST API using a Login and Cookie approach. 2. Spring Security in the web.xml The architecture of Spring Security &hellip;<\/p>\n","protected":false},"author":104,"featured_media":242,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[54,30,125],"class_list":["post-16830","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-restful-web-services","tag-spring","tag-spring-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring REST Service Security 3<\/title>\n<meta name=\"description\" content=\"1. Overview This tutorial shows how to Secure a REST Service using Spring and Spring Security 3.1 with Java based configuration. The article will focus on\" \/>\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.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring REST Service Security 3\" \/>\n<meta property=\"og:description\" content=\"1. Overview This tutorial shows how to Secure a REST Service using Spring and Spring Security 3.1 with Java based configuration. The article will focus on\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2011-11-15T08:00:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2013-09-03T21:10:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-security-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Eugen Paraschiv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/baeldung\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Eugen Paraschiv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html\"},\"author\":{\"name\":\"Eugen Paraschiv\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/7a8ad27f4bb34bb3664fda07d3142bc4\"},\"headline\":\"Securing a RESTful Web Service with Spring Security 3.1, part 3\",\"datePublished\":\"2011-11-15T08:00:35+00:00\",\"dateModified\":\"2013-09-03T21:10:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html\"},\"wordCount\":1050,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-security-logo.jpg\",\"keywords\":[\"RESTful Web Services\",\"Spring\",\"Spring Security\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html\",\"name\":\"Spring REST Service Security 3\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-security-logo.jpg\",\"datePublished\":\"2011-11-15T08:00:35+00:00\",\"dateModified\":\"2013-09-03T21:10:13+00:00\",\"description\":\"1. Overview This tutorial shows how to Secure a REST Service using Spring and Spring Security 3.1 with Java based configuration. The article will focus on\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-security-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-security-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2011\\\/11\\\/securing-restful-web-service-with.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Securing a RESTful Web Service with Spring Security 3.1, part 3\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/7a8ad27f4bb34bb3664fda07d3142bc4\",\"name\":\"Eugen Paraschiv\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g\",\"caption\":\"Eugen Paraschiv\"},\"sameAs\":[\"http:\\\/\\\/www.baeldung.com\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/in\\\/eugenparaschiv\",\"https:\\\/\\\/x.com\\\/http:\\\/\\\/twitter.com\\\/baeldung\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Eugen-Paraschiv\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring REST Service Security 3","description":"1. Overview This tutorial shows how to Secure a REST Service using Spring and Spring Security 3.1 with Java based configuration. The article will focus on","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.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html","og_locale":"en_US","og_type":"article","og_title":"Spring REST Service Security 3","og_description":"1. Overview This tutorial shows how to Secure a REST Service using Spring and Spring Security 3.1 with Java based configuration. The article will focus on","og_url":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2011-11-15T08:00:35+00:00","article_modified_time":"2013-09-03T21:10:13+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-security-logo.jpg","type":"image\/jpeg"}],"author":"Eugen Paraschiv","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/baeldung","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Eugen Paraschiv","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html"},"author":{"name":"Eugen Paraschiv","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/7a8ad27f4bb34bb3664fda07d3142bc4"},"headline":"Securing a RESTful Web Service with Spring Security 3.1, part 3","datePublished":"2011-11-15T08:00:35+00:00","dateModified":"2013-09-03T21:10:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html"},"wordCount":1050,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-security-logo.jpg","keywords":["RESTful Web Services","Spring","Spring Security"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html","url":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html","name":"Spring REST Service Security 3","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-security-logo.jpg","datePublished":"2011-11-15T08:00:35+00:00","dateModified":"2013-09-03T21:10:13+00:00","description":"1. Overview This tutorial shows how to Secure a REST Service using Spring and Spring Security 3.1 with Java based configuration. The article will focus on","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-security-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-security-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2011\/11\/securing-restful-web-service-with.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Securing a RESTful Web Service with Spring Security 3.1, part 3"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/7a8ad27f4bb34bb3664fda07d3142bc4","name":"Eugen Paraschiv","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d1e55876feb753ccc6de08d413df2c915e5704dd901010340c1499a7572f8d7a?s=96&d=mm&r=g","caption":"Eugen Paraschiv"},"sameAs":["http:\/\/www.baeldung.com\/","http:\/\/www.linkedin.com\/in\/eugenparaschiv","https:\/\/x.com\/http:\/\/twitter.com\/baeldung"],"url":"https:\/\/www.javacodegeeks.com\/author\/Eugen-Paraschiv"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/16830","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/104"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=16830"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/16830\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/242"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=16830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=16830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=16830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}