configure JAAS for jboss 7.1 and mysql--reference

Hello all, In this tutorial we are going to configure JAAS for jboss 7.1 and mysql for Form based authentication to be used in a web application. . We have already covered how toconfigure jaas for tomcat 7 and mysql. The difference between these is due to jBoss 7.1 application server. We need to configure subsystems and modules in case of jBoss 7.1 unlike Tomcat. It is assumed that you have basic knowledge of mysql, application servers, eclipse and creating a dynamic web project.

 

Configure JAAS for jboss 7.1

Pre-requisites:

  1. jBoss 7.1 application server
  2. Mysql database
  3. eclipse IDE (I’m using Juno in this article)
  4. Mysql connector jar file

Database configuration.

Create a database and create three tables as provided in the diagram.

Tables  to be created for  JDBCRealm of users

Tables to be created for JDBCRealm of users

  • users: Stores username and password which need to be authenticated
  • roles :  Stores allowed roles
  • users_roles: Stores the relation between users and their allowed roles.

This table structure is needed to configure JAAS for jboss 7.1 and mysql

Create database :

 

 

 Insert data into database:

 

Now we are done with your database part. We need to tell jBoss application server that we are going to use this database for JDBCRealm purpose. Normally we would place mysql connector jar into library of web application but for jBoss 7.1 we need to create a module for it and declare it in jBoss configuration file i.e. standalone.xml .

 

Creating module for mysql :

  • Navigate to <jboss_home>/modules/com  e.g. C:\jboss-as-7.1.1.Final\modules\com
  • Create a folder called mysql in it and under mysql , create another folder named main
  • Under main , copy your mysql connector jar file  and create a file calledmodule.xml

Your structure and the files under main folder should be

Folder structure for module

Folder structure for module

 

filesForModule

Now that we have created module.xml file and copied mysql connector jar for jdbc connectivity we need to specify that we are using this mysql connector jar as a resource for this module name.

So in your blank module.xml file, put following code

 

We are done for module creation. Now we need to configure it in standalone.xml

Configure module in standalone.xml

Navigate to <jboss_home>/standalone/configuration and open standalone.xml .

You will find a datasources tag under which you need to put this

 

  •  jndi name is the identifier we are going to use in our security configuration.
  • jdbc:mysql://localhost:3306/tutorialsDB is our database to which jndi name points.

Add following code to subsystems tag.

Configure jdbc driver using previously created module. Add following into drivers tag instandalone.xml

Now jBoss7.1 know that this database will be used as datasource. Now we need to configure this JAAS for jboss 7.1. So we will define security subsystem for authentication and authorization.

Add following code to  standalone.xml  under security-domains

 

  •  dsJndiName defines the name of the datasource used for jdbc realm.
  • principalsQuery defines the query which retrieves all usernames from the database which is configured for jdbc realm. In our case tutorialsdb will be used.
  • rolesQuery defines the roles defined for user which is authenticated.

Configuration is done for jBoss application server.

Application configuration:

First create a new dynamic web project in eclipse. We will name it jBossJaasMysql.After creating it, create files as shown in following folder structure.

Folder Structure of application

Folder Structure of application

  • login.jsp : asks username and password for the user.
  • index.jsp : This is a protected resource. Accessing this directly should ask for username and password using FORM based authentication and authorization service which we have configured.
  • jboss-web.xml : Tells the application which security system should be used.
  • web.xml: Configures application for FORM based authentication.

 

 

index.jsp

login.jsp

error.jsp

Add this code to your web.xml

 

  •  code in web-resource-collection tag means that resources with url pattern/protected/*  are constrained such that only DELETE, GET, POST and PUT operations can be performed for the role user
  • login-config configures the FORM authentication.

This is your jboss-web.xml

We are all done with configuration and setup part.

Hit the url http://localhost:8080/jBossJaasMysql/protected/index.jsp

As this is constrained resource, you will be asked to log in to application by this page.

Login page

Login page

Enter wrong username and password e.g. someUser/somePassword and click submit. You will see error.jsp showing message Invalid username and/or password.

Now again visit http://localhost:8080/jBossJaasMysql/protected/index.jsp  and enter username as prasad and password as kharkar.

This time, as we configure JAAS for jboss 7.1 and mysql the user prasad will be checked into database and the roles allotted to him. If he enters correct password, then he is authenticated. If a constrained resource is allowed to access a particular role, then it will be available. As index.jsp can be accessed with role user, prasad can accessindex.jsp now.

index.jsp

Hope this tutorial helps configure JAAS for jboss 7.1 and mysql.

原文地址:http://www.thejavageek.com/2013/09/18/configure-jaas-jboss-7-1-mysql/

你可能感兴趣的:(reference)