<?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>dominikdorn.com &#187; JAAS</title>
	<atom:link href="http://dominikdorn.com/tag/jaas/feed/" rel="self" type="application/rss+xml" />
	<link>http://dominikdorn.com</link>
	<description>shit happens ;)</description>
	<lastBuildDate>Mon, 06 Sep 2010 16:37:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Authentication in JSF2 with JAAS: Part 2 &#8211; Authenticating a client/user</title>
		<link>http://dominikdorn.com/2010/02/authentication-jsf2-jaas-authorization-user-roles-realms/</link>
		<comments>http://dominikdorn.com/2010/02/authentication-jsf2-jaas-authorization-user-roles-realms/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 11:28:04 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[JAAS]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JSF2]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=213</guid>
		<description><![CDATA[The second part of the series covers the basics needed to understand authorization in a JavaEE application based on the principles provided by JAAS and how they apply in modern, JavaEE based web applications. The user is thought, how to configure his web-application to use role-based authorization and authenticate against a specific realm.]]></description>
			<content:encoded><![CDATA[<p>In the second part of this series, we are going to understand some more terminology of JAAS and start creating our own authentication system.</p>
<p>A <strong>LoginContext</strong> Object is used by applications to authenticate <strong>user</strong>s independently of the underlying authentication technology. <strong>Authentication technology</strong> in this context means stuff like &#8220;file based authentication&#8221;, &#8220;Database based authentication&#8221;, &#8220;LDAP authentication&#8221;, &#8220;CERTIFICATE authentication&#8221; and so on.</p>
<p>The <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/login/LoginContext.html">LoginContext</a> class is part of the javax.security.auth.login package and describes methods used to authenticate <strong>Subjects/user</strong> in the meaning of <a href="http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/">Part 1 of this series</a>). The <a href="http://java.sun.com/developer/technicalArticles/Security/jaasv2/index.html">documentation</a> states:</p>
<blockquote><p>A subject is an identity in a system that you want to authenticate and assign access rights to. A subject can be a human user, a process, or a machine..</p></blockquote>
<p>which is basically what I said before. Its still a little bit unclear to me, why they use &#8220;user&#8221; in one documentation and &#8220;subject&#8221; in the other, but as its the same thing, we know what to do.</p>
<p>The documentation further states, that a <strong>subject</strong> may interact with different <strong>authorities</strong> (applications) and may have different passwords (<strong>credentials</strong>) for each of them. To represent the <strong>subject/user</strong> in these applications, the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/security/Principal.html"><strong>java.security.Principal</strong></a> class is used.</p>
<p>Our own <strong>LoginContext</strong> now invokes various <strong>LoginModules</strong> ( these handle the different ways of authentication, like form based, HTTP-Basic, HTTP-Digest, etc.). The <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/spi/LoginModule.html"><strong>LoginModule</strong></a> interface is part of<br />
the <strong>javax.security.auth.spi</strong> package. If one ever wants to integrate Facebook connect or something similar, this is where to look at!</p>
<p>But how does the <strong>LoginContext</strong> know, which <strong>LoginModules</strong> to invoke? This is done by a <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/security/auth/login/Configuration.html"><strong>Configuration</strong></a> Object.</p>
<p>So, to sum this up:</p>
<ol>
<li>We have 1-n <strong>LoginModules</strong>, handling different form of authentication.. file, db, ldap and so on</li>
<li>We have a <strong>LoginContext</strong>, taking users credentials and supplying them to the <strong>LoginModules</strong></li>
<li>We have <strong>configuration</strong>s, specifying which <strong>LoginModules</strong> should be used by the <strong>LoginContext</strong></li>
<li>A <strong>client supplies</strong> its <strong>credentials</strong> to the <strong>LoginContext</strong>. The LoginContext <strong>looks up</strong>, <strong>which LoginModules to use</strong> in its <strong>Configuration</strong> and <strong>authenticates the client</strong> with the <strong>provided credentials</strong> against the <strong>LoginModules</strong>.</li>
</ol>
<p><strong>But why bother with all this stuff of LoginContext, LoginModules, Configuration and so on? Shouldn&#8217;t it be easy to use authentication, especially custom authentication in a web-app? </strong></p>
<p>Because we&#8217;ve already got it in our AppServer!</p>
<p>Configuration:<br />
We&#8217;re actually creating our Configuration object manually in our web.xml. </p>
<p>Here we define, how we gain the authentication data from the user. In this example, I used auth-method=FORM because no-one wants to use HTTP-Basic Authentication anymore today! </p>
<pre lang="xml" lines="1">
    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>nameOfTheRealm</realm-name>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/loginError.xhtml</form-error-page>
        </form-login-config>
    </login-config>
</pre>
<p>With this configuration, you can already create your own login form with your preferred style, like this:<br />
login.xhtml</p>
<pre lang="html" line="1">
<form method="post" action="/j_security_check">
<label for="username">Userid</label>
<input type="text" id="username" name="j_username">
<label for="password">Password</label>
<input type="password" id="password" name="j_password">
<input type="submit" value="Login">
</form>
</pre>
<p>In the next part of the series, we&#8217;ll see how you can do it with an jsf/facelets page.</p>
<p>Ok, we now got our form for login. To <strong>Logout</strong> simply make a link to <strong>/j_security_logout </strong></p>
<p>We&#8217;ve now got login + logout, but they are not working yet. What we still have to do, is specify, what we&#8217;re authenticating against. In this post, we called it LoginModule, in Glassfishv3 its called Realm and we have already<br />
configured it with our Login-Config above.</p>
<p>A post which explains <a href="http://www.developinjava.com/features/47-enterprise-java/105-securing-a-web-application-on-glassfish-using-jaas.html">how to configure the simply, build in Glassfish &#8220;File-Realm&#8221;</a> is available at <a href="http://www.developinjava.com">DevelopInJava.com</a>. </p>
<p>In the next blog entry of this series, we&#8217;ll see how we can create our own realm, that really suits our needs.</p>
<p>We now got everything needed for a basic authentication, but we don&#8217;t yet have a group to role mapping, which we need for stuff like #{request.isUserInRole(&#8216;ADMIN&#8217;)} or annotating our beans (see later for all of that).<br />
So what we&#8217;re going to do is, map the given &#8220;groups&#8221; to roles. Before we do that, we should tell our application, which roles we have. Do it like that in your web.xml</p>
<pre lang="xml" line="1">
    <security-role>
        <description>all the users with the role Admin</description>
        <role-name>ADMIN</role-name>
    </security-role>

    <security-role>
        <description>all the users that are authenticated</description>
        <role-name>LOGGEDIN_USER</role-name>
    </security-role>

    <security-role>
        <description>all the users that are moderators/extended rights</description>
        <role-name>MODERATOR</role-name>
    </security-role>
</pre>
<p>Now we can map the groups we get from the realm to our roles. We have to put this in<br />
<strong>/WEB-INF/sun-web.xml </strong> (if you&#8217;re using Glassfish v3). My file looks like this:</p>
<pre lang="xml" line="1">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC '-//Sun Microsystems, Inc.//DTD
Application Server 9.0 Servlet 2.5//EN'
    'http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd'>
<sun-web-app error-url="">
    <context-root>/</context-root>
    <security-role-mapping>
        <role-name>LOGGEDIN_USER</role-name>
        <group-name>default</group-name>
    </security-role-mapping>
    <security-role-mapping>
        <role-name>MODERATOR</role-name>
        <group-name>moderatoren</group-name>
    </security-role-mapping>
    <security-role-mapping>
        <role-name>ADMIN</role-name>
        <group-name>root</group-name>
    </security-role-mapping>
    <class-loader delegate="true"/>
    <jsp-config>
<property name="keepgenerated" value="true">
            <description>Keep a copy of the generated
                servlet class' java code.</description>
        </property>
    </jsp-config>
</sun-web-app>
</pre>
<p>Click here to <a href="http://docs.sun.com/app/docs/doc/819-3660/beaql?a=view">view the general structure of the sun-web.xml file</a>.</p>
<p>Our User -> Principal, Group -> Role mapping now works. We can start protecting resources in our app! <strong>Horray</strong>!</p>
<p>We&#8217;re doing this by specifying &#8220;security-constraints&#8221; like this in our web.xml</p>
<pre lang="xml" line="1">
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>admin-area</web-resource-name>
            <url-pattern>/admin/</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description>admins should be allowed to access this resources</description>
            <role-name>ADMIN</role-name>
        </auth-constraint>
    </security-constraint>
</pre>
<p>This should be quite self-explanatory, for a detailed explanation take a look at<br />
<a href="http://java.sun.com/developer/technicalArticles/J2EE/security_annotation/">Security Annotations and Authorization in GlassFish and the Java EE 5 SDK</a>, which also explains how to secure beans with annotations<br />
and has a nice matrix whats possible with annotations and whats not.</p>
<p>If you want to lock some stuff for everyone, simply leave the auth-constraint element empty, like this:</p>
<pre lang="xml" line="1">
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>admin-area</web-resource-name>
            <url-pattern>/admin/</url-pattern>
        </web-resource-collection>
        <auth-constraint>
        </auth-constraint>
    </security-constraint>
</pre>
<p>If you&#8217;ve followed these instructions and the one on the blog-post <a href="http://www.developinjava.com/features/47-enterprise-java/105-securing-a-web-application-on-glassfish-using-jaas.html">Securing a Web Application on Glassfish using JAAS</a>, you should now be able to authenticate different users with different roles in your app using the File-Realm. </p>
<p>In the next articles of the series, we will look how to create an own realm, how to replace the form-based login with a JSF-Based one and how to work with roles in your JSF-Pages.</p>
<p>References:</p>
<ul>
<li><a href="http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/">Authentication in JSF2 with JAAS: Part 1 – Understand the terminology</a></li>
<li><a href="http://java.sun.com/developer/technicalArticles/Security/jaasv2/index.html">Java Authentication and Authorization Service (JAAS)in Java 2, Standard Edition (J2SE) 1.4</a></li>
<li><a href="http://www.developinjava.com/features/47-enterprise-java/105-securing-a-web-application-on-glassfish-using-jaas.html">Securing a Web Application on Glassfish using JAAS</a></li>
<li><a href="http://docs.sun.com/app/docs/doc/819-3660/beaql?a=view">Structure of sun-web.xml file</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/02/authentication-jsf2-jaas-authorization-user-roles-realms/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Authentication in JSF2 with JAAS: Part 1 &#8211; Understand the terminology</title>
		<link>http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/</link>
		<comments>http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 23:02:12 +0000</pubDate>
		<dc:creator>Dominik Dorn</dc:creator>
				<category><![CDATA[JSF]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaEE6]]></category>
		<category><![CDATA[JAAS]]></category>
		<category><![CDATA[JavaEE]]></category>
		<category><![CDATA[JSF2]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://dominikdorn.com/?p=206</guid>
		<description><![CDATA[Authenticating users in a JSF2 application with the help of JAAS. Part one of a multiple article series, covering the terminology used by JAAS and how to understand it with simple examples.]]></description>
			<content:encoded><![CDATA[<p>Ok, this will be a multi-part blog entry series.<br />
What I want to do with this blog entries, is to document, how to make your JSF2 application use JAAS ( Java Authentication and Authorization Service ) to manage your/my users, authenticate them through a form with<br />
the help of the server and use that security information in our JSF2 pages and our Java Beans.</p>
<p>In this first part of the series, I&#8217;ll try to cover the terminology used in easy to understand words. </p>
<p>These are the terms, you&#8217;ll need to know:</p>
<ul>
<li><strong>realm</strong>: A realm is basically a &quot;user database&quot;, being it a flat file with user/pass + group info,  database tables or even an ldap directory or something else you can imagine, like facebook connect or google authentication system. It may be used by n applications.</li>
<li><strong>user</strong>: A user is a person or program wishing to authenticate against our server/app. If you only make your website for real persons, these are your users. If you also offer a webservice, other programs accessing that service are also users. A user belongs to a realm, so may be valid in n applications (see principal below)</li>
<li><strong>role</strong>: Roles are assigned to users and/or groups in an application. E.g. GUEST for a not authenticated visitor, LOGGEDIN_USER for an authenticated user, MODERATOR or ADMIN for special people. </li>
<li><strong>group</strong>: Groups are like roles, but they are used over multiple applications and mapped to specific ROLES on an per-application-basis</li>
<li><strong>principal</strong>: A Principal is an authenticated user in the scope of an application. The same user may have different principals in different applications. A principal is identified by its <strong>name</strong> and authenticated using <strong>authentication data (credentials)</strong></li>
<li><strong>security policy domain</strong>: Also called <strong>security domain</strong> or <strong>realm</strong>. Basically, the database where you lookup users. But in this meaning, its where the realms are used, being it application1, application2, applicationN</li>
<li><strong>Security attributes</strong>: are attributes associated with every principal, like &quot; is allowed to access the admin area&quot; or stuff like that.</li>
<li><strong>credential</strong>: contains or references security attributes; are used to authenticate a Principal for a Java EE product service (your webapp)</li>
<p>If you want to get the original documentation, take a look at the <a href="http://docs.sun.com/app/docs/doc/820-7627/gijrp?a=view">Security chapter</a> in the <a href="http://docs.sun.com/app/docs/doc/820-7627">Java EE 6 Tutorial Volume I</a></p>
<p>Further references, which I&#8217;ll probably be using in the next posts of these series:</p>
<ul>
<li><a href="http://jcp.org/en/jsr/detail?id=115">JSR 115: JavaTM Authorization Contract for Containers</a></li>
<li><a href="http://java.sun.com/developer/technicalArticles/Security/jaasv2/index.html">Java Authentication and Authorization Service (JAAS)in Java 2, Standard Edition (J2SE) 1.4</a></li>
<li><a href="http://java.sun.com/javase/6/docs/technotes/guides/security/jaas/JAASRefGuide.html">JavaTM  Authentication and Authorization Service (JAAS) Reference Guide</a></li>
<li><a href="http://java.sun.com/javase/6/docs/technotes/guides/security/jaas/JAASLMDevGuide.html">JavaTM  Authentication and Authorization Service (JAAS) LoginModule Developer&#8217;s Guide</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://dominikdorn.com/2010/02/jaas-authentication-jsf2-terminology/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
