<?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>Armbruster IT Blog &#187; tip</title>
	<atom:link href="http://blog.armbruster-it.de/tag/tip/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.armbruster-it.de</link>
	<description>Real world fun with Java, Grails, Groovy, Zope, Plone, Linux and much others.</description>
	<lastBuildDate>Tue, 11 Oct 2011 11:42:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>restrict a Grails controller to localhost access only</title>
		<link>http://blog.armbruster-it.de/2010/04/restrict-a-grails-controller-to-localhost-access-only/</link>
		<comments>http://blog.armbruster-it.de/2010/04/restrict-a-grails-controller-to-localhost-access-only/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 13:47:38 +0000</pubDate>
		<dc:creator>Stefan Armbruster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://blog.armbruster-it.de/?p=137</guid>
		<description><![CDATA[A common requirement for many web applications is that some parts (aka controllers) should only be accessible from specifc ip addresses. Typically controllers doing some administrative or maintenance work must be protected from non-authroized access. The most complete solution for this is using a full blown security framework like the Grails Acegi plugin. But there&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>A common requirement for many web applications is that some parts (aka controllers) should only be accessible from specifc ip addresses. Typically controllers doing some administrative or maintenance work must be protected from non-authroized access. The most complete solution for this is using a full blown security framework like the <a href="http://www.grails.org/plugin/acegi">Grails Acegi plugin</a>. But there&#8217;s also a lean and quick solution for this in Grails: use a <a href="http://www.grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.5%20Controller%20Interceptors">controller interceptor</a>:</p>

<div class="wp_syntax"><div class="wp_syntax_download"><div class="wp_syntax_download_filename"><a name="file-def%20beforeInterceptor%20=%20%7B%0D%0A%20%20%20if%20(!%5B%22127.0.0.1%22,%20%220:0:0:0:0:0:0:1%22%5D.contains(request.remoteAddr))%20%7B%0D%0A%20%20%20%20%20%20render(status:%20401,%20text:%20'Access%20limited%20to%20localhost')%0D%0A%20%20%20%20%20%20return%20false%0D%0A%20%20%20%7D%0D%0A%7D" href="#file-def+beforeInterceptor+%3D+%7B%0D%0A+++if+%28%21%5B%22127.0.0.1%22%2C+%220%3A0%3A0%3A0%3A0%3A0%3A0%3A1%22%5D.contains%28request.remoteAddr%29%29+%7B%0D%0A++++++render%28status%3A+401%2C+text%3A+%27Access+limited+to+localhost%27%29%0D%0A++++++return+false%0D%0A+++%7D%0D%0A%7D" title="def beforeInterceptor = {
   if (![&quot;127.0.0.1&quot;, &quot;0:0:0:0:0:0:0:1&quot;].contains(request.remoteAddr)) {
      render(status: 401, text: 'Access limited to localhost')
      return false
   }
}">def beforeInterceptor = {
   if (!["127.0.0.1", "0:0:0:0:0:0:0:1"].contains(request.remoteAddr)) {
      render(status: 401, text: 'Access limited to localhost')
      return false
   }
}</a></div><div class="wp_syntax_download_actions"><a href="http://blog.armbruster-it.de/wp-content/plugins/wp-syntax-download-extension/wp-syntax-download-extension.php/137/def%20beforeInterceptor%20=%20%7B%0D%0A%20%20%20if%20(!%5B%22127.0.0.1%22,%20%220:0:0:0:0:0:0:1%22%5D.contains(request.remoteAddr))%20%7B%0D%0A%20%20%20%20%20%20render(status:%20401,%20text:%20'Access%20limited%20to%20localhost')%0D%0A%20%20%20%20%20%20return%20false%0D%0A%20%20%20%7D%0D%0A%7D">raw</a> <a href="http://blog.armbruster-it.de/wp-content/plugins/wp-syntax-download-extension/wp-syntax-download-extension.php/137/download/def%20beforeInterceptor%20=%20%7B%0D%0A%20%20%20if%20(!%5B%22127.0.0.1%22,%20%220:0:0:0:0:0:0:1%22%5D.contains(request.remoteAddr))%20%7B%0D%0A%20%20%20%20%20%20render(status:%20401,%20text:%20'Access%20limited%20to%20localhost')%0D%0A%20%20%20%20%20%20return%20false%0D%0A%20%20%20%7D%0D%0A%7D">download</a></div></div><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> beforeInterceptor <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">{</span>
   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">(</span><span style="color: #66cc66;">!</span><span style="color: #66cc66;">[</span><span style="color: #ff0000;">"127.0.0.1"</span>, <span style="color: #ff0000;">"0:0:0:0:0:0:0:1"</span><span style="color: #66cc66;">]</span>.<span style="color: #CC0099;">contains</span><span style="color: #66cc66;">(</span>request.<span style="color: #006600;">remoteAddr</span><span style="color: #66cc66;">)</span><span style="color: #66cc66;">)</span> <span style="color: #66cc66;">{</span>
      render<span style="color: #66cc66;">(</span>status: <span style="color: #cc66cc;">401</span>, text: <span style="color: #ff0000;">'Access limited to localhost'</span><span style="color: #66cc66;">)</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>
   <span style="color: #66cc66;">}</span>
<span style="color: #66cc66;">}</span></pre></div></div>

<p>Grails calls the beforeIntereceptor closure prior every action in a controller. Only if it returns true, the action is executed. In the code above if the client has a non-local IP address, a 401 error is returned with an error message. Note that localhost in IPv6 is 0:0:0:0:0:0:0:1, so it work both in IPv4 and IPv6 networks.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.armbruster-it.de/2010/04/restrict-a-grails-controller-to-localhost-access-only/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

