<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    FORM-based declarative authentication against MySQL Realm
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="https://community.jboss.org/people/rsprasad">R Saravana Prasad</a> in <i>Beginner's Corner</i> - <a href="https://community.jboss.org/docs/DOC-16020">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p><br/>This article is about FORM-Based authentication for jboss for securing admin related pages.<br/>The attached web application uses declarative authentication against mysql security realm.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Add following <strong>security-constraint</strong> section to <strong>web.xml</strong>:</p><p>&lt;security-constraint&gt;<br/>&#160;&#160;&#160;&#160; &lt;display-name&gt;require valid user&lt;/display-name&gt;<br/>&#160; &lt;web-resource-collection&gt;<br/>&#160; &lt;web-resource-name&gt;internal application&lt;/web-resource-name&gt;<br/>&#160; &lt;!-- secure only admin pages--&gt;<br/>&#160; &lt;url-pattern&gt;/admin/*&lt;/url-pattern&gt;<br/>&#160; &lt;http-method&gt;GET&lt;/http-method&gt;<br/>&#160; &lt;http-method&gt;POST&lt;/http-method&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/web-resource-collection&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;auth-constraint&gt;<br/>&#160; &lt;!--Admin pages secured only for admin--&gt;<br/>&#160; &lt;role-name&gt;admin&lt;/role-name&gt;<br/>&lt;/auth-constraint&gt;<br/>&lt;/security-constraint&gt;</p><p><br/>Add following <strong>login-config</strong> section to <strong>web.xml</strong>:</p><p><br/>&lt;login-config&gt;<br/>&lt;auth-method&gt;FORM&lt;/auth-method&gt;<br/>&lt;form-login-config&gt;<br/>&#160; &lt;form-login-page&gt;/login.jsp&lt;/form-login-page&gt;<br/>&#160; &lt;form-error-page&gt;/loginInvalid.jsp&lt;/form-error-page&gt;<br/>&lt;/form-login-config&gt;<br/>&lt;/login-config&gt;</p><p><br/>Find and replace following realm config section in &lt;JBOSS_HOME&gt;\server\default\deploy\jbossweb.sar\<strong>server.xml</strong>:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&lt;Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"<br/>driverName="org.gjt.mm.mysql.Driver"<br/>connectionURL="jdbc:mysql://localhost/jaasrealm"<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connectionName="root"<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; connectionPassword=""<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; userTable="users" <br/>userNameCol="user_name" <br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; userCredCol="user_pass"<br/>userRoleTable="user_roles" <br/>roleNameCol="role_name" /&gt;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>From the above, realm requires DB Class name, DB Driver class, DB URL, <br/>DB name, DB username, DB password and <br/>userTable is users,<br/>userNameCol is user_name,<br/>userRoleTable is user_roles,<br/>userCredCol is user_pass and<br/>roleNameCol is role_name</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Setting up security realm:<br/>Have mysql running.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>mysql&gt; create database jaasrealm;</p><p>mysql&gt; use jaasrealm;</p><p>mysql&gt; create table users (<br/>&#160; user_name varchar(15) not null primary key,<br/>&#160; user_pass varchar(15) not null<br/>);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>mysql&gt; create table user_roles (<br/>&#160; user_name varchar(15) not null,<br/>&#160; role_name varchar(15) not null,<br/>&#160; primary key (user_name, role_name)<br/>);</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>mysql&gt; insert into users values('hari','good');<br/>mysql&gt; insert into users values('hara','better');</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>mysql&gt; insert into user_roles values('hari','usergroup');<br/>mysql&gt; insert into user_roles values('hara','admin');</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In case more roles to be added to same user, hara keep adding more user_role records to hara:</p><p>mysql&gt; insert into user_roles values('hara','editor');</p><p>mysql&gt; insert into user_roles values('hara','publisher');</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Ensure mysql driver in JBoss classpath, browser setting for cookies and modify JBossIPAddress in links in JSPs.<br/>Deploy the application after extracting it to JBOSS_HOME/server/default/deploy/.<br/>Reach the application at URL: <a class="jive-link-external-small" href="http://%3cjbossipaddress%3e:8080/auth/index.jsp">http://&lt;JBossIPAddress&gt;:8080/auth/index.jsp</a><br/>The first two links are to user pages that require no authentication.<br/>The last two links are to admin pages which require authentication.<br/>Clicking on admin links will cause login.jsp to be displayed as configured by login-config section of web.xml.<br/>The pages are authenticated by j_security_check with textboxes for j_username and j_password.<br/>A j_security_check servlet reserved by JBoss for authentication handles the request and the security-constraints associated with it.<br/>On successful authentication, the secured admin page will be displayed.<br/>On unsuccessful authentication, loginInvalid.jsp as configured by login-config section of web.xml will be displayed.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Fire up the JBoss and pull up the app...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Saravana Prasad</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="https://community.jboss.org/docs/DOC-16020">going to Community</a></p>

        <p style="margin: 0;">Create a new document in Beginner's Corner at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2075">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>