{"id":70,"date":"2021-04-01T17:06:33","date_gmt":"2021-04-01T17:06:33","guid":{"rendered":"https:\/\/valerio.nu\/?p=70"},"modified":"2021-04-01T17:06:34","modified_gmt":"2021-04-01T17:06:34","slug":"how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together","status":"publish","type":"post","link":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/","title":{"rendered":"How to use Keycloak Spring Boot adapter and memcached-session-manager together"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">The problem<\/h2>\n\n\n\n<p>If you&#8217;re using <a href=\"https:\/\/stackoverflow.com\/questions\/66583181\/utilize-serialization-filter-objectinputerfilter-with-keycloak-adapter-and-mem\/\"><em>memcached <\/em>for session management in your Spring Boot application and you&#8217;re also using Keycloak<\/a> Spring Boot adapter, you might be googling right now because the Java Serialization does not work in your project. That is, either you&#8217;re getting an Exception with the message:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>filter can not be set after an object has been read<\/code><\/pre>\n\n\n\n<p>or perhaps you noticed that your serialization filter does not work. Either all classes are allowed to be serialized, which creates a security issue, or only the Keycloak classes are allowed, which breaks your application because the sessions saved in <em>memcached<\/em> cannot be resumed properly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s going on<\/h2>\n\n\n\n<p>The issue is that <em>memcached<\/em> session handler is deserializing a bunch of different classes at the same time. That is, in the same <code>ObjectInputStream<\/code>. This happens because your session is composed by a number of different objects; so <em>memcached<\/em> serializes them one-by-one into its storage and then, the same way, it deserializes them back into your application. This works great until you deserialize an object of the class <code>KeycloakSecurityContext<\/code>. <a href=\"https:\/\/github.com\/keycloak\/keycloak\/blob\/master\/core\/src\/main\/java\/org\/keycloak\/KeycloakSecurityContext.java\">That class<\/a> introduces a filter in your <code>ObjectInputStream<\/code>. <\/p>\n\n\n\n<p>What happens after that is that the Keycloak Spring Boot adapter class is allowed and it keeps being deserialized correctly, but now suddenly <strong>all other classes are not allowed<\/strong> <strong>to be deserialized<\/strong> because keycloak excludes them by adding &#8220;!*&#8221; at the end of <a href=\"https:\/\/github.com\/keycloak\/keycloak\/blob\/master\/common\/src\/main\/java\/org\/keycloak\/common\/util\/DelegatingSerializationFilter.java\" target=\"_blank\" rel=\"noreferrer noopener\">its filter<\/a>. <\/p>\n\n\n\n<p>Now you have a strong headache \ud83d\ude35 and you&#8217;re thinking &#8220;Enough bullshit, how do I fix this?&#8221;. I understand and I&#8217;ve got a bad headache as well, keep reading.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The solution<\/h2>\n\n\n\n<p>There are multiple solutions to this issue (good news! \ud83d\ude00) depending on how deep you dare (\ud83d\ude23) go and change things in your application.<\/p>\n\n\n\n<p>The correct\u2122 solution is to make sure that\u00a0<code>KeycloakSecurityContext<\/code>\u00a0objects (and any other class that introduces such filters)\u00a0<strong>are deserialized in their own streams<\/strong>\u00a0rather than in the same, common, stream. <\/p>\n\n\n\n<p>If this is not possible in your project, you need to override the filter in\u00a0<code>KeycloakSecurityContext<\/code>\u00a0by extending the class. If you&#8217;re choosing this path (fly, you fools \ud83d\ude08) you will have to edit the <code>ObjectFilter<\/code>. The approach that I would think it&#8217;s most correct in this case is to completely remove the filter from\u00a0<code>KeycloakSecurityContext<\/code>\u00a0and then\u00a0<strong>add a filter at the application level where you define all the classes and the packages that you allow for deserialization<\/strong>\u00a0in your application.\u00a0<a href=\"https:\/\/docs.oracle.com\/javase\/10\/core\/serialization-filtering1.htm\">Link on how to do it<\/a>.<\/p>\n\n\n\n<p>Another approach, a bit hacky but easier to test, is to add all the relevant classes in the filter in the class extending\u00a0<code>KeycloakSecurityContext<\/code>\u00a0(try this for a quick test but please, don&#8217;t use it in production).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This is a bug, or let&#8217;s say a missing functionality, in\u00a0<code>memcached-session-manager<\/code>. In this article we lister several methods to work around the filtering problem that this issue creates. There is already an <a href=\"https:\/\/github.com\/magro\/memcached-session-manager\/issues\/427\">open ticket in memcached-session-manager<\/a>&#8216;s <a href=\"https:\/\/github.com\/magro\/memcached-session-manager\/issues\/427\">repository<\/a> and hopefully there will soon be a new version that is completely compatible with Keycloak. \ud83e\udd1e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The problem If you&#8217;re using memcached for session management in your Spring Boot application and you&#8217;re also using Keycloak Spring Boot adapter, you might be [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[12,13,14],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-java","tag-keycloak","tag-memcached","tag-objectinputstream"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to use Keycloak Spring Boot adapter with memcached<\/title>\n<meta name=\"description\" content=\"either all classes are allowed to be serialized, which creates a security issue, or only the Keycloak Spring Boot adapter classes are allowed\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use Keycloak Spring Boot adapter with memcached\" \/>\n<meta property=\"og:description\" content=\"either all classes are allowed to be serialized, which creates a security issue, or only the Keycloak Spring Boot adapter classes are allowed\" \/>\n<meta property=\"og:url\" content=\"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/\" \/>\n<meta property=\"og:site_name\" content=\"Andy&#039;s blog \u2935\ufe0f\" \/>\n<meta property=\"article:published_time\" content=\"2021-04-01T17:06:33+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-04-01T17:06:34+00:00\" \/>\n<meta name=\"author\" content=\"andy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"andy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/\"},\"author\":{\"name\":\"andy\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"headline\":\"How to use Keycloak Spring Boot adapter and memcached-session-manager together\",\"datePublished\":\"2021-04-01T17:06:33+00:00\",\"dateModified\":\"2021-04-01T17:06:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/\"},\"wordCount\":479,\"commentCount\":0,\"keywords\":[\"Keycloak\",\"memcached\",\"ObjectInputStream\"],\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/\",\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/\",\"name\":\"How to use Keycloak Spring Boot adapter with memcached\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\"},\"datePublished\":\"2021-04-01T17:06:33+00:00\",\"dateModified\":\"2021-04-01T17:06:34+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\"},\"description\":\"either all classes are allowed to be serialized, which creates a security issue, or only the Keycloak Spring Boot adapter classes are allowed\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/2021\\\/04\\\/01\\\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/valerio.nu\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use Keycloak Spring Boot adapter and memcached-session-manager together\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#website\",\"url\":\"https:\\\/\\\/valerio.nu\\\/\",\"name\":\"Andy&#039;s blog \u2935\ufe0f\",\"description\":\"Abandon all hope, ye who enter here\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/valerio.nu\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/valerio.nu\\\/#\\\/schema\\\/person\\\/da69cc3da309893b0d3bc8fcef75f128\",\"name\":\"andy\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g\",\"caption\":\"andy\"},\"sameAs\":[\"https:\\\/\\\/valerio.nu\"],\"url\":\"https:\\\/\\\/valerio.nu\\\/index.php\\\/author\\\/giuseppe\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use Keycloak Spring Boot adapter with memcached","description":"either all classes are allowed to be serialized, which creates a security issue, or only the Keycloak Spring Boot adapter classes are allowed","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:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/","og_locale":"en_US","og_type":"article","og_title":"How to use Keycloak Spring Boot adapter with memcached","og_description":"either all classes are allowed to be serialized, which creates a security issue, or only the Keycloak Spring Boot adapter classes are allowed","og_url":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/","og_site_name":"Andy&#039;s blog \u2935\ufe0f","article_published_time":"2021-04-01T17:06:33+00:00","article_modified_time":"2021-04-01T17:06:34+00:00","author":"andy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"andy","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/#article","isPartOf":{"@id":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/"},"author":{"name":"andy","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"headline":"How to use Keycloak Spring Boot adapter and memcached-session-manager together","datePublished":"2021-04-01T17:06:33+00:00","dateModified":"2021-04-01T17:06:34+00:00","mainEntityOfPage":{"@id":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/"},"wordCount":479,"commentCount":0,"keywords":["Keycloak","memcached","ObjectInputStream"],"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/","url":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/","name":"How to use Keycloak Spring Boot adapter with memcached","isPartOf":{"@id":"https:\/\/valerio.nu\/#website"},"datePublished":"2021-04-01T17:06:33+00:00","dateModified":"2021-04-01T17:06:34+00:00","author":{"@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128"},"description":"either all classes are allowed to be serialized, which creates a security issue, or only the Keycloak Spring Boot adapter classes are allowed","breadcrumb":{"@id":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/valerio.nu\/index.php\/2021\/04\/01\/how-to-use-keycloak-spring-boot-adapter-and-memcached-session-manager-together\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/valerio.nu\/"},{"@type":"ListItem","position":2,"name":"How to use Keycloak Spring Boot adapter and memcached-session-manager together"}]},{"@type":"WebSite","@id":"https:\/\/valerio.nu\/#website","url":"https:\/\/valerio.nu\/","name":"Andy&#039;s blog \u2935\ufe0f","description":"Abandon all hope, ye who enter here","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/valerio.nu\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/valerio.nu\/#\/schema\/person\/da69cc3da309893b0d3bc8fcef75f128","name":"andy","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4852a025c6250c852814d4017f646677730a23c1ec14eb5ca36b69dcce5135a5?s=96&d=mm&r=g","caption":"andy"},"sameAs":["https:\/\/valerio.nu"],"url":"https:\/\/valerio.nu\/index.php\/author\/giuseppe\/"}]}},"_links":{"self":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":4,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":74,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/posts\/70\/revisions\/74"}],"wp:attachment":[{"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/valerio.nu\/index.php\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}