Create database called jboss_db_security wity the follow structure:
-- Generated by MySQLWorkbench SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; CREATE SCHEMA IF NOT EXISTS `jboss_db_security` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ; USE `jboss_db_security`; -- ----------------------------------------------------- -- Table `jboss_db_security`.`usuario` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `jboss_db_security`.`usuario` ( `id` INT NOT NULL AUTO_INCREMENT , `user` VARCHAR(45) NOT NULL , `pass` VARCHAR(45) NOT NULL , PRIMARY KEY (`id`) ) ENGINE = InnoDB; -- ----------------------------------------------------- -- Table `jboss_db_security`.`role` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `jboss_db_security`.`role` ( `id` INT NOT NULL AUTO_INCREMENT , `role` VARCHAR(45) NOT NULL , PRIMARY KEY (`id`) ) ENGINE = InnoDB; -- ----------------------------------------------------- -- Table `jboss_db_security`.`usuario_role` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `jboss_db_security`.`usuario_role` ( `usuario_id` INT NOT NULL , `role_id` INT NOT NULL , PRIMARY KEY (`usuario_id`, `role_id`) , INDEX `fk_usuario_role_usuario` (`usuario_id` ASC) , INDEX `fk_usuario_role_role` (`role_id` ASC) , CONSTRAINT `fk_usuario_role_usuario` FOREIGN KEY (`usuario_id` ) REFERENCES `jboss_db_security`.`usuario` (`id` ) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT `fk_usuario_role_role` FOREIGN KEY (`role_id` ) REFERENCES `jboss_db_security`.`role` (`id` ) ON DELETE NO ACTION ON UPDATE NO ACTION); USE `jboss_db_security`; -- ----------------------------------------------------- -- Data for table `jboss_db_security`.`usuario` -- ----------------------------------------------------- SET AUTOCOMMIT=0; INSERT INTO `usuario` (`id`, `user`, `pass`) VALUES (1, 'user1', 'pass1'); INSERT INTO `usuario` (`id`, `user`, `pass`) VALUES (2, 'user2', 'pass2'); COMMIT; -- ----------------------------------------------------- -- Data for table `jboss_db_security`.`role` -- ----------------------------------------------------- SET AUTOCOMMIT=0; INSERT INTO `role` (`id`, `role`) VALUES (1, 'administrador'); INSERT INTO `role` (`id`, `role`) VALUES (2, 'superusuario'); INSERT INTO `role` (`id`, `role`) VALUES (3, 'convidado'); COMMIT; -- ----------------------------------------------------- -- Data for table `jboss_db_security`.`usuario_role` -- ----------------------------------------------------- SET AUTOCOMMIT=0; INSERT INTO `usuario_role` (`usuario_id`, `role_id`) VALUES (1, 1); INSERT INTO `usuario_role` (`usuario_id`, `role_id`) VALUES (1, 2); INSERT INTO `usuario_role` (`usuario_id`, `role_id`) VALUES (2, 3); COMMIT; SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
Drop mysql-connector-java-5.1.6-bin.jar on folder: ${JBOSS_HOME}\server\default\lib\
Where ${JBOSS_HOME} is your JBoss root folder
Create file ${JBOSS_HOME}\server\default\deploy\mysql-ds.xml
With the following contents
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>MySqlDS</jndi-name> <connection-url>jdbc:mysql://127.0.0.1:3306/jboss_db_security</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>fiap</password> <valid-connection-checker-class-name> org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker </valid-connection-checker-class-name> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
Testing the JDBC connection
Create a new web project Eg: test_jdbc and create the file index.jsp
With the following contents:
<%@page contentType="text/html" import="java.util.*,javax.naming.*,javax.sql.DataSource,java.sql.*"%> <% DataSource ds = null; Connection con = null; PreparedStatement pr = null; InitialContext ic; try { ic = new InitialContext(); ds = (DataSource) ic.lookup("java:/MySqlDS"); con = ds.getConnection(); pr = con.prepareStatement("SELECT USER, PASS FROM USUARIO"); ResultSet rs = pr.executeQuery(); while (rs.next()) { out.println("<br> " + rs.getString("USER") + " | " + rs.getString("PASS")); } rs.close(); pr.close(); } catch (Exception e) { out.println("Exception thrown " + e); } finally { if (con != null) { con.close(); } } %>
Deploy it on JBoss and open on your browser: http://localhost:8080/test_jdbc
You should get:
user1 | user1
user2 | pass2
Edit the file: ${JBOSS_HOME}\server\default\conf\login-config.xml and add the following node:
<application-policy name="test-policy"> <authentication> <login-module code="org.jboss.security.auth.spi.DatabaseServerLoginModule" flag="required"> <module-option name="dsJndiName">java:/MySqlDS</module-option> <module-option name="principalsQuery"> SELECT pass FROM usuario WHERE user = ? </module-option> <module-option name="rolesQuery"> SELECT r.role, 'Roles' FROM usuario_role ur INNER JOIN role r ON ur.role_id = r.id INNER JOIN usuario u ON ur.usuario_id = u.id WHERE u.user = ? </module-option> </login-module> </authentication> </application-policy>
References:
http://www.jboss.org/community/wiki/DatabaseServerLoginModule
JBoss5_Installation_And_Getting_Started_Guide.pdf
Comentários