<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"
>

<channel>
	<title>acidum.de &#187; Spring Security</title>
	<atom:link href="http://www.acidum.de/tag/spring-security/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.acidum.de</link>
	<description></description>
	<lastBuildDate>Sun, 08 Nov 2009 20:12:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Grails Spring Security</title>
		<link>http://www.acidum.de/2008/10/08/grails-spring-security/</link>
		<comments>http://www.acidum.de/2008/10/08/grails-spring-security/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 13:13:35 +0000</pubDate>
		<dc:creator>Christoph Hartmann</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Grails]]></category>
		<category><![CDATA[Groovy]]></category>
		<category><![CDATA[Acegi Security]]></category>
		<category><![CDATA[Spring Security]]></category>

		<guid isPermaLink="false">http://www.acidum.de/?p=160</guid>
		<description><![CDATA[For my current grails project I use the Spring Security Plugin (formerly Acegi Security) to secure my views and services. A tutorial at the Grails site gives a great overview how to install the grails plugin.
Right after installation I faced some issues:
Secure your Rest Services
This sounds quite easy due to a simple switch within the [...]]]></description>
			<content:encoded><![CDATA[<p>For my current grails project I use the <a title="Grails Acegi Plugin" href="http://grails.org/AcegiSecurity+Plugin" target="_blank">Spring Security Plugin (formerly Acegi Security)</a> to secure my views and services. A <a title="Acegi Installation Tutorial" href="http://www.grails.org/AcegiSecurity+Plugin+-+Basic+Tutorial" target="_blank">tutorial</a> at the Grails site gives a great overview how to install the grails plugin.</p>
<p>Right after installation I faced some issues:</p>
<p><strong>Secure your Rest Services</strong></p>
<p>This sounds quite easy due to a simple switch within the SecurityConfig.groovy. Just activate the BasicProcessingFilter. Now grails uses the Http Basic Authentification.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #008000; font-style: italic; font-weight: bold;">/** use basicProcessingFilter */</span>
basicProcessingFilter <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span></pre></div></div>

<p>At the client side the code would like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">sun.misc.BASE64Encoder</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">groovy.util.XmlSlurper</span>
&nbsp;
def userid <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;john.doe&quot;</span>
def password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;pass&quot;</span>
&nbsp;
def url <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://localhost:8080/CandyStreamServer/rest/gps/&quot;</span>
&nbsp;
def conn <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>url<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>userid <span style="color: #339933;">&amp;&amp;</span> password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	println <span style="color: #0000ff;">&quot;set authorization&quot;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// add HTTP authentication</span>
       <span style="color: #003399;">String</span> encodedAuth <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> BASE64Encoder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">encode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>userid <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;:&quot;</span> <span style="color: #339933;">+</span> password<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBytes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
&nbsp;
	conn.<span style="color: #006633;">setRequestProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Authorization&quot;</span>, <span style="color: #0000ff;">&quot;Basic &quot;</span> <span style="color: #339933;">+</span> encodedAuth<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
def slurper <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XmlSlurper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
&nbsp;
conn.<span style="color: #006633;">requestMethod</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET&quot;</span>
conn.<span style="color: #006633;">doOutput</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">true</span>
&nbsp;
println <span style="color: #0000ff;">&quot;check connection&quot;</span>
def response
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>conn.<span style="color: #006633;">responseCode</span> <span style="color: #339933;">==</span> conn.<span style="color: #006633;">HTTP_OK</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	conn.<span style="color: #006633;">inputStream</span>.<span style="color: #006633;">withStream</span> <span style="color: #009900;">&#123;</span>
		response <span style="color: #339933;">=</span> slurper.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>it<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	response <span style="color: #339933;">=</span> conn.<span style="color: #006633;">responseCod</span>
<span style="color: #009900;">&#125;</span>
println response
conn.<span style="color: #006633;">disconnect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>Retrieve the Current User</strong></p>
<p>On the server side it may necessary to retrieve the current user. Spring Security offers a SecurityContextHolder. Unfortunately the  securityContext.getAuthentication().getPrincipal(); does not return the Groovy user object. Instead the Spring Security Plugin uses its own user implementation that holds the Groovy user object. This is required due to Springs dependency on a specific interface.</p>
<p>The plugin provides a org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser interface that extends the org.springframework.security.userdetails.UserDetails interface. To retrieve your Groovy user object just call getDomainClass()</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">SecurityContext securityContext <span style="color: #339933;">=</span> SecurityContextHolder.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
def springUser <span style="color: #339933;">=</span> securityContext.<span style="color: #006633;">getAuthentication</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPrincipal</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">return</span> springUser.<span style="color: #006633;">getDomainClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.acidum.de/2008/10/08/grails-spring-security/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.533 seconds -->
