<?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 Boot &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/spring-boot/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:25:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Spring Boot &#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>Feign + Eureka: UnknownHostException when Attempting to Invoke a Service</title>
		<link>https://myshittycode.com/2019/04/30/feign-eureka-unknownhostexception-when-attempting-to-invoke-a-service/</link>
					<comments>https://myshittycode.com/2019/04/30/feign-eureka-unknownhostexception-when-attempting-to-invoke-a-service/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 01 May 2019 01:52:09 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<category><![CDATA[Spring Cloud Netflix Eureka]]></category>
		<category><![CDATA[Spring Cloud Netflix Ribbon]]></category>
		<category><![CDATA[Spring Cloud Open Feign]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1104</guid>

					<description><![CDATA[<p>PROBLEM When attempting use Feign to invoke a service through Eureka, the following exception occurs:- SOLUTION Go to the donkey-kong-service app and add the following line to the application.properties:- Now, when Feign tries to invoke the service, the IP address will be used instead of the OS&#8217; reported hostname.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/30/feign-eureka-unknownhostexception-when-attempting-to-invoke-a-service/">Feign + Eureka: UnknownHostException when Attempting to Invoke a Service</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">When attempting use Feign to invoke a service through Eureka, the following exception occurs:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
MY-HOST-NAME executing GET http://donkey-kong-service/throw/barrels/10
feign.RetryableException: MY-HOST-NAME executing GET http://donkey-kong-service/throw/barrels/10
	at feign.FeignException.errorExecuting(FeignException.java:84)
	at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:113)
	...
Caused by: java.net.UnknownHostException: MY-HOST-NAME
		at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
</pre></div>


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



<p class="wp-block-paragraph">Go to the <b>donkey-kong-service</b> app and add the following line to the <code>application.properties</code>:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
eureka.instance.prefer-ip-address=true
</pre></div>


<p class="wp-block-paragraph">Now, when Feign tries to invoke the service, the IP address will be used instead of the OS&#8217; reported hostname.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/30/feign-eureka-unknownhostexception-when-attempting-to-invoke-a-service/">Feign + Eureka: UnknownHostException when Attempting to Invoke a Service</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2019/04/30/feign-eureka-unknownhostexception-when-attempting-to-invoke-a-service/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1104</post-id>	</item>
		<item>
		<title>Spring Boot: Restarting App using Dev Tools with IntelliJ IDEA</title>
		<link>https://myshittycode.com/2019/04/26/spring-boot-restarting-app-using-dev-tools-in-intellij-idea/</link>
					<comments>https://myshittycode.com/2019/04/26/spring-boot-restarting-app-using-dev-tools-in-intellij-idea/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Fri, 26 Apr 2019 17:47:44 +0000</pubDate>
				<category><![CDATA[Development Tools]]></category>
		<category><![CDATA[IntelliJ IDEA]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1096</guid>

					<description><![CDATA[<p>Spring Boot provides spring-boot-devtools module that allows the app to &#8220;smartly&#8221; restart whenever the files on the classpath have changed. Because the rarely changed classes (ex: 3rd party JARs) are separated out into a different classloader from the app&#8217;s actively developed classes&#8217; classloader, it allows Spring Boot to quickly restart the app compared to &#8220;cold [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/26/spring-boot-restarting-app-using-dev-tools-in-intellij-idea/">Spring Boot: Restarting App using Dev Tools with IntelliJ IDEA</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Spring Boot provides <b>spring-boot-devtools</b> module that allows the app to &#8220;smartly&#8221; restart whenever the files on the classpath have changed.</p>



<p class="wp-block-paragraph">Because the rarely changed classes (ex: 3rd party JARs) are separated out into a different classloader from the app&#8217;s actively developed classes&#8217; classloader, it allows Spring Boot to quickly restart the app compared to &#8220;cold start&#8221;.</p>



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



<p class="wp-block-paragraph">First, add the following dependency:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
	&lt;groupid&gt;org.springframework.boot&lt;/groupid&gt;
	&lt;artifactid&gt;spring-boot-devtools&lt;/artifactid&gt;
	&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
</pre></div>


<h2 class="wp-block-heading">CONFIGURING INTELLIJ IDEA</h2>



<p class="wp-block-paragraph">In IntelliJ IDEA:-</p>



<ul class="wp-block-list">
<li>Click <b>SHIFT</b> twice to bring up the &#8220;Search History&#8221; dialog.</li>



<li>Select &#8220;Actions&#8221; tab.</li>



<li>Type &#8220;Registry&#8221; in the search box.</li>



<li>Select &#8220;Registry&#8230;&#8221;.</li>
</ul>



<figure class="wp-block-image"><img fetchpriority="high" decoding="async" width="1446" height="544" src="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_22_30_pm-1.png?x45560" alt="" class="wp-image-1097" srcset="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_22_30_pm-1.png 1446w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_22_30_pm-1-300x113.png 300w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_22_30_pm-1-1024x385.png 1024w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_22_30_pm-1-768x289.png 768w" sizes="(max-width: 1446px) 100vw, 1446px" /></figure>



<p class="wp-block-paragraph">In the &#8220;Registry&#8221; dialog:-</p>



<ul class="wp-block-list">
<li>Find &#8220;compiler.automake.allow.when.app.running&#8221; key.</li>



<li>Check the checkbox.</li>



<li>Close the dialog.</li>
</ul>



<figure class="wp-block-image"><img decoding="async" width="1358" height="1244" src="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_26_04_pm-1.png?x45560" alt="" class="wp-image-1098" srcset="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_26_04_pm-1.png 1358w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_26_04_pm-1-300x275.png 300w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_26_04_pm-1-1024x938.png 1024w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_26_04_pm-1-768x704.png 768w" sizes="(max-width: 1358px) 100vw, 1358px" /></figure>



<p class="wp-block-paragraph">In IntelliJ IDEA &#8220;Preferences&#8221; dialog:-</p>



<ul class="wp-block-list">
<li>Go to &#8220;Build, Execution, Deployment&#8221; » &#8220;Compiler&#8221;.</li>



<li>Check &#8220;Build project automatically&#8221;.</li>



<li>Close the dialog.</li>
</ul>



<figure class="wp-block-image"><img decoding="async" width="2126" height="1368" src="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_28_40_pm-1.png?x45560" alt="" class="wp-image-1099" srcset="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_28_40_pm-1.png 2126w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_28_40_pm-1-300x193.png 300w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_28_40_pm-1-1024x659.png 1024w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_28_40_pm-1-768x494.png 768w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_28_40_pm-1-1536x988.png 1536w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_28_40_pm-1-2048x1318.png 2048w" sizes="(max-width: 2126px) 100vw, 2126px" /></figure>



<p class="wp-block-paragraph">Finally, instead of running Maven goals to run the Spring Boot app, select the Application class (annotated with <b>@SpringBootApplication</b>) and run it from IntelliJ IDEA.</p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="476" height="102" src="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_31_18_pm-1.png?x45560" alt="" class="wp-image-1100" srcset="https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_31_18_pm-1.png 476w, https://myshittycode.com/wp-content/uploads/2019/04/screen_shot_2019-04-26_at_12_31_18_pm-1-300x64.png 300w" sizes="auto, (max-width: 476px) 100vw, 476px" /></figure>



<p class="wp-block-paragraph">Anytime the app&#8217;s class files have changed, IntelliJ IDEA will compile the app, which will then trigger Spring Boot Dev Tools to restart the app.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/26/spring-boot-restarting-app-using-dev-tools-in-intellij-idea/">Spring Boot: Restarting App using Dev Tools with IntelliJ IDEA</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2019/04/26/spring-boot-restarting-app-using-dev-tools-in-intellij-idea/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1096</post-id>	</item>
		<item>
		<title>Spring Boot: Connecting to IBM MQ over JMS using non-IBM JRE</title>
		<link>https://myshittycode.com/2019/04/23/spring-boot-connecting-to-ibm-mq-over-jms-using-non-ibm-jre/</link>
					<comments>https://myshittycode.com/2019/04/23/spring-boot-connecting-to-ibm-mq-over-jms-using-non-ibm-jre/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Wed, 24 Apr 2019 01:44:44 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[IBM MQ]]></category>
		<category><![CDATA[JMS]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1092</guid>

					<description><![CDATA[<p>There are several ways to connect to IBM MQ:- This article shows you how to connect with Spring&#8217;s JmsTemplate. CONNECTIVITY INFO Typically, the MQ admin will provide the following connectivity info that allows you to connect to MQ:- DEPENDENCY Add the following dependency:- SPRING CONFIGURATION While the connectivity info can be hardcoded in Spring Boot&#8217;s [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/23/spring-boot-connecting-to-ibm-mq-over-jms-using-non-ibm-jre/">Spring Boot: Connecting to IBM MQ over JMS using non-IBM JRE</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">There are several ways to connect to IBM MQ:-</p>



<ul class="wp-block-list">
<li><code>com.ibm.mq.MQQueueManager</code></li>



<li><code>com.ibm.mq.jms.MQQueueConnectionFactory</code></li>



<li><code>com.ibm.msg.client.jms.JmsConnectionFactory</code></li>
</ul>



<p class="wp-block-paragraph">This article shows you how to connect with Spring&#8217;s <code>JmsTemplate</code>.</p>



<h2 class="wp-block-heading">CONNECTIVITY INFO</h2>



<p class="wp-block-paragraph">Typically, the MQ admin will provide the following connectivity info that allows you to connect to MQ:-</p>



<ul class="wp-block-list">
<li>Queue manager name, ex: <code>MY.QUEUE.MANAGER</code></li>



<li>Host name, ex: <code>server.com</code></li>



<li>Port, ex: <code>1415</code></li>



<li>Channel name, ex: <code>MY.SSL.CHANNEL</code></li>



<li>SSL Cipher Suite, ex: <code>SSL_ECDHE_RSA_WITH_AES_128_GCM_SHA256</code></li>



<li>User, ex: <code>user</code></li>



<li>Password, ex: <code>password</code></li>



<li>Queue name, ex: <code>MY.QUEUE</code></li>
</ul>



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



<p class="wp-block-paragraph">Add the following dependency:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
dependencies {
  compile &#039;com.ibm.mq:mq-jms-spring-boot-starter:2.1.1&#039;
}
</pre></div>


<h2 class="wp-block-heading">SPRING CONFIGURATION</h2>



<p class="wp-block-paragraph">While the connectivity info can be hardcoded in Spring Boot&#8217;s <code>application.properties</code>, it&#8217;s probably more logical to use Spring <code>@Configuration</code> to dynamically set the values especially dealing with credential.</p>



<p class="wp-block-paragraph">So, create a Spring configuration that looks something like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
// while all the value are hardcoded here for simplicity sake, you can inject
// sensitive values from DB, through &lt;code&gt;Environment&lt;/code&gt;, etc.
@EnableJms
@Configuration
class JmsConfig {
  // Adding @Primary will force Spring Boot to use this bean
  // instead of the one provided by the dependency
  @Primary
  @Bean
  MQConfigurationProperties mqConfigurationProperties() {
    System.setProperty(&#039;javax.net.ssl.keyStore&#039;, &#039;/path/to/keystore.jks&#039;)
    System.setProperty(&#039;javax.net.ssl.keyStorePassword&#039;, &#039;XXXXXXX&#039;)

    return new MQConfigurationProperties(
      queueManager: &#039;MY.QUEUE.MANAGER&#039;,
      channel: &#039;MY.SSL.CHANNEL&#039;,
      connName: &#039;server.com(1415)&#039;,
      user: &#039;user&#039;,
      password: &#039;password&#039;,
      userAuthenticationMQCSP: true,

      // If the provided SSL cipher suite begins with &quot;SSL&quot;,
      // replace it with &quot;TLS&quot; instead.
      // SSL_* is IBM JRE CipherSuite name.
      // TLS_* is Oracle JRE CipherSuite name.
      sslCipherSuite: &#039;TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256&#039;,

      // true - if using IBM JRE
      // false - if using non-IBM JRE, ex: Oracle, OpenJDK, etc
      useIBMCipherMappings: false
    )
  }
}
</pre></div>


<h2 class="wp-block-heading">USING JMS TEMPLATE</h2>



<p class="wp-block-paragraph">Finally, to listen to the given queue, it is as easy as autowiring <code>JmsTemplate</code> and start using it.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: groovy; title: ; notranslate">
@Autowired
JmsTemplate jmsTemplate

...

final Message message = jmsTemplate.receive(&#039;MY.QUEUE&#039;)
println message
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2019/04/23/spring-boot-connecting-to-ibm-mq-over-jms-using-non-ibm-jre/">Spring Boot: Connecting to IBM MQ over JMS using non-IBM JRE</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2019/04/23/spring-boot-connecting-to-ibm-mq-over-jms-using-non-ibm-jre/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1092</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>
	</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-24 03:21:04 by W3 Total Cache
-->