<?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>SiteMesh &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/sitemesh/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 17:01:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>SiteMesh &#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 Security: Handling 403 Error Page</title>
		<link>https://myshittycode.com/2014/04/11/spring-security-handling-403-error-page/</link>
					<comments>https://myshittycode.com/2014/04/11/spring-security-handling-403-error-page/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Fri, 11 Apr 2014 14:58:31 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[SiteMesh]]></category>
		<category><![CDATA[Spring MVC]]></category>
		<category><![CDATA[Spring Security]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=480</guid>

					<description><![CDATA[<p>If you are already using Spring, then you might want to use Spring Security to secure your web resources. To do that, we specify the URI to be secured with &#60;security:intercept-url/&#62; tag:- When users without role ROLE_TOPSECRET access /top-secrets/kfc-secret, they will see this default error page:- This proves that Spring Security is doing its job. [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/04/11/spring-security-handling-403-error-page/">Spring Security: Handling 403 Error Page</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you are already using Spring, then you might want to use Spring Security to secure your web resources.</p>



<p>To do that, we specify the URI to be secured with <code>&lt;security:intercept-url/&gt;</code> tag:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [8]; title: ; notranslate">
&lt;beans ...&gt;
    &lt;!-- Error pages don&#039;t need to be secured --&gt;
    &lt;security:http pattern=&quot;/error/**&quot; security=&quot;none&quot;/&gt;

    &lt;security:http auto-config=&quot;true&quot;&gt;
        &lt;security:form-login ... /&gt;
        &lt;security:logout ... /&gt;
        &lt;security:intercept-url pattern=&quot;/top-secrets/**&quot; access=&quot;ROLE_TOPSECRET&quot;/&gt;
    &lt;/security:http&gt;
	...
&lt;/beans&gt;
</pre></div>


<p>When users without role <code>ROLE_TOPSECRET</code> access <code>/top-secrets/kfc-secret</code>, they will see this default error page:-</p>



<figure class="wp-block-image aligncenter"><img decoding="async" src="http://myshittycode.files.wordpress.com/2014/04/screen-shot-2014-04-11-at-8-47-53-am.png" alt="" class="wp-image-484"/></figure>



<p>This proves that Spring Security is doing its job. However, the default error page looks rather F.U.G.L.Y. Further, the error page may reveal too much information about the application server. The above error page shows the application runs on Jetty. If I&#8217;m a motherhacker, I would research all the possible vulnerabilities on this particular application server in attempt to hack it.</p>



<p>A better solution is to provide a friendly error page when the user access is denied. This can be done by specifying <code>&lt;security:access-denied-handler/&gt;</code> tag:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [8]; title: ; notranslate">
&lt;beans ...&gt;
    &lt;!-- Error pages don&#039;t need to be secured --&gt;
    &lt;security:http pattern=&quot;/error/**&quot; security=&quot;none&quot;/&gt;

    &lt;security:http auto-config=&quot;true&quot;&gt;
        &lt;security:form-login ... /&gt;
        &lt;security:logout ... /&gt;
        &lt;security:access-denied-handler error-page=&quot;/error/access-denied&quot;/&gt;
        &lt;security:intercept-url pattern=&quot;/top-secrets/**&quot; access=&quot;ROLE_TOPSECRET&quot;/&gt;
    &lt;/security:http&gt;
	...
&lt;/beans&gt;
</pre></div>


<p>Then, we create a simple error controller that returns the error page:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Controller
@RequestMapping(value = &quot;/error&quot;)
public class ErrorController {
    @RequestMapping(value = &quot;/access-denied&quot;, method = RequestMethod.GET)
    public String accessDenied() {
        return &quot;error-access-denied&quot;;
    }
}
</pre></div>


<p>Now, the user will see this custom error page:-</p>



<figure class="wp-block-image aligncenter"><img fetchpriority="high" decoding="async" width="721" height="205" src="https://myshittycode.com/wp-content/uploads/2014/04/screen-shot-2014-04-11-at-8-38-16-am-1.png?x45560" alt="" class="wp-image-483" srcset="https://myshittycode.com/wp-content/uploads/2014/04/screen-shot-2014-04-11-at-8-38-16-am-1.png 721w, https://myshittycode.com/wp-content/uploads/2014/04/screen-shot-2014-04-11-at-8-38-16-am-1-300x85.png 300w" sizes="(max-width: 721px) 100vw, 721px" /></figure>



<p>This solution is better than the previous one. However, SiteMesh doesn&#8217;t have the opportunity to decorate this error page before it gets rendered.</p>



<p>To fix this, we can create a simple redirect to allow the request to make a full-round trip to the server so that SiteMesh can decorate the error page:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [4,5,6,7]; title: ; notranslate">
@Controller
@RequestMapping(value = &quot;/error&quot;)
public class ErrorController {
    @RequestMapping(value = &quot;/router&quot;, method = RequestMethod.GET)
    public String errorRouter(@RequestParam(&quot;q&quot;) String resource) {
        return &quot;redirect:/error/&quot; + resource;
    }&lt;/code&gt;
&lt;code&gt;
&lt;/code&gt;
&lt;code&gt;    @RequestMapping(value = &quot;/access-denied&quot;, method = RequestMethod.GET)
    public String accessDenied() {
        return &quot;error-access-denied&quot;;
    }
}

</pre></div>


<p>Then, we tweak the Spring Security to use the error router URI:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [8]; title: ; notranslate">
&lt;beans ...&gt;
    &lt;!-- Error pages don&#039;t need to be secured --&gt;
    &lt;security:http pattern=&quot;/error/**&quot; security=&quot;none&quot;/&gt;&lt;/code&gt;
&lt;code&gt;

    &lt;security:http auto-config=&quot;true&quot;&gt;
        &lt;security:form-login ... /&gt;
        &lt;security:logout ... /&gt;
        &lt;security:access-denied-handler error-page=&quot;/error/router?q=access-denied&quot;/&gt;
        &lt;security:intercept-url pattern=&quot;/top-secrets/**&quot; access=&quot;ROLE_TOPSECRET&quot;/&gt;
    &lt;/security:http&gt;
&lt;/code&gt;
&lt;code&gt;		...
&lt;/beans&gt;

</pre></div>


<p>Now, the user will see this nice beautiful error page:-</p>



<figure class="wp-block-image aligncenter"><img decoding="async" width="845" height="290" src="https://myshittycode.com/wp-content/uploads/2014/04/screen_shot_2014-04-11_at_8_36_30_am-1.png?x45560" alt="" class="wp-image-485" srcset="https://myshittycode.com/wp-content/uploads/2014/04/screen_shot_2014-04-11_at_8_36_30_am-1.png 845w, https://myshittycode.com/wp-content/uploads/2014/04/screen_shot_2014-04-11_at_8_36_30_am-1-300x103.png 300w, https://myshittycode.com/wp-content/uploads/2014/04/screen_shot_2014-04-11_at_8_36_30_am-1-768x264.png 768w" sizes="(max-width: 845px) 100vw, 845px" /></figure>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/04/11/spring-security-handling-403-error-page/">Spring Security: Handling 403 Error Page</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/04/11/spring-security-handling-403-error-page/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">480</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 58/71 queries in 0.021 seconds using Disk

Served from: myshittycode.com @ 2026-02-18 04:19:48 by W3 Total Cache
-->