<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><title>Eric on Java</title> <link>http://www.ericonjava.com</link> <description>Java, JavaFX, Groovy, Grails ...</description> <lastBuildDate>Thu, 02 Sep 2010 23:28:51 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/EricOnJava" /><feedburner:info uri="ericonjava" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><item><title>Remote Desktop to a Amazon EC2 CentOS 5.4 AMI</title><link>http://feedproxy.google.com/~r/EricOnJava/~3/1K1oJEcL0cs/</link> <comments>http://www.ericonjava.com/?p=366#comments</comments> <pubDate>Mon, 05 Jul 2010 15:46:51 +0000</pubDate> <dc:creator>Eric Warriner</dc:creator> <category><![CDATA[Amazon EC2]]></category> <category><![CDATA[Amazon EC2 Freenx nx Centos]]></category><guid isPermaLink="false">http://www.ericonjava.com/?p=366</guid> <description><![CDATA[<script type="text/javascript">dzone_url="http://www.ericonjava.com/?p=366";</script>Although this has been posted before by a few folks (some more comprehensive than others depending on your OS / configuration)&#8230;I thought I would add my notes to the &#8220;Internets&#8221; on creating a remote desktop connection to a Amazon EC2 CentOS 5.4 AMI. Install FreeNX package on your instance yum install -y freenx for a KDE]]></description> <content:encoded><![CDATA[<script type="text/javascript">dzone_url="http://www.ericonjava.com/?p=366";</script> <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>Although this has been posted before by a few folks (some more comprehensive than others depending on your OS / configuration)&#8230;I thought I would add my notes to the &#8220;<a href="http://en.wikipedia.org/wiki/Internets">Internets</a>&#8221; on creating a remote desktop connection to a Amazon EC2 CentOS 5.4 <a href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=3733" target="_blank">AMI</a>.</p><ul><li>Install FreeNX package on your instance</li><li><pre class="brush: java;">yum install -y freenx</pre></li><li>for a KDE desktop</li><li><pre class="brush: java;">yum install -y kdebase</pre></li><li>for a gnome desktop</li><li><pre class="brush: java;">yum install -y gnome-session</pre></li><li>navigate to /etc/nxserver and copy the node.conf.sample to node.conf</li><li><pre class="brush: java;">cp node.conf.sample node.conf</pre></li><li>Edit the node.conf file to enable &#8220;ENABLE_PASSDB_AUTHENTICATION=&#8221;1&#8243;</li><li><pre class="brush: java;">nano node.conf</pre></li><li><a href="http://images.ericwarriner.com/wp-content/uploads/2010/07/nano1.gif#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="aligncenter size-medium wp-image-373" title="edit node.conf" src="http://images.ericwarriner.com/wp-content/uploads/2010/07/nano1-300x184.gif" alt="" width="300" height="184" /></a>Note that you can click on the image to make it larger.</li><li>on CentOS, create a user and set the password that will be logging in via remote desktop</li><li><pre class="brush: java;">adduser eric
passwd eric</pre></li><li>on nxserver, add the user that you just created and set the password (this is the user the NX client will log in with)</li><li><pre class="brush: java;">nxserver --adduser eric
nxserver --passwd eric</pre></li><li><a href="http://images.ericwarriner.com/wp-content/uploads/2010/07/nano2.gif#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="aligncenter size-medium wp-image-372" title="nano2usernamepassword" src="http://images.ericwarriner.com/wp-content/uploads/2010/07/nano2-300x108.gif" alt="" width="300" height="108" /></a></li><li>Now that the nxserver is complete we need to configure a client.</li><li>Use your favorite method to transfer client.id_dsa.key (I used FileZilla SFTP) to the client machine</li><li>Download and install the <a href="http://www.nomachine.com/download-client-windows.php" target="_blank">NoMachine NX client and fonts</a></li><li>Direct the NX Client to use the client.id_dsa.key by clicking the &#8220;Configure Button&#8221;</li></ul><p style="text-align: center;"><a href="http://images.ericwarriner.com/wp-content/uploads/2010/07/configure.gif#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="size-medium wp-image-367 aligncenter" title="nomachine configure button" src="http://images.ericwarriner.com/wp-content/uploads/2010/07/configure-300x188.gif" alt="" width="300" height="188" /></a></p><ul><li>Then select the &#8220;Key&#8221; where you can &#8220;Import&#8221; the client.id_dsa.key &#8230;make sure to save.</li></ul><p><a href="http://images.ericwarriner.com/wp-content/uploads/2010/07/key.gif#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="aligncenter size-medium wp-image-368" title="NoMachinekeybutton" src="http://images.ericwarriner.com/wp-content/uploads/2010/07/key-300x195.gif" alt="" width="300" height="195" /></a></p><ul><li>In this configuration menu you can also set the &#8220;Desktop&#8221; type KDE or GNOME (dependent on what you installed on the server earlier) and host, port and dimension parameters.</li><li>For username and password&#8230;you would obviously use the user earlier created on the server.</li><li>Upon clicking connect&#8230;if everything is configured correctly &#8230; a desktop should appear.<a href="http://images.ericwarriner.com/wp-content/uploads/2010/07/home.gif#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><img class="aligncenter size-medium wp-image-369" title="home" src="http://images.ericwarriner.com/wp-content/uploads/2010/07/home-300x232.gif" alt="" width="300" height="232" /></a></li></ul><p>That should do it&#8230;but if you know of &#8220;<a href="http://www.youtube.com/watch?v=bl6RJyZdBSU" target="_blank">Harder, Better, Faster, Stronger</a>&#8221; ways to do this&#8230;please leave a comment.</p><p>To complete this task I mostly used the following reference: <a href="http://wiki.centos.org/HowTos/FreeNX" target="_blank">CentOS Wiki</a></p><ul class="socialwrap size24 row"><li class="iconOnly"><a rel="nofollow" target="_blank" class="delicious" href="http://delicious.com/post?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366&amp;title=Remote+Desktop+to+a+Amazon+EC2+CentOS+5.4+AMI" title="Bookmark this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI on Delicious"><span class="head">Bookmark on Delicious</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="digg" href="http://digg.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366&amp;title=Remote+Desktop+to+a+Amazon+EC2+CentOS+5.4+AMI&amp;bodytext=Although+this+has+been+posted+before+by+a+few+folks+%28some+more+comprehensive+than+others+depending+on+your+OS+%2F+configuration%29...I+thought+I+would+add+my+notes+to+the+%22Internets%22+on+creating+a+remote+desktop+connection+to+a+Amazon+EC2+CentOS+5.4%C2%A0AMI.%0D%0A%0D%0A%09Install+FreeNX+package+on+your+instance%0D%0A%09%0D%0A%5Bjava%5Dyum+install+-y" title="Digg this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI"><span class="head">Digg this post</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="facebook" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366&amp;t=Remote+Desktop+to+a+Amazon+EC2+CentOS+5.4+AMI" title="Recommend this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI on Facebook"><span class="head">Recommend on Facebook</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="linkedin" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366&amp;title=Remote+Desktop+to+a+Amazon+EC2+CentOS+5.4+AMI&amp;&amp;summary=" title="Share this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI on Linkedin"><span class="head">Share on Linkedin</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="myspace" href="javascript:void(window.open('http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(document.location.toString()),'ptm','height=450,width=440').focus())#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" title="Share this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI via MySpace"><span class="head">Share via MySpace</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="reddit" href="http://www.reddit.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366&amp;title=Remote+Desktop+to+a+Amazon+EC2+CentOS+5.4+AMI" title="Share this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI on Reddit"><span class="head">share via Reddit</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="stumble" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366&amp;title=Remote+Desktop+to+a+Amazon+EC2+CentOS+5.4+AMI" title="Share this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI with Stumblers"><span class="head">Share with Stumblers</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="tumblr" href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366&amp;t=Remote+Desktop+to+a+Amazon+EC2+CentOS+5.4+AMI" title="Tumblr. this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI "><span class="head">Tumblr it</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="twitter" href="http://twitter.com/home/?status=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D366" title="Tweet this post : Remote Desktop to a Amazon EC2 CentOS 5.4 AMI on Twitter"><span class="head">Tweet about it</span></a></li></ul><div class="clean"></div><img src="http://feeds.feedburner.com/~r/EricOnJava/~4/1K1oJEcL0cs" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.ericonjava.com/?feed=rss2&amp;p=366</wfw:commentRss> <slash:comments>0</slash:comments> <feedburner:origLink>http://www.ericonjava.com/?p=366#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</feedburner:origLink></item> <item><title>Screen Scraping? Groovy GPath FTW!</title><link>http://feedproxy.google.com/~r/EricOnJava/~3/7gQLU8aqbT8/</link> <comments>http://www.ericonjava.com/?p=347#comments</comments> <pubDate>Thu, 24 Jun 2010 03:56:19 +0000</pubDate> <dc:creator>Eric Warriner</dc:creator> <category><![CDATA[Groovy]]></category> <category><![CDATA[groovy xpath gpath]]></category><guid isPermaLink="false">http://www.ericonjava.com/?p=347</guid> <description><![CDATA[<script type="text/javascript">dzone_url="http://www.ericonjava.com/?p=347";</script>While I may be a little late to extol all of the virtues of Groovy as so many before me have already done with great eloquence&#8230;I would like to quickly point out that Groovy GPath rocks! Take, for instance, a requirement to screen scrap HTML pages. A couple of the ways to approach this task]]></description> <content:encoded><![CDATA[<script type="text/javascript">dzone_url="http://www.ericonjava.com/?p=347";</script> <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>While I may be a little late to extol all of the virtues of <a href="http://groovy.codehaus.org/">Groovy</a> as so many before me have already done with great eloquence&#8230;I would like to quickly point out that Groovy GPath rocks!</p><p>Take, for instance, a requirement to screen scrap HTML pages. A couple of the ways to approach this task is to use:</p><ul><li><a href="http://en.wikipedia.org/wiki/Regular_expression" target="_blank">Regular Expressions</a> parsing (hopefully you&#8217;re a regex ninja)</li><li><a href="http://www.w3.org/TR/xpath20/" target="_blank">XPath</a> (W3C recommendation)</li></ul><p>and my favorite</p><ul><li><a href="http://groovy.codehaus.org/GPath">GPath</a> (a Groovier XPath)</li></ul><p>GPath is</p><blockquote><p>a path expression language integrated into Groovy which allows parts of nested structured data to be identified</p></blockquote><p>This applies to nested POJOs as well as XML and to boot <a href="http://ccil.org/~cowan/XML/tagsoup/">TagSoup</a> &#8216;d HTML as demonstrated a few years ago <a href="http://froth-and-java.blogspot.com/2007/06/html-screen-scraping-with-groovy.html">here</a>.</p><p>So as a quick example of how easy Groovy makes scraping, let&#8217;s scrape this site for the text of the title element using a traditional Java way (keep in mind there are numerous ways to do XPath in pure Java) and then the Groovy &#8216;er way.</p><p>A Traditional Java XPath Approach:</p><pre class="brush: java;">

import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Nodes;
import nu.xom.XPathContext;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class Main {
    public static void main(String[] args) {
        try {

            XMLReader tagsoup = XMLReaderFactory.createXMLReader(&quot;org.ccil.cowan.tagsoup.Parser&quot;);

            Builder builder = new Builder(tagsoup);
            Document doc = builder.build(new URL(&quot;http://www.ericonjava.com&quot;).openStream());

            XPathContext context = new XPathContext(&quot;h&quot;, &quot;http://www.w3.org/1999/xhtml&quot;);
            Nodes table = doc.query(&quot;/h:html/h:body/h:div/h:div/h:div/h:div/h:h1/h:a&quot;, context);

            System.out.println(&quot;TITLE = &quot; + table.get(0).getValue());

        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
</pre><p>The Groovy GPath Approach:</p><pre class="brush: java;">
def slurper = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser());

def seedURL = new URL(&quot;http://www.ericonjava.com&quot;)
seedURL.withReader { seedReader -&gt;

    def seedHTML = slurper.parse(seedReader)

        Title= seedHTML.body.div.div.div.div.h1.a
        println &quot;Title =  ${Title}&quot;;
    }
</pre><p>Clearly&#8230;.Groovy GPath FTW.</p><ul class="socialwrap size24 row"><li class="iconOnly"><a rel="nofollow" target="_blank" class="delicious" href="http://delicious.com/post?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347&amp;title=Screen+Scraping%3F+Groovy+GPath+FTW%21" title="Bookmark this post : Screen Scraping? Groovy GPath FTW! on Delicious"><span class="head">Bookmark on Delicious</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="digg" href="http://digg.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347&amp;title=Screen+Scraping%3F+Groovy+GPath+FTW%21&amp;bodytext=While+I+may+be+a+little+late+to+extol+all+of+the+virtues+of+Groovy+as+so+many+before+me+have+already+done+with+great+eloquence...I+would+like+to+quickly+point+out+that+Groovy+GPath+rocks%21%0D%0A%0D%0ATake%2C+for+instance%2C+a+requirement+to+screen+scrap+HTML+pages.+A+couple+of+the+ways+to+approach+this+task+is+to+use%3A%0D%0A%0D%0A%09Regular+E" title="Digg this post : Screen Scraping? Groovy GPath FTW!"><span class="head">Digg this post</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="facebook" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347&amp;t=Screen+Scraping%3F+Groovy+GPath+FTW%21" title="Recommend this post : Screen Scraping? Groovy GPath FTW! on Facebook"><span class="head">Recommend on Facebook</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="linkedin" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347&amp;title=Screen+Scraping%3F+Groovy+GPath+FTW%21&amp;&amp;summary=" title="Share this post : Screen Scraping? Groovy GPath FTW! on Linkedin"><span class="head">Share on Linkedin</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="myspace" href="javascript:void(window.open('http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(document.location.toString()),'ptm','height=450,width=440').focus())#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" title="Share this post : Screen Scraping? Groovy GPath FTW! via MySpace"><span class="head">Share via MySpace</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="reddit" href="http://www.reddit.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347&amp;title=Screen+Scraping%3F+Groovy+GPath+FTW%21" title="Share this post : Screen Scraping? Groovy GPath FTW! on Reddit"><span class="head">share via Reddit</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="stumble" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347&amp;title=Screen+Scraping%3F+Groovy+GPath+FTW%21" title="Share this post : Screen Scraping? Groovy GPath FTW! with Stumblers"><span class="head">Share with Stumblers</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="tumblr" href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347&amp;t=Screen+Scraping%3F+Groovy+GPath+FTW%21" title="Tumblr. this post : Screen Scraping? Groovy GPath FTW! "><span class="head">Tumblr it</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="twitter" href="http://twitter.com/home/?status=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D347" title="Tweet this post : Screen Scraping? Groovy GPath FTW! on Twitter"><span class="head">Tweet about it</span></a></li></ul><div class="clean"></div><img src="http://feeds.feedburner.com/~r/EricOnJava/~4/7gQLU8aqbT8" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.ericonjava.com/?feed=rss2&amp;p=347</wfw:commentRss> <slash:comments>2</slash:comments> <feedburner:origLink>http://www.ericonjava.com/?p=347#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</feedburner:origLink></item> <item><title>Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations</title><link>http://feedproxy.google.com/~r/EricOnJava/~3/R_e_AQR57NU/</link> <comments>http://www.ericonjava.com/?p=325#comments</comments> <pubDate>Fri, 21 May 2010 21:13:09 +0000</pubDate> <dc:creator>Eric Warriner</dc:creator> <category><![CDATA[Java]]></category> <category><![CDATA[Jersey]]></category> <category><![CDATA[JAX-RS]]></category> <category><![CDATA[Security Annotations]]></category> <category><![CDATA[Tomcat]]></category><guid isPermaLink="false">http://www.ericonjava.com/?p=325</guid> <description><![CDATA[<script type="text/javascript">dzone_url="http://www.ericonjava.com/?p=325";</script>While doing some research on securing JAX-RS Restful services, I came across this question on stackoverflow which asks how best to wire up a simple security mechanism for: Jersey JAX-RS. Tomcat. Basic Authentication. JSR 250 Annotations such as @RolesAllowed. So in a nutshell&#8230;.this can be accomplished quite easily by configuring the following: tomcat-users.xml (found in]]></description> <content:encoded><![CDATA[<script type="text/javascript">dzone_url="http://www.ericonjava.com/?p=325";</script> <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script><p>While doing some research on securing JAX-RS Restful services, I came across <a href="http://stackoverflow.com/questions/2291323/jersey-spring-tomcat-and-security-annotations" target="_blank">this</a> question on <a href="http://www.stackoverflow.com" target="_blank">stackoverflow</a> which asks how best to wire up a <strong><em>simple security</em></strong> mechanism for:</p><ol><li>Jersey JAX-RS.</li><li>Tomcat.</li><li>Basic Authentication.</li><li>JSR 250 Annotations such as @RolesAllowed.</li></ol><p>So in a nutshell&#8230;.this can be accomplished quite easily by configuring the following:</p><ol><li>tomcat-users.xml (found in the conf dir of your tomcat install).</li><li>the applications web.xml.</li><li>and lastly annotating the restful service.</li></ol><p>By adjusting the tomcat-users.xml  you will create a tomcat memory realm of usernames and passwords.</p><p>To do this&#8230;simpy add the following to the tomcat-users.xml  file:</p><pre class="brush: java;">

&lt;role rolename=&quot;admin&quot; /&gt;
&lt;user username=&quot;admin&quot; password=&quot;adminadmin&quot; roles=&quot;manager,admin,user&quot; /&gt;
&lt;role rolename=&quot;user&quot; /&gt;
&lt;user username=&quot;user&quot; password=&quot;useruser&quot; roles=&quot;user&quot; /&gt;
</pre><div>Next you will want to adjust your web.xml to include both jersey resources and also the applications security configuration:</div><div><pre class="brush: java;">
&lt;servlet&gt;
&lt;servlet-name&gt;ServletAdaptor&lt;/servlet-name&gt;
&lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
&lt;init-param&gt;
&lt;param-name&gt;com.sun.jersey.spi.container.ResourceFilters&lt;/param-name&gt;
&lt;param-value&gt;com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;ServletAdaptor&lt;/servlet-name&gt;
&lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;
&lt;session-config&gt;
&lt;session-timeout&gt;
30
&lt;/session-timeout&gt;
&lt;/session-config&gt;
&lt;welcome-file-list&gt;
&lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;
&lt;security-constraint&gt;
&lt;web-resource-collection&gt;
&lt;web-resource-name&gt;Secure&lt;/web-resource-name&gt;
&lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
&lt;/web-resource-collection&gt;
&lt;auth-constraint&gt;
&lt;role-name&gt;admin&lt;/role-name&gt;
&lt;role-name&gt;user&lt;/role-name&gt;
&lt;/auth-constraint&gt;
&lt;/security-constraint&gt;
&lt;login-config&gt;
&lt;auth-method&gt;BASIC&lt;/auth-method&gt;
&lt;realm-name&gt;Test&lt;/realm-name&gt;
&lt;/login-config&gt;
&lt;security-role&gt;
&lt;role-name&gt;admin&lt;/role-name&gt;
&lt;/security-role&gt;
&lt;security-role&gt;
&lt;role-name&gt;user&lt;/role-name&gt;
&lt;/security-role&gt;
</pre><div>and lastly a rest service that utilizes the JSR-250 security annotations:</div><pre class="brush: java;">import com.sun.jersey.core.util.Base64;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;

@Path(&quot;/Test&quot;)
@RolesAllowed(&quot;admin&quot;)
public class Test {

    @GET
    @Produces(&quot;text/plain&quot;)
    @RolesAllowed(&quot;user&quot;)
    public String getNumber(@Context HttpHeaders headers){
        System.out.println( getCredentials(headers));

        return &quot;1 Secured with User&quot;;
    }

     @GET
     @Path(&quot;/Secure&quot;)
     @RolesAllowed(&quot;admin&quot;)
     @Produces(&quot;text/plain&quot;)
    public String getSecureNumber(@Context HttpHeaders headers){
         System.out.println( getCredentials(headers));

        return &quot;2 Secured with Admin&quot;;
    }

    private String getCredentials(HttpHeaders headers) {
        String auth = headers.getRequestHeader(&quot;authorization&quot;).get(0);

        auth = auth.substring(&quot;Basic &quot;.length());
        String[] values = new String(Base64.base64Decode(auth)).split(&quot;:&quot;);
        String username = values[0];
        String password = values[1];

        String return_val = &quot;Username = &quot; + username + &quot; Password = &quot;+ password;

        return return_val;
    }

}</pre><p>If you deploy to localhost with the above configuration the urls are as follows</p><ol><li>an &#8220;admin&#8221; person has access to  <a href="http://localhost:8080/MyService2/resources/Test/Secure">http://localhost:8080/MyService2/resources/Test/Secure</a></li><li>a &#8220;user&#8221; person has access to = <a href="http://localhost:8080/MyService2/resources/Test/">http://localhost:8080/MyService2/resources/Test/</a></li></ol><p>Because this is &#8220;BASIC&#8221; security&#8230;a pop up will appear to log the person in when a request is made to the secured resource.</p><p>You can find a zipped copy of my Netbeans project <a href="http://www.ericonjava.com/MyService2.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">here</a></p></div><ul class="socialwrap size24 row"><li class="iconOnly"><a rel="nofollow" target="_blank" class="delicious" href="http://delicious.com/post?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325&amp;title=Jersey+JAX-RS%2C+Tomcat%2C+Basic+Auth+and+Security+Annotations" title="Bookmark this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations on Delicious"><span class="head">Bookmark on Delicious</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="digg" href="http://digg.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325&amp;title=Jersey+JAX-RS%2C+Tomcat%2C+Basic+Auth+and+Security+Annotations&amp;bodytext=While+doing+some+research+on+securing+JAX-RS+Restful+services%2C+I+came+across+this+question+on+stackoverflow+which+asks+how+best+to+wire+up+a+simple+security+mechanism+for%3A%0D%0A%0D%0A%09Jersey+JAX-RS.%0D%0A%09Tomcat.%0D%0A%09Basic+Authentication.%0D%0A%09JSR+250+Annotations+such+as+%40RolesAllowed.%0D%0A%0D%0ASo+in+a+nutshell....this+can+be+accomplished+qu" title="Digg this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations"><span class="head">Digg this post</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="facebook" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325&amp;t=Jersey+JAX-RS%2C+Tomcat%2C+Basic+Auth+and+Security+Annotations" title="Recommend this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations on Facebook"><span class="head">Recommend on Facebook</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="linkedin" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325&amp;title=Jersey+JAX-RS%2C+Tomcat%2C+Basic+Auth+and+Security+Annotations&amp;&amp;summary=" title="Share this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations on Linkedin"><span class="head">Share on Linkedin</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="myspace" href="javascript:void(window.open('http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(document.location.toString()),'ptm','height=450,width=440').focus())#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" title="Share this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations via MySpace"><span class="head">Share via MySpace</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="reddit" href="http://www.reddit.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325&amp;title=Jersey+JAX-RS%2C+Tomcat%2C+Basic+Auth+and+Security+Annotations" title="Share this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations on Reddit"><span class="head">share via Reddit</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="stumble" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325&amp;title=Jersey+JAX-RS%2C+Tomcat%2C+Basic+Auth+and+Security+Annotations" title="Share this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations with Stumblers"><span class="head">Share with Stumblers</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="tumblr" href="http://www.tumblr.com/share?v=3&amp;u=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325&amp;t=Jersey+JAX-RS%2C+Tomcat%2C+Basic+Auth+and+Security+Annotations" title="Tumblr. this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations "><span class="head">Tumblr it</span></a></li><li class="iconOnly"><a rel="nofollow" target="_blank" class="twitter" href="http://twitter.com/home/?status=http%3A%2F%2Fwww.ericonjava.com%2F%3Fp%3D325" title="Tweet this post : Jersey JAX-RS, Tomcat, Basic Auth and Security Annotations on Twitter"><span class="head">Tweet about it</span></a></li></ul><div class="clean"></div><img src="http://feeds.feedburner.com/~r/EricOnJava/~4/R_e_AQR57NU" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.ericonjava.com/?feed=rss2&amp;p=325</wfw:commentRss> <slash:comments>2</slash:comments> <feedburner:origLink>http://www.ericonjava.com/?p=325#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed</feedburner:origLink></item> </channel> </rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Database Caching 2/18 queries in 0.047 seconds using disk
Object Caching 1282/1300 objects using disk
Content Delivery Network via Amazon Web Services: CloudFront: Amazon Web Services: S3: images.ericwarriner.com

Served from: www.ericonjava.com @ 2010-09-03 00:54:03 -->
