Jguard MyEclipse Tutorial
Jguard MyEclipse Tutorial
Jguard MyEclipse Tutorial
Requirements
Below is a list of software required before using this guide:
• Windows 2000 or XP
• JDK 1.4+
• MySQL 5.0
If you need complete information on JGuard, please visit JGuard home page at:
http://www.xwiki.jguard.com
In order to create and develop web application, a minimum knowledge on using MyEclipse web
development tools will help.
In order to setup databases, reader will need enough information on using MySQL and related
GUI Tools.
Outline
1. New Project Setup & Structure
b. Configure JGuard
b. Configure Actions
d. Configure Controller
Preface
One of the tedious tasks in web application development especially for a JEE web application is
setting up a proper security for both authentication and authorization section.
As we all know, security is a relative concept and hence every system needs to set it up based on
its requirements. But most the times developers do not need to configure a high level of security
with complex access controls and etc. but a nice and reliable security plug-in will completely
suffice.
Meanwhile, two important factors that come in between would be the ease of configuration and
plugging and also flexibility to upgrade security level when needed.
JGuard
JGuard is an open source JEE security framework for resolving access control problems in web
and standalone applications.
Once the Web Project is created, we need to add Struts Capabilities to it. This is done by right
clicking on the root of our project in the Package Explorer View, and selecting MyEclipse >
Add Struts Capabilities, as shown in Figure 3.
Figure 3 - Adding Struts Capabilities to a Web Project
The default values for the Struts dialog are fine for the purposes of this tutorial. However, you
might want to change the Base package for new classes to reflect your desired location. In
Figure 4 below, we will simply leave the default values.
Figure 4 - Configuring Struts Capabilities
After the wizard completes, the project structure will look like that shown in Figure 5.
Create folders jGuard and tld under the WEB-INF/conf directory and to group tld
files move them to the tld folder. Now our project should look like this:
Figure 5 - Project Layout After Configuration
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.5"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
So, remove all <web-app> attributes from the source code and then in order to add proper DTD
information switch to design mode and right-click on <web-app> element and select Add DTD
Information.
On Public ID: field click Browse and select -//Sun Microsystems, Inc.//DTD Web Application
2.3//EN as the figure suggests and hit OK and again OK to add DTD Information to your
web.xml file.
Configure JGuard
To start, let’s give our web application a name which is also necessary for JGuard to realize the
application name.
So, in design mode, right-click on the <web-app> element and select Add Child>display-name
and for its value type securedapp. You can also add the description exactly the same way and
fill the value as you desire.
Now, let’s set JGuard key files and configuration in our web.xml through following steps.
1. To set JGuard Authentication file right-click on <web-app> and then Add Child >
context-param and set the values as following:
<param-name> with authenticationConfigurationLocation and
<param-value> with /WEB-INF/conf/jGuard/jGuardAuthentication.xml
2. To set JGuard Authentication scope add another context-param and set the values as
following:
<param-name> with authenticationScope and
<param-value> with local
3. To set JGuard Authorization file add another context-param and set the values as
following:
<param-name> with authorizationConfigurationLocation and
<param-value> with /WEB-INF/conf/jGuard/jGuardAuthorization.xml
4. To set JGuard Authorization scope add another context-param and set the values as
following:
<param-name> with authorizationScope and
<param-value> with local
5. In order to disable JMX add another context-param and set the values as following:
<param-name> with enableJMX and
<param-value> with false
6. Now, let’s add JGuard AccessFilter configuration by adding a <filter> in the same way
and setting related values as following:
<filter-name> with AccessFilter
<filter-class> with net.sf.jguard.jee.authentication.http.AccessFilter
7. To pass its location as an <init-param> element, right-click on <filter> element and add
the <init-param> element and set its values as following:
8. Now, We need to map this filter to every actions. Then, right-click on <web-app> and
add a <filter-mapping> element and set the values as following:
<filter-name> with AccessFilter
<url-pattern> with *.do
9. The final step would be adding the listener class. So, right-click on <web-app>, add the
<listener> element and set the value as below:
<listener-class> with net.sf.jguard.jee.listeners.ContextListener
The main configuration of JGuard in web.xml ends here but you can set one more additional
element which tells your web application when to timeout your session. So, right-click on <web-
app> and add <session-config> element and set its value as: <session-timeout> with 30.
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>
/WEB-INF/conf/struts/struts-config.xml
</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
• Create the jsp directory under WEB-INF, right-click on the newly created directory and
then create a new JSP file and name it: logon.jsp and replace the source code with the
code below:
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el"%>
<title>authentication</title>
<html:form action="/LogonProcess" method="post">
you can enter admin/admin in user/password fields to authenticate
<div>
Login
<input
id="login" type="text" maxlength="20"
size="30" name="login" tabindex="1" />
</div>
<div>
Password
<input
id="password" type="password" value=""
size="30" name="password" tabindex="2" />
</div>
<input type="submit" value="submit" tabindex="4" />
<input type="reset" value="reset" tabindex="5" />
</html:form>
• create a new JSP file and name it: logoff.jsp and replace the source code with the code
below:
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el"%>
<title>logoff</title>
<h1>now you are unauthenticated</h1>
• create a new JSP file and name it: accessDenied.jsp and replace the source code with the
code below:
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el" %>
<title>access denied</title>
<h1>access to this ressource is denied</h1>
• create a new JSP file and name it: authenticationFailed.jsp and replace the source code
with the code below:
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el" %>
<title>authentication failed</title>
<h1>authentication Failed</h1>
We will create two more JSP files. The first file is to setup our welcome page and the second one
is created to test JGuard functionality.
• create a new JSP file and name it: welcome.jsp and replace the source code with the code
below:
<%@ taglib prefix="jguard" uri="http://jguard.net/tld/jguard.tld"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html-el"%>
<title>welcome page</title>
<h1>JGuard Welcome page</h1>
<h2>all users in the jGuardExample webApp have access to this page</h2>
<html:link forward="vipArea">
go to the vip area!!!
</html:link>
<jguard:hasPrincipal
principals="customer"
applicationName="anotherApplication">
you have got the 'customer' role
</jguard:hasPrincipal>
• create a new JSP file and name it: vipArea.jsp and replace the source code with the code
below:
<%@ taglib prefix="jguard" uri="http://jguard.net/tld/jguard.tld" %>
<title>vip area</title>
<h1>vip area</h1>
<p>only authenticated users will have access to this page</p>
<jguard:hasPrincipal principals="businessManager">
role = businessManager
</jguard:hasPrincipal>
<jguard:hasPrincipal principals="admin,stuff">
hello!! you have got a role called admin
</jguard:hasPrincipal>
<form-beans>
<form-bean name="generalForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="permissionName" type="java.lang.String" />
<form-property name="domainName" type="java.lang.String" />
<form-property name="Domains" type="java.util.ArrayList" />
<form-property name="DomainsNotBound" type="java.util.ArrayList" />
<form-property name="permissionsNotBound" type="java.util.ArrayList" />
<form-property name="description" type="java.lang.String" />
<form-property name="permissionURI" type="java.lang.String" />
<form-property name="scheme" type="java.lang.String" />
<form-property name="oldPermissionName" type="java.lang.String" />
<form-property name="oldDomainName" type="java.lang.String" />
<form-property name="principals" type="java.util.ArrayList" />
<form-property name="principalName" type="java.lang.String" />
<form-property name="principalAsc" type="java.lang.String" />
<form-property name="principalDesc" type="java.lang.String" />
<form-property name="oldPrincipalName" type="java.lang.String" />
<form-property
name="principal"
type="net.sf.jguard.core.principals.RolePrincipal" />
<form-property name="domainNames" type="java.lang.String" />
<form-property name="permissionNames" type="java.lang.String" />
<form-property name="permissionActions" type="java.lang.String" />
<form-property name="permissionClass" type="java.lang.String" />
<form-property name="scriptText" type="java.lang.String" />
<form-property name="scriptResult" type="java.lang.String" />
<form-property name="job" type="java.lang.String" />
<form-property name="location" type="java.lang.String" />
<form-property name="login" type="java.lang.String" />
<form-property name="oldLogin" type="java.lang.String" />
<form-property name="password" type="java.lang.String" />
<form-property name="users" type="java.util.ArrayList" />
<form-property name="firstname" type="java.lang.String" />
<form-property name="lastname" type="java.lang.String" />
<form-property name="country" type="java.lang.String" />
<form-property name="religion" type="java.lang.String" />
<form-property name="hobbies" type="java.lang.String" />
<form-property name="userPrincipalsNames" type="java.lang.String" />
</form-bean>
</form-beans>
Configure Actions
Switch to design mode and add New>Action, in the path field type /Welcome (MyEclipse will
show you an error sign because of empty use case field, but ignore it for now).
In Action Impl select Use existing Action class and click browse for Existing Type and look
for ForwardAction which resides in org.apache.struts.actions. When you click OK button,
MyEclipse error will disappear.
Figure 7 – Select ForwardAction as Existing Type
Now go to Parameter tab and type /WEB-INF/jsp/welcome.jsp in the parameter field and click
OK button.
Now repeat the above steps with the following values to setup remaining Actions. In all Actions
the Existing Type will remain the same.
• path="/AccessDenied", parameter="/WEB-INF/jsp/accessDenied.jsp"
• path="/AuthenticationFailed", parameter="/WEB-INF/jsp/authenticationFailed.jsp"
• path="/Logon", parameter="/WEB-INF/jsp/logon.jsp"
• path="/VipArea", parameter="/WEB-INF/jsp/vipArea.jsp"
• path="/Logoff", parameter="/WEB-INF/jsp/logoff.jsp" />
To configure the last Action, do exactly the above steps with these values:
• path="/LogonProcess", parameter="/WEB-INF/jsp/welcome.jsp"
But before clicking OK button, in the Form tab, click Browse on Name: field and choose the
generalForm that We recently added and at the end delete the content of the Scope field.
Note: The /VipArea is a secured path where we will test if JGuard works properly.
Configure Controller
As MyEclipse seems not able to setup the controller visually, you will have to add the following
code to your struts-config.xml manually right after the </action-mappings> element.
<controller
locale="false"
inputForward="true"
processorClass="org.apache.struts.action.RequestProcessor"
contentType="text/html" />
<struts-config>
<data-sources />
<form-beans>
<form-bean name="generalForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="permissionName" type="java.lang.String" />
<form-property name="domainName" type="java.lang.String" />
<form-property name="Domains" type="java.util.ArrayList" />
<form-property name="DomainsNotBound" type="java.util.ArrayList" />
<form-property name="permissionsNotBound" type="java.util.ArrayList" />
<form-property name="description" type="java.lang.String" />
<form-property name="permissionURI" type="java.lang.String" />
<form-property name="scheme" type="java.lang.String" />
<form-property name="oldPermissionName" type="java.lang.String" />
<form-property name="oldDomainName" type="java.lang.String" />
<form-property name="principals" type="java.util.ArrayList" />
<form-property name="principalName" type="java.lang.String" />
<form-property name="principalAsc" type="java.lang.String" />
<form-property name="principalDesc" type="java.lang.String" />
<form-property name="oldPrincipalName" type="java.lang.String" />
<form-property
name="principal"
type="net.sf.jguard.core.principals.RolePrincipal" />
<form-property name="domainNames" type="java.lang.String" />
<form-property name="permissionNames" type="java.lang.String" />
<form-property name="permissionActions" type="java.lang.String" />
<form-property name="permissionClass" type="java.lang.String" />
<form-property name="scriptText" type="java.lang.String" />
<form-property name="scriptResult" type="java.lang.String" />
<form-property name="job" type="java.lang.String" />
<form-property name="location" type="java.lang.String" />
<form-property name="login" type="java.lang.String" />
<form-property name="oldLogin" type="java.lang.String" />
<form-property name="password" type="java.lang.String" />
<form-property name="users" type="java.util.ArrayList" />
<form-property name="firstname" type="java.lang.String" />
<form-property name="lastname" type="java.lang.String" />
<form-property name="country" type="java.lang.String" />
<form-property name="religion" type="java.lang.String" />
<form-property name="hobbies" type="java.lang.String" />
<form-property name="userPrincipalsNames" type="java.lang.String" />
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="welcome" path="/Welcome.do" />
<forward name="logon" path="/Logon.do" redirect="false" contextRelative="true" />
<forward name="logoff" path="/Logoff.do" redirect="false" />
<forward name="vipArea" path="/VipArea.do" redirect="false" />
</global-forwards>
<action-mappings>
<action
path="/Welcome"
type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/jsp/welcome.jsp" />
<action
path="/AccessDenied"
type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/jsp/accessDenied.jsp" />
<action
path="/AuthenticationFailed"
parameter="/WEB-INF/jsp/authenticationFailed.jsp"
type="org.apache.struts.actions.ForwardAction" />
<action
path="/Logon"
parameter="/WEB-INF/jsp/logon.jsp"
type="org.apache.struts.actions.ForwardAction" />
<action
path="/VipArea"
parameter="/WEB-INF/jsp/vipArea.jsp"
type="org.apache.struts.actions.ForwardAction" />
<action
path="/Logoff"
type="org.apache.struts.actions.ForwardAction"
parameter="/WEB-INF/jsp/logoff.jsp" />
<action
path="/Captcha"
type="com.nsbsfi.prsnt.struts.action.CaptchaAction"
name="generalForm" />
<action
path="/LogonProcess"
type="org.apache.struts.actions.ForwardAction"
name="generalForm"
parameter="/WEB-INF/jsp/welcome.jsp"
attribute="generalForm" />
</action-mappings>
<controller
locale="false"
inputForward="true"
processorClass="org.apache.struts.action.RequestProcessor"
contentType="text/html" />
Configuring these files from scratch is beyond this tutorial. But you can refer to JGuard main
documentation for further information.
While, in order to setup XML based authentication and authorization, you can copy these XML
file and also their DTD files from WEB-INF/conf/jGuard of our securedapp.zip into yours.
NOTE: Open the jGuardUsersPrincipals.xml and notice that everywhere you can find the
applicatioName it is equal to securedapp (except for the customer role which belongs to
another web application). This name should exactly be the same and the one you define in your
<display-name> element in your web.xml. So, if you are configuring JGuard for a web
application with a different name, be sure to correct this in the jGuardUsersPrincipals.xml for all
principals that belong to this web application.