<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Spring &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/spring/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 16:35:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Spring &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Spring Web: Encode &#8216;+&#8217; Value Using UriComponentsBuilder</title>
		<link>https://myshittycode.com/2018/08/13/spring-web-encode-value-using-uricomponentsbuilder/</link>
					<comments>https://myshittycode.com/2018/08/13/spring-web-encode-value-using-uricomponentsbuilder/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 13 Aug 2018 15:18:18 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1085</guid>

					<description><![CDATA[<p>PROBLEM Given the following code&#8230; When using Spring Web 4.3.18.RELEASE, the URL is properly encoded:- However, when using version between 5.0.0.RELEASE and 5.0.7.RELEASE, the URL containing &#8220;+&#8221; value does not get encoded:- SOLUTION There is a ticket opened regarding this breaking change. To properly encode &#8220;+&#8221; value, use 5.0.8.RELEASE or later. Then, ensure encode() is [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2018/08/13/spring-web-encode-value-using-uricomponentsbuilder/">Spring Web: Encode &#8216;+&#8217; Value Using UriComponentsBuilder</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p class="wp-block-paragraph">Given the following code&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
final String url = UriComponentsBuilder.
        fromHttpUrl(&#039;https://server&#039;).
        queryParam(&#039;var&#039;, &#039;{var}&#039;).
        buildAndExpand(&#039;1 2+3&#039;).
        encode().
        toString()
</pre></div>


<p class="wp-block-paragraph"><br>When using Spring Web <b>4.3.18.RELEASE</b>, the URL is properly encoded:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
https://server?var=1%202%2B3
</pre></div>


<p class="wp-block-paragraph"><br>However, when using version between <b>5.0.0.RELEASE</b> and <b>5.0.7.RELEASE</b>, the URL containing &#8220;+&#8221; value does not get encoded:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
https://server?var=1%202+3
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p class="wp-block-paragraph">There is <a href="https://jira.spring.io/browse/SPR-17039" target="_blank" rel="noopener">a ticket</a> opened regarding this breaking change.</p>



<p class="wp-block-paragraph">To properly encode &#8220;+&#8221; value, use <b>5.0.8.RELEASE</b> or later.</p>



<p class="wp-block-paragraph">Then, ensure <code>encode()</code> is invoked before <code>buildAndExpand(..)</code>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [5,6]; title: ; notranslate">
// Produces https://server?var=1%202%2B3
final String url = UriComponentsBuilder.
        fromHttpUrl(&#039;https://server&#039;).
        queryParam(&#039;var&#039;, &#039;{var}&#039;).
        encode().
        buildAndExpand(&#039;1 2+3&#039;).
        toString()
</pre></div>


<p class="wp-block-paragraph"><br>The above code can be further shorten to this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [5]; title: ; notranslate">
// Produces https://server?var=1%202%2B3
final String url = UriComponentsBuilder.
        fromHttpUrl(&#039;https://server&#039;).
        queryParam(&#039;var&#039;, &#039;{var}&#039;).
        build(&#039;1 2+3&#039;).
        toString()
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2018/08/13/spring-web-encode-value-using-uricomponentsbuilder/">Spring Web: Encode &#8216;+&#8217; Value Using UriComponentsBuilder</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2018/08/13/spring-web-encode-value-using-uricomponentsbuilder/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1085</post-id>	</item>
		<item>
		<title>JEE Security: Preventing Clickjacking Attacks</title>
		<link>https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/</link>
					<comments>https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 31 Aug 2017 15:03:05 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Spring Security]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1066</guid>

					<description><![CDATA[<p>PROBLEM Clickjacking is an attack that tricks the users to perform unintended actions&#8230; see OWASP&#8217;s Testing for Clickjacking (OTG-CLIENT-009) SOLUTION To prevent clickjacking attacks, the app must set X-FRAME-OPTIONS header with an appropriate value:- If set correctly, the HTTPS response should show X-FRAME-OPTIONS header:- There are several ways to set this header. Solution 1: Using [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/">JEE Security: Preventing Clickjacking Attacks</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p class="wp-block-paragraph">Clickjacking is an attack that tricks the users to perform unintended actions&#8230; see <a href="https://www.owasp.org/index.php/Testing_for_Clickjacking_(OTG-CLIENT-009)" target="_blank" rel="noopener">OWASP&#8217;s Testing for Clickjacking (OTG-CLIENT-009)</a></p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p class="wp-block-paragraph">To prevent clickjacking attacks, the app must set X-FRAME-OPTIONS header with an appropriate value:-</p>



<ul class="wp-block-list">
<li><b>DENY</b>: this denies any domain using the page as an iFrame source. This is the best option.</li>



<li><b>SAMEORIGIN</b>: this allows pages within the same domain to use other application pages as iFrame sources.</li>



<li><b>ALLOW-FROM [whitelisted domains]</b>: this declares a list of domains that are allowed to include the pages as iFrame sources.</li>
</ul>



<p class="wp-block-paragraph">If set correctly, the HTTPS response should show X-FRAME-OPTIONS header:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [9]; title: ; notranslate">
➜  ~ curl -i -k https://localhost:8443/
HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
X-Application-Context: application:local:8443
Set-Cookie: JSESSIONID=04ADDAF886A20AA561021E869E980BCC; Path=/; Secure; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Content-Length: 631
Date: Thu, 31 Aug 2017 14:56:57 GMT
</pre></div>


<p class="wp-block-paragraph">There are several ways to set this header.</p>



<h3 class="wp-block-heading">Solution 1: Using a servlet filter</h3>



<p class="wp-block-paragraph">You may create a servlet filter that sets X-FRAME-OPTIONS in the response header.</p>



<p class="wp-block-paragraph">Here&#8217;s an example using web.xml-less Spring Boot:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35]; title: ; notranslate">
@SpringBootApplication
class Application extends SpringBootServletInitializer {
    static void main(String&#x5B;] args) {
        SpringApplication.run(Application, args)
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(Application)
    }

    @Bean
    FilterRegistrationBean clickjackingPreventionFilter() {
        return new FilterRegistrationBean(
                urlPatterns: &#x5B;&#039;/**&#039;],
                filter: new Filter() {
                    @Override
                    void init(final FilterConfig filterConfig) throws ServletException {
                    }

                    @Override
                    void doFilter(final ServletRequest servletRequest,
                                  final ServletResponse servletResponse,
                                  final FilterChain filterChain) throws IOException, ServletException {
                        final HttpServletResponse response = (HttpServletResponse) servletResponse
                        response.addHeader(&#039;X-FRAME-OPTIONS&#039;, &#039;DENY&#039;)
                        filterChain.doFilter(servletRequest, servletResponse)
                    }

                    @Override
                    void destroy() {
                    }
                }
        )
    }
}
</pre></div>


<h3 class="wp-block-heading">Solution 2: Using Spring Security</h3>



<p class="wp-block-paragraph">Spring Security provides a very easy way to set the X-FRAME-OPTIONS header:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [7]; title: ; notranslate">
@Configuration
@EnableWebSecurity
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.
                headers().frameOptions().deny().
                and().
                authorizeRequests().
                antMatchers(&#039;/**&#039;).permitAll()
    }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/">JEE Security: Preventing Clickjacking Attacks</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/08/31/jee-security-preventing-clickjacking-attacks/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1066</post-id>	</item>
		<item>
		<title>JEE Security: Disabling HTTP OPTIONS method</title>
		<link>https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/</link>
					<comments>https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 31 Aug 2017 14:37:04 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1062</guid>

					<description><![CDATA[<p>PROBLEM HTTP OPTIONS method is used to provide a list of methods that are supported by the web server. For example, the following shows both GET and HEAD are allowed on the given link:- Enabling OPTIONS may increase the risk of cross-site tracing (XST)&#8230; see OWASP&#8217;s Test HTTP Methods (OTG-CONFIG-006). SOLUTION There are several ways [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/">JEE Security: Disabling HTTP OPTIONS method</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p class="wp-block-paragraph">HTTP OPTIONS method is used to provide a list of methods that are supported by the web server.</p>



<p class="wp-block-paragraph">For example, the following shows both GET and HEAD are allowed on the given link:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [11]; title: ; notranslate">
➜  ~ curl -i -k -X OPTIONS https://localhost:8443/
HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
X-Application-Context: application:local:8443
Allow: GET,HEAD
Content-Length: 0
Date: Thu, 31 Aug 2017 14:07:21 GMT
</pre></div>


<p class="wp-block-paragraph">Enabling OPTIONS may increase the risk of cross-site tracing (XST)&#8230; see <a href="https://www.owasp.org/index.php/Testing_for_HTTP_Methods_and_XST_(OWASP-CM-008)" target="_blank" rel="noopener">OWASP&#8217;s Test HTTP Methods (OTG-CONFIG-006)</a>.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p class="wp-block-paragraph">There are several ways to disable OPTIONS method.</p>



<h3 class="wp-block-heading">Solution 1: Using web.xml</h3>



<p class="wp-block-paragraph">If your app has <b>web.xml</b>, you may add the following snippet:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [5,6,7,8,9,10,11,12,13]; title: ; notranslate">
&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?--&gt;
&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemalocation=&quot;http://xmlns.jcp.org/xml/ns/javaee
		 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot; metadata-complete=&quot;true&quot; version=&quot;3.1&quot;&gt;

    &lt;security-constraint&gt;
        &lt;web-resource-collection&gt;
            &lt;web-resource-name&gt;restricted methods&lt;/web-resource-name&gt;
            &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
            &lt;http-method&gt;OPTIONS&lt;/http-method&gt;
        &lt;/web-resource-collection&gt;
        &lt;auth-constraint&gt;
        &lt;/auth-constraint&gt;
    &lt;/security-constraint&gt;

    &lt;!-- Other configurations --&gt;
&lt;/web-app&gt;
</pre></div>


<h3 class="wp-block-heading">Solution 2: Using Spring Boot</h3>



<p class="wp-block-paragraph">If you are using Spring Boot, there isn&#8217;t any option to mimic the above configuration programmatically.</p>



<p class="wp-block-paragraph">However, you still can use <b>web.xml</b> in conjunction with Spring Boot by setting <b>metadata-complete</b> to <b>false</b> and use servlet version 3.0 or higher:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [3]; title: ; notranslate">
&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?--&gt;
&lt;web-app xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemalocation=&quot;http://xmlns.jcp.org/xml/ns/javaee
		 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd&quot; metadata-complete=&quot;false&quot; version=&quot;3.1&quot;&gt;

    &lt;security-constraint&gt;
        &lt;web-resource-collection&gt;
            &lt;web-resource-name&gt;restricted methods&lt;/web-resource-name&gt;
            &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
            &lt;http-method&gt;OPTIONS&lt;/http-method&gt;
        &lt;/web-resource-collection&gt;
        &lt;auth-constraint&gt;
    &lt;/auth-constraint&gt;&lt;/security-constraint&gt;
&lt;/web-app&gt;
</pre></div>


<h3 class="wp-block-heading">Solution 3: Using Spring Security</h3>



<p class="wp-block-paragraph">If you don&#8217;t want to use <b>web.xml</b>, you may configure Spring Security to disable OPTIONS method on all URIs:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [7]; title: ; notranslate">
@Configuration
@EnableWebSecurity
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests().
                antMatchers(HttpMethod.OPTIONS, &#039;/**&#039;).denyAll().
                antMatchers(&#039;/**&#039;).permitAll()
    }
}
</pre></div>


<p class="wp-block-paragraph">Now, when trying to hit the same link with OPTIONS method, the app will return 403 Forbidden:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; highlight: [2]; title: ; notranslate">
➜  ~ curl -i -k -X OPTIONS https://localhost:8443/
HTTP/1.1 403
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
Strict-Transport-Security: max-age=31536000 ; includeSubDomains
X-Frame-Options: DENY
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 31 Aug 2017 14:26:51 GMT
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/">JEE Security: Disabling HTTP OPTIONS method</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/08/31/jee-security-disabling-http-options-method/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1062</post-id>	</item>
		<item>
		<title>Spring + Ehcache: XML-less Spring Configuration for Ehcache 2.x vs Ehcache 3.x</title>
		<link>https://myshittycode.com/2017/04/11/spring-ehcache-xml-less-spring-configuration-for-ehcache-2-x-vs-ehcache-3-x/</link>
					<comments>https://myshittycode.com/2017/04/11/spring-ehcache-xml-less-spring-configuration-for-ehcache-2-x-vs-ehcache-3-x/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 12 Apr 2017 01:55:59 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Ehcache]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=958</guid>

					<description><![CDATA[<p>BACKGROUND The documentation on the web regarding Ehcache 3.x configuration using Spring is rather lacking. There is apparently a very distinct difference in Spring Java-based configuration between Ehcache 2.x vs Ehcache 3.x. Spring + Ehcache 2.x Dependency:- Spring configuration:- Spring + Ehcache 3.x Dependency:- Spring configuration:-</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/04/11/spring-ehcache-xml-less-spring-configuration-for-ehcache-2-x-vs-ehcache-3-x/">Spring + Ehcache: XML-less Spring Configuration for Ehcache 2.x vs Ehcache 3.x</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">BACKGROUND</h2>



<p class="wp-block-paragraph">The documentation on the web regarding Ehcache 3.x configuration using Spring is rather lacking. There is apparently a very distinct difference in Spring Java-based configuration between Ehcache 2.x vs Ehcache 3.x.</p>



<h2 class="wp-block-heading">Spring + Ehcache 2.x</h2>



<p class="wp-block-paragraph">Dependency:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupid&gt;net.sf.ehcache&lt;/groupid&gt;
    &lt;artifactid&gt;ehcache&lt;/artifactid&gt;
    &lt;version&gt;2.10.3&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<p class="wp-block-paragraph">Spring configuration:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
@Configuration
@EnableCaching
class Config {
    @Bean
    CacheManager cacheManager() {
        return new EhCacheCacheManager(ehCacheManager())
    }

    @Bean(destroyMethod = &#039;shutdown&#039;)
    net.sf.ehcache.CacheManager ehCacheManager() {
        CacheConfiguration cacheConfiguration = new CacheConfiguration(
                name: &#039;person&#039;,
                maxEntriesLocalHeap: 5,
                timeToLiveSeconds: 5
        )

        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration()
        config.addCache(cacheConfiguration)

        return new net.sf.ehcache.CacheManager(config)
    }
}
</pre></div>


<h2 class="wp-block-heading">Spring + Ehcache 3.x</h2>



<p class="wp-block-paragraph">Dependency:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupid&gt;org.ehcache&lt;/groupid&gt;
    &lt;artifactid&gt;ehcache&lt;/artifactid&gt;
    &lt;version&gt;3.3.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;javax.cache&lt;/groupid&gt;
    &lt;artifactid&gt;cache-api&lt;/artifactid&gt;
    &lt;version&gt;1.0.0&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<p class="wp-block-paragraph">Spring configuration:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
import org.ehcache.config.CacheConfiguration
import org.ehcache.config.builders.CacheConfigurationBuilder
import org.ehcache.config.builders.ResourcePoolsBuilder
import org.ehcache.core.config.DefaultConfiguration
import org.ehcache.expiry.Duration
import org.ehcache.expiry.Expirations
import org.ehcache.jsr107.EhcacheCachingProvider
import org.springframework.cache.annotation.EnableCaching
import org.springframework.cache.jcache.JCacheCacheManager
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

import javax.cache.CacheManager
import javax.cache.Caching
import java.util.concurrent.TimeUnit

@Configuration
@EnableCaching
class Config {
    @Bean
    JCacheCacheManager jCacheCacheManager() {
        return new JCacheCacheManager(cacheManager())
    }

    @Bean(destroyMethod = &#039;close&#039;)
    CacheManager cacheManager() {
        CacheConfiguration cacheConfiguration = CacheConfigurationBuilder.newCacheConfigurationBuilder(
                Object,
                Object,
                ResourcePoolsBuilder.heap(5)).
                withExpiry(Expirations.timeToLiveExpiration(new Duration(5, TimeUnit.SECONDS))).
                build()

        Map&lt;string, cacheconfiguration=&quot;&quot;&gt; caches = &#x5B;&#039;person&#039;: cacheConfiguration]

        EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider()
        DefaultConfiguration configuration = new DefaultConfiguration(caches, provider.getDefaultClassLoader())

        return provider.getCacheManager(provider.getDefaultURI(), configuration)
    }
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/04/11/spring-ehcache-xml-less-spring-configuration-for-ehcache-2-x-vs-ehcache-3-x/">Spring + Ehcache: XML-less Spring Configuration for Ehcache 2.x vs Ehcache 3.x</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/04/11/spring-ehcache-xml-less-spring-configuration-for-ehcache-2-x-vs-ehcache-3-x/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">958</post-id>	</item>
		<item>
		<title>LdapTemplate: javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name &#8216;&#8230;&#8217;</title>
		<link>https://myshittycode.com/2017/03/26/ldaptemplate-javax-naming-partialresultexception-unprocessed-continuation-references-remaining-name/</link>
					<comments>https://myshittycode.com/2017/03/26/ldaptemplate-javax-naming-partialresultexception-unprocessed-continuation-references-remaining-name/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Sun, 26 Mar 2017 21:05:59 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=928</guid>

					<description><![CDATA[<p>BACKGROUND Let&#8217;s assume we have the following LDAP configuration&#8230; When running any LDAP query, the following exception is thrown:- SOLUTION There are 3 solutions to this problem. Query against Gobal Catalog To prevent the referral issues when dealing with Active Directory, we may query against the Global Catalog by using port 3268. The possible downside [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/03/26/ldaptemplate-javax-naming-partialresultexception-unprocessed-continuation-references-remaining-name/">LdapTemplate: javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name &#8216;&#8230;&#8217;</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">BACKGROUND</h2>



<p class="wp-block-paragraph">Let&#8217;s assume we have the following LDAP configuration&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
@Configuration
class LdapConfig {
    @Bean
    AuthenticationSource getAuthenticationSource(AppConfigService appConfigService) {
        return new AuthenticationSource() { ... }
    }

    @Bean
    ContextSource contextSource(AuthenticationSource authenticationSource) {
        return new LdapContextSource(
                authenticationSource: authenticationSource,
                url: &#039;ldap://server:389&#039;,
                base: &#039;dc=domain&#039;
        )
    }

    @Bean
    LdapTemplate getLdapTemplate(ContextSource contextSource) {
        return new LdapTemplate(contextSource: contextSource)
    }
}
</pre></div>


<p class="wp-block-paragraph">When running any LDAP query, the following exception is thrown:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Caused by: javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name &#039;/&#039;
	at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2846)
	at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2820)
	at com.sun.jndi.ldap.LdapNamingEnumeration.getNextBatch(LdapNamingEnumeration.java:129)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(LdapNamingEnumeration.java:198)
	at com.sun.jndi.ldap.LdapNamingEnumeration.hasMore(LdapNamingEnumeration.java:171)
	at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:365)
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p class="wp-block-paragraph">There are 3 solutions to this problem.</p>



<h3 class="wp-block-heading">Query against Gobal Catalog</h3>



<p class="wp-block-paragraph">To prevent the referral issues when dealing with Active Directory, we may query against the Global Catalog by using port 3268.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [5]; title: ; notranslate">
@Bean
ContextSource contextSource(AuthenticationSource authenticationSource) {
    return new LdapContextSource(
            authenticationSource: authenticationSource,
            url: &#039;ldap://server:3268&#039;,
            base: &#039;dc=domain&#039;
    )
}
</pre></div>


<p class="wp-block-paragraph">The possible downside to this approach is the Global Catalog may not have the pertinent data we need, such as employeeID, etc.</p>



<h3 class="wp-block-heading">Configure Referral to Follow</h3>



<p class="wp-block-paragraph">We can configure LdapTemplate to automatically follow any referrals.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [7]; title: ; notranslate">
@Bean
ContextSource contextSource(AuthenticationSource authenticationSource) {
    return new LdapContextSource(
            authenticationSource: authenticationSource,
            url: &#039;ldap://server:389&#039;,
            base: &#039;dc=domain&#039;,
            referral: &#039;follow&#039;
    )
}
</pre></div>


<p class="wp-block-paragraph">The downside to this approach is it makes the query much slower. Based on my testing, it is at least 5 to 10 seconds slower.</p>



<h3 class="wp-block-heading">Ignore Exception</h3>



<p class="wp-block-paragraph">Sometimes, it pays to read the JavaDoc. <a href="http://docs.spring.io/spring-ldap/docs/current/apidocs/org/springframework/ldap/core/LdapTemplate.html" target="_blank" rel="noopener">Based on the LdapTemplate&#8217;s documentation</a>, it says&#8230;</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">Note for Active Directory (AD) users: AD servers are apparently unable to handle referrals automatically, which causes a PartialResultException to be thrown whenever a referral is encountered in a search. To avoid this, set the ignorePartialResultException property to true. There is currently no way of manually handling these referrals in the form of ReferralException, i.e. either you get the exception (and your results are lost) or all referrals are ignored (if the server is unable to handle them properly. Neither is there any simple way to get notified that a PartialResultException has been ignored (other than in the log).</p>
</blockquote>



<p class="wp-block-paragraph">Bada Bing, Bada Boom&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; highlight: [5]; title: ; notranslate">
@Bean
LdapTemplate getLdapTemplate(ContextSource contextSource) {
    return new LdapTemplate(
            contextSource: contextSource,
            ignorePartialResultException: true
    )
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/03/26/ldaptemplate-javax-naming-partialresultexception-unprocessed-continuation-references-remaining-name/">LdapTemplate: javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name &#8216;&#8230;&#8217;</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/03/26/ldaptemplate-javax-naming-partialresultexception-unprocessed-continuation-references-remaining-name/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">928</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-07-04 21:14:54 by W3 Total Cache
-->