Creacion de Aplicaciones Seguras en J2EE Con Jaas y Wildfly Version 2
Creacion de Aplicaciones Seguras en J2EE Con Jaas y Wildfly Version 2
1
2. CONFIGURAR EL SERVIDOR WILDFLY PARA UTILIZAR HTTPS
2
Para probar el protocolo https, corra el servidor de aplicaciones wildfly
(standalone.bat o standalone.sh), abra un navegador y digite la dirección del
servidor (https://localhost:8443/).
3
Verifique el protocolo https y el puerto 8443 en la url.
4
3. AUTORIZACION Y AUTENTICACION
Para la creación de aplicaciones seguras con el servidor de aplicaciones wildfly
vamos a utilizar JAAS (Java Authentication and Authorization Service).
La siguiente sentencia SQL permite crear un nuevo role (usuario), que será el
propietario de la base de datos:
CREATE ROLE web201 WITH LOGIN PASSWORD 'web201';
5
6
También es posible utilizar el editor gráfico como se muestra a continuación:
Haga click con el boton derecho del mouse en la opción de Login Roles y seleccione
el ítem [New Login Role].
7
Si lo prefiere hacer por el entorno gráfico, haga click en la opción Databases y
seleccione el ítem [New Database…].
8
3.1.3 Crear las tablas:
Para crear las tablas (grupo, usuario y usuario_grupo) ejecute las siguientes
sentencias en el editor de SQL:
Tabla grupo
CREATE TABLE grupo
(
gru_codigo serial NOT NULL,
gru_nombre character varying (100),
gru_id character varying (100),
CONSTRAINT grupo_pkey PRIMARY KEY (gru_codigo)
)
WITH (
OIDS=FALSE
);
ALTER TABLE grupo
OWNER TO web201;
Tabla usuario
CREATE TABLE usuario
(
usu_codigo bigserial NOT NULL,
usu_nombre character varying (50),
usu_login character varying (20),
usu_password character varying (64),
CONSTRAINT usuario_pkey PRIMARY KEY (usu_codigo)
)
WITH (
OIDS=FALSE
);
ALTER TABLE usuario
OWNER TO web201;
Tabla usuario_grupo
CREATE TABLE usuario_grupo
(
usg_codigo bigserial NOT NULL,
usg_usuario bigint,
usg_grupo bigint,
CONSTRAINT usuario_grupo_pkey PRIMARY KEY (usg_codigo),
CONSTRAINT usuario_grupo_usg_grupo_fkey FOREIGN KEY (usg_grupo)
REFERENCES grupo (gru_codigo) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT usuario_grupo_usg_usuario_fkey FOREIGN KEY (usg_usuario)
REFERENCES usuario (usu_codigo) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
ALTER TABLE usuario_grupo
OWNER TO web201;
9
3.1.4 Alimentar la base de datos:
10
4. CRIPTOGRAFIA
Para encriptar una clave con el algoritmo md5 y mostrar el resultado con el encode
base64:
11
5. CONFIGURACION DEL SERVIDOR DE APLICACIONES WILDFLY
12
<connection-url>jdbc:postgresql://127.0.0.1:5432/web201</connection-
url>
<driver-class>org.postgresql.Driver</driver-class>
<driver>postgresql-9.4-1208.jar</driver>
<security>
<user-name>web201</user-name>
<password>web201</password>
</security>
</datasource>
13
6. CONFIGURACION DE LA APLICACION WEB
Aquí se hace un enlace a la carpeta /usuario, la cual está protegida (solo para
usuarios con el perfil ADMINISTRADOR o CLIENTE) y de esta manera se activará
el mecanismo de seguridad (dominio) que obliga al usuario a loguearse.
14
>> Cree el archivo de /login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sitio Seguro :: Login</title>
</head>
<body>
<h2>Ingresar al Sistema</h2>
<form action="j_security_check" method="post">
<p>Usuario: <input type="text" name="j_username"></p>
<p>Clave: <input type="password" name="j_password"></p>
<p><input type="submit" value="Ingresar al Sistema">
</form>
</body>
</html>
15
>> Cree el archivo /usuario/index.jsp
16
>> Cree el archivo /admin/index.jsp
17
6.4 ASEGURE LOS RECURSOS DE LA APLICACIÓN WEB
Adicione al archivo /WEB-INF/web.xml al final del archivo, antes de la etiqueta de
cierre </web-app> el siguiente contenido:
...
...
<security-constraint>
<display-name>Restricción de Seguridad - Administrador</display-name>
<web-resource-collection>
<web-resource-name>Area de Administrador</web-resource-name>
<url-pattern>/admin/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ADMINISTRADOR</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>Restricción de Seguridad - Cliente</display-name>
<web-resource-collection>
<web-resource-name>Area de Cliente</web-resource-name>
<url-pattern>/pedido/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>CLIENTE</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<security-constraint>
<display-name>Restricción de Seguridad - Usuarios</display-name>
<web-resource-collection>
<web-resource-name>Area de Usuarios</web-resource-name>
18
<url-pattern>/usuario/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ADMINISTRADOR</role-name>
<role-name>CLIENTE</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>seguridadWeb201</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<url-pattern>/admin/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
<auth-constraint>
<role-name>ADMINISTRADOR</role-name>
</auth-constraint>
19
Indica que los recursos definidos en el patrón url solo podrán ser utilizados por los
usuarios que tengan el perfil de ADMINISTRADOR.
20