{"id":26376,"date":"2016-02-11T13:35:43","date_gmt":"2016-02-11T08:05:43","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=26376"},"modified":"2016-03-03T17:24:37","modified_gmt":"2016-03-03T11:54:37","slug":"restrict-ajax-request-caching-in-springsecurity","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/","title":{"rendered":"Restrict Ajax request caching in SpringSecurity"},"content":{"rendered":"<p>Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict request caching for Ajax request in <a title=\"grails development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">Grails App<\/a>, we just need to follow some steps.<\/p>\n<ol>\n<li>Create a class and extends it with HttpSessionRequestCache.<\/li>\n<li>Override its saveRequest() method<\/li>\n<\/ol>\n<p>[java]<\/p>\n<p>class CustomHttpSessionRequestCache extends HttpSessionRequestCache {<br \/>\n\tpublic void saveRequest(HttpServletRequest request, HttpServletResponse response) {<br \/>\n\t\tif(!&quot;XMLHttpRequest&quot;.equals(request.getHeader(&quot;X-Requested-With&quot;))) {<br \/>\n\t\t\tsuper.saveReqeust(request, response);<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>Now register it as spring bean with name <strong>requestCache <\/strong>in your resources.groovy file<\/p>\n<p>[java]<\/p>\n<p>requestCache(ApplicationHttpSessionRequestCache) {<br \/>\n\tportResolver = ref(&#8216;portResolver&#8217;)<br \/>\n\tcreateSessionAllowed = conf.requestCache.createSession \/\/ true<br \/>\n\trequestMatcher = ref(&#8216;requestMatcher&#8217;)<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n<p>&nbsp;<\/p>\n<p>Thats it \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict [&hellip;]<\/p>\n","protected":false},"author":185,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":5,"footnotes":""},"categories":[7],"tags":[672],"class_list":["post-26376","post","type-post","status-publish","format-standard","hentry","category-grails","tag-spring-security"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Jitendra Singh\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#article\",\"name\":\"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog\",\"headline\":\"Restrict Ajax request caching in SpringSecurity\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/jitendra-singh\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2016-02-11T13:35:43+05:30\",\"dateModified\":\"2016-03-03T17:24:37+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#webpage\"},\"articleSection\":\"Grails, spring security\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#listItem\",\"name\":\"Restrict Ajax request caching in SpringSecurity\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#listItem\",\"position\":3,\"name\":\"Restrict Ajax request caching in SpringSecurity\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/jitendra-singh\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/jitendra-singh\\\/\",\"name\":\"Jitendra Singh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/ea191f7a1cf42accf601146c6296da68cdea5f0d5e3a816907ee76d86aa4a6f2?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Jitendra Singh\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/\",\"name\":\"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog\",\"description\":\"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/restrict-ajax-request-caching-in-springsecurity\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/jitendra-singh\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/jitendra-singh\\\/#author\"},\"datePublished\":\"2016-02-11T13:35:43+05:30\",\"dateModified\":\"2016-03-03T17:24:37+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog","description":"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict","canonical_url":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#article","name":"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog","headline":"Restrict Ajax request caching in SpringSecurity","author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/jitendra-singh\/#author"},"publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"},"datePublished":"2016-02-11T13:35:43+05:30","dateModified":"2016-03-03T17:24:37+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#webpage"},"isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#webpage"},"articleSection":"Grails, spring security"},{"@type":"BreadcrumbList","@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","position":1,"name":"Home","item":"https:\/\/www.tothenew.com\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/www.tothenew.com\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#listItem","name":"Restrict Ajax request caching in SpringSecurity"},"previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#listItem","position":3,"name":"Restrict Ajax request caching in SpringSecurity","previousItem":{"@type":"ListItem","@id":"https:\/\/www.tothenew.com\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/www.tothenew.com\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/www.tothenew.com\/blog\/"},{"@type":"Person","@id":"https:\/\/www.tothenew.com\/blog\/author\/jitendra-singh\/#author","url":"https:\/\/www.tothenew.com\/blog\/author\/jitendra-singh\/","name":"Jitendra Singh","image":{"@type":"ImageObject","@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/ea191f7a1cf42accf601146c6296da68cdea5f0d5e3a816907ee76d86aa4a6f2?s=96&d=mm&r=g","width":96,"height":96,"caption":"Jitendra Singh"}},{"@type":"WebPage","@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#webpage","url":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/","name":"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog","description":"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/www.tothenew.com\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/#breadcrumblist"},"author":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/jitendra-singh\/#author"},"creator":{"@id":"https:\/\/www.tothenew.com\/blog\/author\/jitendra-singh\/#author"},"datePublished":"2016-02-11T13:35:43+05:30","dateModified":"2016-03-03T17:24:37+05:30"},{"@type":"WebSite","@id":"https:\/\/www.tothenew.com\/blog\/#website","url":"https:\/\/www.tothenew.com\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/www.tothenew.com\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog","og:description":"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict","og:url":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/","og:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Restrict Ajax request caching in SpringSecurity | TO THE NEW Blog","twitter:description":"Spring Security has a nice feature of request caching. When user try to access secured resource without logging in into the system, spring security caches that request and redirect the user to the login page. After successful authentication it redirects user to that cached request. This works for both Ajax and non-ajax requests. To restrict","twitter:image":"https:\/\/www.tothenew.com\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"26376","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":"","og_description":"","og_object_type":"blog","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":"","og_article_tags":"","twitter_use_og":false,"twitter_card":"summary","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 13:23:00","updated":"2024-02-29 08:44:21","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/www.tothenew.com\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tRestrict Ajax request caching in SpringSecurity\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/www.tothenew.com\/blog"},{"label":"Grails","link":"https:\/\/www.tothenew.com\/blog\/category\/grails\/"},{"label":"Restrict Ajax request caching in SpringSecurity","link":"https:\/\/www.tothenew.com\/blog\/restrict-ajax-request-caching-in-springsecurity\/"}],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/185"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=26376"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/26376\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=26376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=26376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=26376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}