<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JotaDeveloper &#187; Hibernate</title>
	<atom:link href="http://blog.jotadeveloper.com/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jotadeveloper.com</link>
	<description>Programacion y algo más</description>
	<lastBuildDate>Wed, 20 Jan 2010 09:28:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cómo crear el schema de la base datos con Anotaciones de Hibernate y Spring</title>
		<link>http://blog.jotadeveloper.com/2009/10/23/como-crear-el-schema-de-la-base-datos-con-anotaciones-de-hibernate-y-spring/</link>
		<comments>http://blog.jotadeveloper.com/2009/10/23/como-crear-el-schema-de-la-base-datos-con-anotaciones-de-hibernate-y-spring/#comments</comments>
		<pubDate>Sat, 24 Oct 2009 04:46:31 +0000</pubDate>
		<dc:creator>Jota</dc:creator>
				<category><![CDATA[Articulos]]></category>
		<category><![CDATA[Programación]]></category>
		<category><![CDATA[bases de datos]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://blog.jotadeveloper.com/?p=2963</guid>
		<description><![CDATA[A veces es necesario hacer cambios a nuestra base de datos, y siempre estaremos lidiando con las herramientas gráficas o desde la consola, con los cambios en la base de datos, pero existen formas de hacer este trabajo &#8220;sucio&#8221; mucho más rapido y ahorrando tiempo.
En nuestro caso, usaremos Spring 2.5 y Hibernate 3.3 y MySQL, [...]]]></description>
			<content:encoded><![CDATA[<p>A veces <strong>es necesario hacer cambios a nuestra base de datos</strong>, y siempre estaremos lidiando con las herramientas gráficas o desde la consola, con los cambios en la base de datos, pero existen formas de hacer este trabajo &#8220;sucio&#8221; <strong>mucho más rapido y ahorrando tiempo</strong>.</p>
<p>En nuestro caso, usaremos Spring 2.5 y Hibernate 3.3 y MySQL, donde es necesario ya tengas armado los beans de conexión a la base de datos. El siguiente código es un array de cadenas, si en nuestro caso tenemos segmentado los xml de spring, los añadiremos de la siguiente manera.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// array de string para agregar todos tus archivos de configuración de spring</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> SPRING_CONFIG_FILES <span style="color: #339933;">=</span>
<span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;source/spring/config/xxxxxx-db-context.xml&quot;</span>, <span style="color: #0000ff;">&quot;source/spring/config/xxxxxxx-param-context.xml&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A continuación, el código necesario para generar nuestra base de datos, el <strong>FileSystemXmlApplicationContext</strong> carga el Contexto de la Aplicación, y una vez teniendo el contexto obtenemos el SessionFactory, que en nuestro caso usamos Anotaciones, creamos el bean en base a la clase <strong>AnnotationSessionFactoryBean</strong> , esto es importante pues en base a esta clase usaremos los metodos <strong>dropDatabaseSchema();</strong> para borrar el actual schema (tablas, relaciones , etc) y luego ejecutamos <strong>createDatabaseSchema();</strong> para crear el nuevo schema.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// carga el application context de Spring</span>
 FileSystemXmlApplicationContext appContext <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FileSystemXmlApplicationContext<span style="color: #009900;">&#40;</span>SPRING_CONFIG_FILES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Obtiene el bean del session factory</span>
 AnnotationSessionFactoryBean annotationSF <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>AnnotationSessionFactoryBean<span style="color: #009900;">&#41;</span>  appContext.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;sessionFactory&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// si quieres eliminar el schema de la base de datos</span>
 annotationSF.<span style="color: #006633;">dropDatabaseSchema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// para crear nuevamente el schema de la base de datos</span>
 annotationSF.<span style="color: #006633;">createDatabaseSchema</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Los archivos de configuración de <strong>Spring</strong>, deben lucir de la siguiente manera, hay que destacar el <strong>hibernate.dialect</strong> donde indicamos el dialecto que usaremos para crear el schema,  en el caso de MySQL puede ser MySAM o InnoDB, luego el parametro  <strong>hibernate.hbm2ddl.delimiter</strong> que también indicamos el tipo de tablas que usaremos en mi caso usaré <strong>type=InnoDB</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.jdbc.datasource.DriverManagerDataSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;driverClassName&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${datasource.classname}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;url&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${datasource.urldb}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${datasource.userbd}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;${datasource.pass}&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;annotatedClasses&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clase1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
              <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Clase2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;hibernateProperties&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
           <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #808080; font-style: italic;">&lt;!-- &lt;prop key=&quot;hibernate.hbm2ddl.auto&quot;&gt;update&lt;/prop&gt; --&gt;</span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;hibernate.dialect&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>${datasource.dialect}
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;hibernate.show_sql&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>${datasource.showsql}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;prop</span> <span style="color: #000066;">key</span>=<span style="color: #ff0000;">&quot;hibernate.hbm2ddl.delimiter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>${datasource.delimited}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/prop<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/props<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

 <img src="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?view=1&post_id=2963" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.jotadeveloper.com/2009/10/23/como-crear-el-schema-de-la-base-datos-con-anotaciones-de-hibernate-y-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Security 2, la configuración a la medida con DAO Hibernate IV</title>
		<link>http://blog.jotadeveloper.com/2009/06/23/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-iv/</link>
		<comments>http://blog.jotadeveloper.com/2009/06/23/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-iv/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 16:08:12 +0000</pubDate>
		<dc:creator>Jota</dc:creator>
				<category><![CDATA[Articulos]]></category>
		<category><![CDATA[Programación]]></category>
		<category><![CDATA[filtros]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring security]]></category>

		<guid isPermaLink="false">http://blog.jotadeveloper.com/?p=2143</guid>
		<description><![CDATA[Más filtros de los que explicamos en el artículo anterior se puede usar en Spring Security, donde vimos los filtros esenciales para la configuración de nuestra capa de seguridad.
AnonymousProcessingFilter
Este filtro es muy util, nos permite darle acceso a un usuarios anónimos a secciones del sistema, digamos que la pagina de registro debe ser de acceso [...]]]></description>
			<content:encoded><![CDATA[<p>Más filtros de los que explicamos en el <a href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vMjAwOS8wNS8yNS9zcHJpbmctc2VjdXJpdHktMi1sYS1jb25maWd1cmFjaW9uLWEtbGEtbWVkaWRhLWNvbi1kYW8taGliZXJuYXRlLWlpaS8=" target=\"_blank\">artículo anterior</a> se puede usar en Spring Security, donde vimos los filtros esenciales para la configuración de nuestra capa de seguridad.</p>
<h2>AnonymousProcessingFilter</h2>
<p>Este filtro es muy util, nos permite darle acceso a un usuarios anónimos a secciones del sistema, digamos que la pagina de registro debe ser de acceso anónimo, en el <strong>FilterChainProxy</strong>  agregamos otra linea donde colocamos el recurso y le asignamos el filtro, ese recurso sera accesible siempre y cuando en el <strong>FilterSecurityInterceptor </strong> también podemos hacer uso del Rol que indicamos que seran Roles para identificar al usuario anónimo.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;anonymousProcessingFilter&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.providers.anonymous.AnonymousProcessingFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;key&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MU7kyU0he1MvXEDZ9Mdj7MVvkXOXJ8uRgtg/Xb/3eJyW0HZa3csBoyvinGEC4vmi<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;userAttribute&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>anonymousUser,ENCUESTAME_ANONYMOUS
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2> ExceptionTranslationFilter </h2>
<p>Maneja cualquier <strong>AccessDeniedException </strong>y <strong>AuthenticationException </strong> arrojados dentro de la cadena de filtro. Este filtro es necesario, ya que proporciona el puente entre las excepciones de Java y las respuestas HTTP.  Sólo se ocupan de mantener la interfaz de usuario. Este filtro no hace ninguna garantía real de la ejecución. Para utilizar este filtro, es necesario especificar las siguientes propiedades:</p>
<ul>
<li><strong>authenticationEntryPoint </strong>indica que el controlador debe comenzar el proceso de autenticación si un AuthenticationException se detecta. Tenga en cuenta que esto también puede cambiar el actual protocolo de http a https para un inicio de sesión SSL.</li>
<li><strong>portResolver</strong> (opcional) Se utiliza para determinar el &#8220;verdadero&#8221; puerto por la que fue recibida la petición.</li>
<ul>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;exceptionTranslationFilter&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.ExceptionTranslationFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;authenticationEntryPoint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">local</span>=<span style="color: #ff0000;">&quot;authenticationEntryPoint&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2> BasicProcessingFilterEntryPoint</h2>
<p>Este filtro es responsable de la tramitación de cualquier solicitud que tiene un encabezado de petición HTTP Authorization con un sistema de autenticación Basica y una codificación username:password simbólico. Por ejemplo, para autenticar el usuario &#8220;Aladdin&#8221;, con contraseña &#8220;ábrete Sésamo&#8221; el siguiente encabezado se mostrara como el siguiente ejemplo, (<strong>Autorización: Básica QWxhZGRpbjpvcGVuIHNlc2FtZQ == </strong> ). Si la autorización es satisfactoria, el objeto<strong>Authentication </strong>se colocará en el <strong>SecurityContextHolder.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;authenticationEntryPoint&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.basicauth.BasicProcessingFilterEntryPoint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;realmName&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>EmForge<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>LogoutFilter</h2>
<p>Se necesita una forma de cerrar sesión, el <strong>LogoutFilter</strong> es la solución perfecta, indicamos en el constructor del bean el lugar donde seremos dirigidos cuando cerremos la sesión, y una lista de beans o manejadores de Cerrar sesión, por defecto se debe usar el SecurityContextLogoutHandler, aunque puedes personalizar el tuyo propio.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;logoutFilter&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.logout.LogoutFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
	 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/pages/index.me&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #808080; font-style: italic;">&lt;!--  &lt;ref bean=&quot;rememberMeServices&quot;/&gt;  --&gt;</span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span></span>
<span style="color: #009900;">					<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.logout.SecurityContextLogoutHandler&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/constructor-arg<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>AuthenticationProcessingFilterEntryPoint</h2>
<p>AuthenticationProcessingFilterEntryPoint, es el bean que nos redirecionará a la pantalla de la aplicación donde se inicia sesión, o a donde nosotros queramos por eso la propiedad <strong>loginFormUrl</strong>. En el caso en que los credenciales no sean correctos se utliza el objeto <strong>AccessDeniedHandlerImpl</strong>, y en su propiedad errorPage se le especifica a que camino se debe dirigir la aplicación en caso de error.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;formEntryPoint&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;loginFormUrl&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/login.faces&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
   	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Estos son los filtros más usados y que podemos aplicar a nuestra seguridad, junto con los explicados anteriormente son una <strong>magnifica opción</strong> para crear nuestra seguridad, y como ven, con muy poca programación. En el <strong>siguiente artícul</strong>o veremos la integración del <strong>OPENID</strong> en Spring Security.</p>
 <img src="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?view=1&post_id=2143" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.jotadeveloper.com/2009/06/23/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-iv/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Spring Security 2, la configuración a la medida con DAO Hibernate III</title>
		<link>http://blog.jotadeveloper.com/2009/05/25/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-iii/</link>
		<comments>http://blog.jotadeveloper.com/2009/05/25/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-iii/#comments</comments>
		<pubDate>Mon, 25 May 2009 15:32:11 +0000</pubDate>
		<dc:creator>Jota</dc:creator>
				<category><![CDATA[Articulos]]></category>
		<category><![CDATA[filtros]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[Seguridad]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring security]]></category>

		<guid isPermaLink="false">http://blog.jotadeveloper.com/?p=1723</guid>
		<description><![CDATA[El dia de hoy vamos a continuar con los Filtros, es la parte central de la seguridad de Spring, y por eso la hace tan flexible, porque esta completamente separada del modelo de Negocio, eso es lo que lo hace tan genial, pues si el dia de mañana se te ocurre cambiar toda la seguridad, [...]]]></description>
			<content:encoded><![CDATA[<p>El dia de hoy vamos a continuar con los Filtros, es la parte central de la <strong>seguridad de Spring</strong>, y por eso la hace tan flexible, porque esta completamente separada del modelo de Negocio, eso es lo que lo hace tan genial, pues si el dia de mañana se te ocurre cambiar toda la seguridad, tu sistema no sufrirá grandes cambios.</p>
<p><strong>Los filtros se encargan de la seguridad de la aplicación</strong>. Existen tres filtros fundamentales se <strong>encadenan</strong> juntos mediante un objeto llamado &#8220;filterChainProxy&#8221;, que crea e inicializa los tres filtros; como se ve en el siguiente diagrama.
</p>
<p><a href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMDkvMDUvZmlsdHJvc2FjZWdpLmpwZw=="><img class="size-full wp-image-1724 alignnone" title="filtrosacegi" src="http://blog.jotadeveloper.com/wp-content/uploads/2009/05/filtrosacegi.jpg" alt="filtrosacegi" width="592" height="324" /></a></p>
<ol>
<li> El filtro <strong>AuthenticationProcessingFilter</strong> maneja la petición o requerimiento (request) que chequea la autenticación -Authentication Request Check- (&#8220;el login de la aplicación&#8221;). Para ello usa el <strong>AuthenticationManager </strong>que vimos en el articulo anterior.</li>
<li> El filtro <strong>HttpSessionContextIntegrationFilter </strong>mantiene el objeto Authentication entre varios requests y se lo pasa al <strong>AuthenticationManager</strong> y al <strong>AccessDecisionManager </strong>cuando sea necesario.</li>
<li> El filtro <strong>ExceptonTranslationFilter</strong> verifica la existencia de autenticación , maneja las excepciones de seguridad y ejecuta la acción apropiada. El ExceptonTranslationFilter depende del filtro siguiente, <strong>FilterSecurityInterceptor.</strong></li>
<li> <strong>FilterSecurityInterceptor </strong>controla el acceso restricto a recursos determinados , y el chequeo de autorización.  Conoce <em>qué recursos son seguros y qué roles tienen acceso a ellos</em>.  FilterSecurityInterceptor usa el <strong>AuthenticationManager </strong>y el  <strong>AccessDecisionManager </strong>para hacer su trabajo.</li>
</ol>
<p>Cuando inicias, todo esto es una maraña de filtros sin sentido, pero vamos a profundizar un poco para que te quede más claro la funcion y configuración de cada uno de ellos.</p>
<h2>AuthenticationProcessingFilter</h2>
<p>El primer filtro donde pasa el RequestHTTP. Este filtro se especializa en manejar el request de autentificación, valida el usuario y la contraseña, más alla de esto solo debes conocer otros parametros imporantes.</p>
<ul>
<li> <strong>authenticationFailureUrl: </strong> En el caso de fallo, algún lugar debe de ir cuando no se logea el usuario.</li>
<li> <strong>defaultTargetUrl:</strong> Es el URL por defecto, generalmente es la raiz.</li>
<li> <strong>filterProcessesUrl:</strong> Es a quien le encarga la responsbilidad de verificar si el usuario se logea o no..</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;authenticationProcessingFilter&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.webapp.AuthenticationProcessingFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;authenticationManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;authenticationManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;authenticationFailureUrl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/login.me<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;defaultTargetUrl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;filterProcessesUrl&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/j_spring_security_check<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>HttpSessionContextIntegrationFilter</h2>
<p>El HttpSessionContextIntegrationFilter es fácil de configurar. Su única función, es propagar por el Contexto de Seguridad la autenticación a través de todas las solicitudes. No necesita propiedades ni nada en especial.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;httpSessionContextIntegrationFilter&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.context.HttpSessionContextIntegrationFilter&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<h2>ExceptionTranslationFilter</h2>
<p>El filtro ExceptionTranslationFilter  intercepta cualquier error de autenticación o autorización, por ejemplo <strong>UsernameNotFoundException</strong> o <strong>DataAccessException</strong>.</p>
<p>Si la excepción fue causada por una excepción de autorización lanzada por el filtro FilterSecurityInterceptor (puede ser porque no tiene permisos para acceder a un Recurso Web, una imagen o un URL), el filtro lanzará un HTTP 403 al navegador, el cual mostrará una página de acceso no autorizado.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;formExceptionTranslationFilter&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.ExceptionTranslationFilter&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;authenticationEntryPoint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ref</span> <span style="color: #000066;">local</span>=<span style="color: #ff0000;">&quot;formEntryPoint&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;formEntryPoint&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;loginFormUrl&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/login.me&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>FilterSecurityInterceptor</h2>
<p>El FilterSecurityInterceptor, es donde protegeremos todos nuestros recursos, donde decidimos que ROL entra a ciertos recursos, cuales pueden ser accedido por usuarios anónimos, todo esto se configura en el <strong>objectDefinitionSource</strong>. Necesitamos 2 referencias para configurar este Filtro, el <strong>authenticationManager</strong> y el bean <strong>accessDecisionManager</strong> que miraremos en el siguiente artículo.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;filterInvocationInterceptor&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.intercept.web.FilterSecurityInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;authenticationManager&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;authenticationManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;accessDecisionManager&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;voteAccessDecisionManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;objectDefinitionSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
				PATTERN_TYPE_APACHE_ANT
				/=ENCUESTAME_ANONYMOUS
				/pages/**=ENCUESTAME_USER
				/pages/admon/**=ENCUESTAME_ADMIN
				/user/**=ENCUESTAME_ANONYMOUS,ENCUESTAME_USER,ENCUESTAME_ADMIN				           
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>FilterChainProxy</h2>
<p>El filtro inicializador, su función principal es indicar o personalizar, que recursos ejecutaran los filtros deseados en <strong>filterInvocationDefinitionSource</strong>, por ejemplo, si tenemos un sevlet<strong> /uploadFile</strong> y solo nos interesa aplicar algunos filtros</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;springSecurityFilterChain&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.security.util.FilterChainProxy&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;filterInvocationDefinitionSource&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		   CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
		    PATTERN_TYPE_APACHE_ANT
               /**=httpSessionContextIntegrationFilter,logoutFilter,basicProcessingFilter,authenticationProcessingFilter....
              /uploadFile= basicProcessingFilter,OtroFiltroPersonalizado
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>En el siguiente artículo, veremos una extensión de los diferentes filtros opcionales que podemos integrar en nuestra seguridad, <strong>¿preguntas?</strong></p>
 <img src="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?view=1&post_id=1723" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.jotadeveloper.com/2009/05/25/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Security 2, la configuración a la medida con DAO Hibernate II</title>
		<link>http://blog.jotadeveloper.com/2009/05/13/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-ii/</link>
		<comments>http://blog.jotadeveloper.com/2009/05/13/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-ii/#comments</comments>
		<pubDate>Wed, 13 May 2009 06:24:10 +0000</pubDate>
		<dc:creator>Jota</dc:creator>
				<category><![CDATA[Articulos]]></category>
		<category><![CDATA[filtros]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[Seguridad]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring security]]></category>

		<guid isPermaLink="false">http://blog.jotadeveloper.com/?p=1649</guid>
		<description><![CDATA[Siguiendo con el articulo anterior, voy a explicar los diferentes Filtros de Seguridad y las Configuraciones Personales que podemos aplicar en nuestra Seguridad. El siguiente gráfico explica el ciclo de vida de la autentificación en Spring.
En el post anterior, vimos el Bean del Formulario de logeo, donde se creo el objeto Authentication que se pasa [...]]]></description>
			<content:encoded><![CDATA[<p>Siguiendo con el articulo anterior, voy a explicar los diferentes <strong>Filtros de Seguridad y las Configuraciones Personales </strong>que podemos aplicar en nuestra Seguridad. El siguiente gráfico explica el ciclo de vida de la autentificación en Spring.</p>
<p>En el post anterior, vimos el Bean del Formulario de logeo, donde se creo el objeto <strong>Authentication </strong>que se pasa al <strong>AuthenticationManager</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">UsernamePasswordAuthenticationToken authReq <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UsernamePasswordAuthenticationToken<span style="color: #009900;">&#40;</span>userName, password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
.......
<span style="color: #006633;">Authentication</span> auth <span style="color: #339933;">=</span> getAuthenticationManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">authenticate</span><span style="color: #009900;">&#40;</span>authReq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><a href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMDkvMDUvc3ByaW5nX2V4Y2VwdGlvbl9maWx0ZXIuZ2lm"><img class="alignnone size-full wp-image-1650" title="spring_exception_filter" src="http://blog.jotadeveloper.com/wp-content/uploads/2009/05/spring_exception_filter.gif" alt="spring_exception_filter" width="637" height="400" /></a></p>
<h3>AuthenticationManager</h3>
<p>El bean AuthenticationManager es del tipo <strong>ProviderManager</strong>, lo que significa que actúa de proxy con AuthenticationProvider. En Spring, el <strong>AuthenticationProvider es el encargado de validar la combinación nombre de usuario/contraseña por medio del objeto Authentication  y retornar los roles asociados a dicho usuario.</strong> Esta clase tan sólo delega la autenticación en una lista de proveedores configurable, cada uno de los cuales implementa el interfaz AuthenticationProvider. Hay muchos tipos de AuthenticationProvider( JDBC, Hibernate,  LDAP, RememberMe, OpenID). Usted puede indicar cuales quiere usar son los proveedores de autentificación que desee usar, en nuestro caso, <strong>usaremos un DAO Hibernate personalizado</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>bean id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;authenticationManager&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;paquete.MiAuthenticationManager&quot;</span><span style="color: #339933;">&gt;</span>
	<span style="color: #339933;">&lt;</span>property name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;providerString&quot;</span> value<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userDaoProvider&quot;</span> <span style="color: #339933;">/&gt;</span>
<span style="color: #339933;">&lt;/</span>bean<span style="color: #339933;">&gt;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MiAuthenticationManager <span style="color: #000000; font-weight: bold;">extends</span> ProviderManager <span style="color: #009900;">&#123;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> providerString<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setProviderString<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> providerString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">providerString</span> <span style="color: #339933;">=</span> providerString<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
 <span style="color: #008000; font-style: italic; font-weight: bold;">/**
  * Agrega al Manejador de Proveedores un listado
  */</span> 
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> afterPropertiesSet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>providerString <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>			
			List<span style="color: #339933;">&lt;</span>authenticationprovider<span style="color: #339933;">&gt;</span> providers <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">LinkedList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> names <span style="color: #339933;">=</span> providerString.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
			<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> providerUnit <span style="color: #339933;">:</span> names<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				AuthenticationProvider provider <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>AuthenticationProvider<span style="color: #009900;">&#41;</span> applicationContext
						.<span style="color: #006633;">getBean</span><span style="color: #009900;">&#40;</span>providerUnit.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>provider <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
					<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> EnMeExpcetion<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;AuthenticationProvider &quot;</span>
							<span style="color: #339933;">+</span> providerUnit <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; don't exist&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>				
				providers.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>provider<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
			setProviders<span style="color: #009900;">&#40;</span>providers<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">afterPropertiesSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">&lt;/</span>authenticationprovider<span style="color: #339933;">&gt;</span></pre></div></div>

<h3>DaoAuthenticationProvider </h3>
<p>DaoAuthenticationProvider, es el comúnmente usado puesto que es el que permite acceder a la información almacenada en una base de datos. El proveedor DaoAuthenticationProvider merece una mención especial. Esta implementación delega a su vez en un objeto de tipo <strong>UserDetailsService</strong>, un interfaz que define un objeto de acceso a datos con un único método <strong>loadUserByUsername </strong>que permite obtener la información de un usuario a partir de su nombre de usuario.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>bean id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userDaoProvider&quot;</span>
		<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;org.springframework.security.providers.dao.DaoAuthenticationProvider&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>property name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userDetailsService&quot;</span> ref<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dbUserService&quot;</span> <span style="color: #339933;">/&gt;</span>		
	<span style="color: #339933;">&lt;/</span>bean<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Si deseas agregarle encriptación a la contraseña del usuario, agregale una propiedad más al userDaoProvider, <a href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vMjAwOC8xMS8yOS9lbmNyaXB0YWNpb24tamFweXN0LXNwcmluZy1zZWMtMi8=">este artículo que escribi tiempo atrás te ayudará a poner una mejor seguridad que el tipico MD5</a>. </p>
<h3>UserDetailsService, un Dao Personalizado</h3>
<p>Si no queremos que Spring acceda a la bases de datos directamente podemos configurar un Dao personalizado por medio de una Implementación UserDetailsService, , un interfaz que define un objeto de acceso a datos con un único método loadUserByUsername que permite obtener la información de un usuario a partir de su nombre de usuario.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #339933;">&lt;</span>bean id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dbUserService&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;paquete.MiUserServiceImp&quot;</span><span style="color: #339933;">&gt;</span>
		<span style="color: #339933;">&lt;</span>property name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userDao&quot;</span> ref<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;userDao&quot;</span> <span style="color: #339933;">/&gt;</span>		
	<span style="color: #339933;">&lt;/</span>bean<span style="color: #339933;">&gt;</span></pre></div></div>

<p><!-- Un Dao Cualquiera para Hibernate --><br />
<bean id="userDao" class="paquete.dao.UserDaoImp"></p>
<property name="hibernateTemplate">
			<ref bean="hibernateTemplate" />
		</property>
</bean></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MiUserServiceImp <span style="color: #000000; font-weight: bold;">implements</span> UserDetailsService <span style="color: #009900;">&#123;</span>
         ......
        <span style="color: #000000; font-weight: bold;">public</span> UserDetails loadUserByUsername<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> username<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> UsernameNotFoundException, DataAccessException <span style="color: #009900;">&#123;</span>		
		SecUsers user <span style="color: #339933;">=</span> userDao.<span style="color: #006633;">getUser</span><span style="color: #009900;">&#40;</span>username<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>user <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no encontrado...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> UsernameNotFoundException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>		
		<span style="color: #000000; font-weight: bold;">return</span> convertToUserDetails<span style="color: #009900;">&#40;</span>user<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
         ......
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> UserDetails convertToUserDetails<span style="color: #009900;">&#40;</span>SecUsers user<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">//lista de permisos, </span>
         List<span style="color: #339933;">&lt;</span>string<span style="color: #339933;">&gt;</span> listPermissions <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;&lt;</span>string<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ...........
        <span style="color: #006633;">GrantedAuthority</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> authorities <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GrantedAuthority<span style="color: #009900;">&#91;</span>listPermissions
				.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> permission <span style="color: #339933;">:</span> listPermissions<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			authorities<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GrantedAuthorityImpl<span style="color: #009900;">&#40;</span>permission.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		User userDetails <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span>user.<span style="color: #006633;">getUsername</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, user.<span style="color: #006633;">getPassword</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>,
				user.<span style="color: #006633;">isStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">?</span> <span style="color: #000066; font-weight: bold;">false</span> <span style="color: #339933;">:</span> user.<span style="color: #006633;">isStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #000066; font-weight: bold;">true</span>, 
				<span style="color: #000066; font-weight: bold;">true</span>, 
				<span style="color: #000066; font-weight: bold;">true</span>,
				authorities<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		log.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;userDetails &quot;</span><span style="color: #339933;">+</span>userDetails<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> userDetails<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>string<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Vamos a dejar la explicación de los Filtros, para una tercera parte, ya se hizo largo <img src='http://blog.jotadeveloper.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
 <img src="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?view=1&post_id=1649" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.jotadeveloper.com/2009/05/13/spring-security-2-la-configuracion-a-la-medida-con-dao-hibernate-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Spring Security 2, la configuración a la medida con DAO Hibernate I</title>
		<link>http://blog.jotadeveloper.com/2009/05/11/spring-security-2-hibernate-dao-part/</link>
		<comments>http://blog.jotadeveloper.com/2009/05/11/spring-security-2-hibernate-dao-part/#comments</comments>
		<pubDate>Mon, 11 May 2009 15:38:48 +0000</pubDate>
		<dc:creator>Jota</dc:creator>
				<category><![CDATA[Articulos]]></category>
		<category><![CDATA[filtros]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[jsf]]></category>
		<category><![CDATA[Seguridad]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring security]]></category>

		<guid isPermaLink="false">http://blog.jotadeveloper.com/?p=1617</guid>
		<description><![CDATA[Uno de los aspectos que toda aplicación debe considerar es la seguridad, entendiendo como tal la necesidad de saber que el usuario es quien dice ser (autenticación), y permitirle acceso sólo a aquellos recursos necesarios (autorización). En un princio el framework  se llamaba Acegi Security e inicio en el 2003, 5 años después en Abril [...]]]></description>
			<content:encoded><![CDATA[<p>Uno de los aspectos que toda aplicación debe considerar es la seguridad, entendiendo como tal la necesidad de saber que el usuario es quien dice ser (autenticación), y permitirle acceso sólo a aquellos recursos necesarios (autorización). En un princio el framework  se llamaba Acegi Security e inicio en el 2003, 5 años después en Abril del 2008 se incorporó al portafolio de Spring Framework como un súbmodulo. Spring trae por defecto ciertas caracteristicas que a la hora de querer integrarlas en nuestros sistemas avanzados no encajan a la perfección y por lo general se necesitan modificaciones a las clases principales.</p>
<p>Anteriormente hemos visto configuraciones mucho más sencillas :</p>
<ul>
<li> <a title=\"Permanent Link to Configuración Spring Security 2.0 con JDBC\" rel=\"bookmark\" href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vMjAwOC8xMS8yMS9jb25maWd1cmFjaW9uLXNwcmluZy1zZWN1cml0eS0yMC1jb24tamRiYy8=">Configuración Spring Security 2.0 con JDBC</a></li>
<li> <a title=\"Permanent Link to Configuración Spring Security 2.04 para LDAP\" rel=\"bookmark\" href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vMjAwOC8xMS8xNy9jb25maWd1cmFjaW9uLXNwcmluZy1zZWN1cml0eS0yMDQtcGFyYS1sZGFwLw==">Configuración Spring Security 2.04 para LDAP</a></li>
<li> <a title=\"Permanent Link to La Seguridad Perfecta con Spring y el dropDownMenu de RichFaces\" rel=\"bookmark\" href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vMjAwOC8xMi8wNy9sYS1zZWd1cmlkYWQtcGVyZmVjdGEtY29uLXNwcmluZy15LWVsLWRyb3Bkb3dubWVudS1kZS1yaWNoZmFjZXMv">La Seguridad Perfecta con Spring y el dropDownMenu de RichFaces</a></li>
</ul>
<p>Creo que una de las configuraciones más complejas es fusionando con el framework Hibernate y esto nos permite no anclarnos a las tablas que ya Spring Security trae por defecto, asi poder personalizar nuestra seguridad de una mejor manera.</p>
<p><a href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vd3AtY29udGVudC91cGxvYWRzLzIwMDkvMDUvdW1sLnBuZw=="><img class="size-full wp-image-1625 alignnone" title="uml" src="http://blog.jotadeveloper.com/wp-content/uploads/2009/05/uml.png" alt="uml" width="520" height="326" /></a></p>
<p>En nuestro caso vamos a trabajar con Spring Framework, y lo ideal es separar nuestra aplicación en 3 capas:</p>
<ul>
<li><strong>Capa de Presentación: </strong>En esta capa vamos a programar el formulario, el típico formulario con el nombre de usuario y la contraseña, podemos integrar OPEN ID, e incluso la funcionalidad de Recordar la Sessión por medio de un Cookie.</li>
<li><strong>Filtros de Seguridad: </strong>Aqui aplicaremos toda la configuración de Spring Security, aplicaremos todos los filtros y las clases que vamos a modificar algunas clases de Spring que se comuniquen con nuestros servicios en la capa de negocio.</li>
<li><strong>Servicios en la Capa de Negocio</strong>: En esta capa colocaremos nuestros Beans de Spring que a su vez acceden a otra capa en la cual no vamos a entrar en detalle, la de acceso a datos.</li>
</ul>
<p>Iniciemos con la capa de presentación y el formulario para logearse, en su forma más sencilla, aqui se pueden agregar validadores y mensajes de error, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h</span> :inputText <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;j_username&quot;</span>  <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{loginForm.userName}&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;40&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h</span> :inputSecret <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;j_password&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;#{loginForm.userPassword}&quot;</span> <span style="color: #000066;">size</span>=<span style="color: #ff0000;">&quot;40&quot;</span> <span style="color: #000066;">required</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h</span> :commandButton <span style="color: #000066;">action</span>=<span style="color: #ff0000;">&quot;#{loginForm.login}&quot;</span>  <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;Iniciar Sesión&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>Ahora necesitamos nuestro Bean LoginForm, este puedes colocarlo como un ManageBean o un Bean de Spring, como mas te parezca, en mi caso use Spring.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> LoginForm<span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Bean Personalizado del Manejador de Autentificación</span>
	<span style="color: #000000; font-weight: bold;">private</span> AuthenticationManager authenticationManager<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> userName<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> userPassword<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Application app<span style="color: #339933;">;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getUserName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> userName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUserName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userName</span> <span style="color: #339933;">=</span> userName<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getUserPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> userPassword<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUserPassword<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> userPassword<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">userPassword</span> <span style="color: #339933;">=</span> userPassword<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> AuthenticationManager getAuthenticationManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> authenticationManager<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAuthenticationManager<span style="color: #009900;">&#40;</span>
			AuthenticationManager authenticationManager<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">authenticationManager</span> <span style="color: #339933;">=</span> authenticationManager<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 
&nbsp;
	/**
	 * Ejecuta un envio a un url definido por la navegación JSF
	 * @param viewId
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> forward<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> viewId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    	ViewHandler viewHandler <span style="color: #339933;">=</span> getApplication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getViewHandler</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    FacesContext facesCtx <span style="color: #339933;">=</span> getFacesContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    UIViewRoot view <span style="color: #339933;">=</span> viewHandler.<span style="color: #006633;">createView</span><span style="color: #009900;">&#40;</span>facesCtx, viewId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    facesCtx.<span style="color: #006633;">setViewRoot</span><span style="color: #009900;">&#40;</span>view<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    facesCtx.<span style="color: #006633;">renderResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Obtiene el contexto de la Aplicación
	 * @return
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> Application getApplication<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>app <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			ApplicationFactory appFactory <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ApplicationFactory<span style="color: #009900;">&#41;</span> FactoryFinder.<span style="color: #006633;">getFactory</span><span style="color: #009900;">&#40;</span>FactoryFinder.<span style="color: #006633;">APPLICATION_FACTORY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        app <span style="color: #339933;">=</span> appFactory.<span style="color: #006633;">getApplication</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> 
		<span style="color: #000000; font-weight: bold;">return</span> app<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Obtiene el Contexto de JSF
	 * @return
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> FacesContext getFacesContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Obtiene la Petición del HttpServletRequest
	 * @return
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> HttpServletRequest getRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		ExternalContext context <span style="color: #339933;">=</span> 
			FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    HttpServletRequest request <span style="color: #339933;">=</span> 
	    	<span style="color: #009900;">&#40;</span>HttpServletRequest<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	    
	    <span style="color: #000000; font-weight: bold;">return</span> request<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Obtiene la Respuesta del HttpServletResponse	
	 * @return
	 */</span>
	<span style="color: #000000; font-weight: bold;">protected</span> HttpServletResponse getResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		ExternalContext context <span style="color: #339933;">=</span> 
			FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		HttpServletResponse response <span style="color: #339933;">=</span> 
	    	<span style="color: #009900;">&#40;</span>HttpServletResponse<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #000000; font-weight: bold;">return</span> response<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		HttpServletRequest request <span style="color: #339933;">=</span> getRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
&nbsp;
               <span style="color: #008000; font-style: italic; font-weight: bold;">/**
                 * Obtenemos del Formulario los Datos, por mayor seguridad podriamos agregar filtros contra XSS e Inyección HQL
                 * Creamos un token para Spring
                 */</span>
	    	<span style="color: #003399;">String</span> userName <span style="color: #339933;">=</span> getUserName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    	<span style="color: #003399;">String</span> password <span style="color: #339933;">=</span> getUserPassword<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
                 * Creamos un token para Spring
                 */</span>
	    	UsernamePasswordAuthenticationToken authReq <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> UsernamePasswordAuthenticationToken<span style="color: #009900;">&#40;</span>userName, password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
               <span style="color: #008000; font-style: italic; font-weight: bold;">/**
                 * Le agregamos al token el request de HttpServletRequest
                 */</span>
	    	authReq.<span style="color: #006633;">setDetails</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> WebAuthenticationDetails<span style="color: #009900;">&#40;</span>request<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>			    	
	    	HttpSession session <span style="color: #339933;">=</span> request.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
               <span style="color: #008000; font-style: italic; font-weight: bold;">/**
                 * Asignamos la sesión el atributo UserName
                 * Obtenemos el manager auth y le asignamos el token
                 */</span>
	    	session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>AuthenticationProcessingFilter.<span style="color: #006633;">SPRING_SECURITY_LAST_USERNAME_KEY</span>, userName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    	Authentication auth <span style="color: #339933;">=</span> getAuthenticationManager<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">authenticate</span><span style="color: #009900;">&#40;</span>authReq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	    	
&nbsp;
                  <span style="color: #008000; font-style: italic; font-weight: bold;">/**
                 * Obtenemos el Contexto de Spring Security
                 * Le asignamos el Autentication Manager al Contexto de Seguridad
                 */</span>	
&nbsp;
	    	SecurityContext secCtx <span style="color: #339933;">=</span> SecurityContextHolder.<span style="color: #006633;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	    	
	    	secCtx.<span style="color: #006633;">setAuthentication</span><span style="color: #009900;">&#40;</span>auth<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    	session.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>HttpSessionContextIntegrationFilter.<span style="color: #006633;">SPRING_SECURITY_CONTEXT_KEY</span>, secCtx<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   			    	
&nbsp;
	    	<span style="color: #003399;">String</span> urlKey <span style="color: #339933;">=</span> AbstractProcessingFilter.<span style="color: #006633;">SPRING_SECURITY_SAVED_REQUEST_KEY</span><span style="color: #339933;">;</span>	   
	    	SavedRequest savedRequest <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>SavedRequest<span style="color: #009900;">&#41;</span>session.<span style="color: #006633;">getAttribute</span><span style="color: #009900;">&#40;</span>urlKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    	session.<span style="color: #006633;">removeAttribute</span><span style="color: #009900;">&#40;</span>urlKey<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    	<span style="color: #003399;">String</span> target <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/index.jsf&quot;</span><span style="color: #339933;">;</span>
	    	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
                  * Si perdiste la sesión en una página en particular, este parametro te ayudara a regresar a donde perdiste la sesión
                 */</span>
	    	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>savedRequest <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>	    		
		    	<span style="color: #003399;">String</span> targetUrl <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	    		targetUrl <span style="color: #339933;">=</span> savedRequest.<span style="color: #006633;">getFullRequestUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	    		
	    		FacesContext.<span style="color: #006633;">getCurrentInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getExternalContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">redirect</span><span style="color: #009900;">&#40;</span>targetUrl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	    		
	    		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>	    		
	    	<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
	    		.................		  
	    	<span style="color: #009900;">&#125;</span>
	    	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
                 * Si no existiera un SavedRequest, entonces te enviaria a la pagina de inicio
                  */</span>
	    	forward<span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>BadCredentialsException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    	...............	    	
	    	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">AuthenticationException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    	............
	    	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> ioException<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	       ..............
	    <span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;index&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>	
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Y como lo vamos a integrar con Spring, la declaración del Bean en el contexto</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;loginForm&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.xxx.LoginForm&quot;</span> <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;authenticationManager&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;authenticationManager&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>En la <a href="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?url=aHR0cDovL2Jsb2cuam90YWRldmVsb3Blci5jb20vMjAwOS8wNS8xMy9zcHJpbmctc2VjdXJpdHktMi1sYS1jb25maWd1cmFjaW9uLWEtbGEtbWVkaWRhLWNvbi1kYW8taGliZXJuYXRlLWlpLw==">parte II miraremos los Filtros de Seguridad.</a></p>
 <img src="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?view=1&post_id=1617" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.jotadeveloper.com/2009/05/11/spring-security-2-hibernate-dao-part/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Relación Muchos a Muchos en Hibernate 3.2</title>
		<link>http://blog.jotadeveloper.com/2008/10/04/relacion-muchos-a-muchos-en-hibernate-32/</link>
		<comments>http://blog.jotadeveloper.com/2008/10/04/relacion-muchos-a-muchos-en-hibernate-32/#comments</comments>
		<pubDate>Sat, 04 Oct 2008 14:04:10 +0000</pubDate>
		<dc:creator>Jota</dc:creator>
				<category><![CDATA[Articulos]]></category>
		<category><![CDATA[ejemplo]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.jotadeveloper.com/?p=583</guid>
		<description><![CDATA[Quien ha trabajado con hibernate y ha tenido este problema, bueno, yo para empezar, asi que como logre hacerlo voy a publicar la solución, bien sencillo, despues de generar con Hibernate Tools los pojos, hbm.xml y los DAO, eso es sencillo, pondre dos clases de ejemplo.
En este ejemplo tendremos 3 tablas, Servicio, Variable y Servicio_Variable, [...]]]></description>
			<content:encoded><![CDATA[<p>Quien ha trabajado con hibernate y ha tenido este problema, bueno, yo para empezar, asi que como logre hacerlo voy a publicar la solución, bien sencillo, <strong>despues de generar con Hibernate Tools los pojos, hbm.xml y los DAO</strong>, eso es sencillo, pondre dos clases de ejemplo.</p>
<p>En este ejemplo tendremos<strong> 3 tablas, Servicio, Variable y Servicio_Variable,</strong> que es la tabla que relaciona muchos servicios con muchas variables.</p>
<p>Clase Servicio</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BmServicios <span style="color: #000000; font-weight: bold;">implements</span> java.<span style="color: #006633;">io</span>.<span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> idServicio<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>BmVariable<span style="color: #339933;">&gt;</span> bmVariables <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>BmVariable<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BmServicios<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BmServicios<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> idServicio,
			 <span style="color: #003399;">String</span> nombre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idServicio</span> <span style="color: #339933;">=</span> idServicio<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BmServicios<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> idServicio,Set<span style="color: #339933;">&lt;</span>BmVariable<span style="color: #339933;">&gt;</span> bmVariables,		
		 <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idServicio</span> <span style="color: #339933;">=</span> idServicio<span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getIdServicio<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idServicio</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIdServicio<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> idServicio<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idServicio</span> <span style="color: #339933;">=</span> idServicio<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getNombre<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setNombre<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nombre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Set<span style="color: #339933;">&lt;</span>BmVariable<span style="color: #339933;">&gt;</span> getBmVariables<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bmVariables</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBmVariables<span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">&lt;</span>BmVariable<span style="color: #339933;">&gt;</span> bmVariables<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bmVariables</span> <span style="color: #339933;">=</span> bmVariables<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Clase Variable</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> BmVariable <span style="color: #000000; font-weight: bold;">implements</span> java.<span style="color: #006633;">io</span>.<span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> idVariable<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> descripcion<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> tipoDeDato<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>BmServicios<span style="color: #339933;">&gt;</span> bmServicioses <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>BmServicios<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BmVariable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BmVariable<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> idVariable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idVariable</span> <span style="color: #339933;">=</span> idVariable<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> BmVariable<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> idVariable,  <span style="color: #003399;">String</span> nombre,
			<span style="color: #003399;">String</span> descripcion, <span style="color: #003399;">String</span> tipoDeDato,
			Set<span style="color: #339933;">&lt;</span>BmServicios<span style="color: #339933;">&gt;</span> bmServicioses<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idVariable</span> <span style="color: #339933;">=</span> idVariable<span style="color: #339933;">;</span>	
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descripcion</span> <span style="color: #339933;">=</span> descripcion<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tipoDeDato</span> <span style="color: #339933;">=</span> tipoDeDato<span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bmServicioses</span> <span style="color: #339933;">=</span> bmServicioses<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getIdVariable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idVariable</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIdVariable<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> idVariable<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">idVariable</span> <span style="color: #339933;">=</span> idVariable<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getNombre<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setNombre<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> nombre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">nombre</span> <span style="color: #339933;">=</span> nombre<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getDescripcion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descripcion</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setDescripcion<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> descripcion<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">descripcion</span> <span style="color: #339933;">=</span> descripcion<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTipoDeDato<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tipoDeDato</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTipoDeDato<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> tipoDeDato<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">tipoDeDato</span> <span style="color: #339933;">=</span> tipoDeDato<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Set<span style="color: #339933;">&lt;</span>BmServicios<span style="color: #339933;">&gt;</span> getBmServicioses<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bmServicioses</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setBmServicioses<span style="color: #009900;">&#40;</span>Set<span style="color: #339933;">&lt;</span>BmServicios<span style="color: #339933;">&gt;</span> bmServicioses<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">bmServicioses</span> <span style="color: #339933;">=</span> bmServicioses<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ahora, como hago para guardar unr registro en Servicio_Variable, si el Hibernate Tools no me genera un pojo para esa tabla (ojo, no la genera porque no tiene mas que los ID (PK) de las tablas Servicio y Variable.</p>
<p>Esta es la solución</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Buscamos registros de ejemplo en Servicio</span>
BmServicio servicio<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>BmServicio <span style="color: #009900;">&#41;</span> session.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>BmServicio .<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Buscamos registros de ejemplo en Variable</span>
BmVariable variable<span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>BmVariable <span style="color: #009900;">&#41;</span> session.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>BmVariable .<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Integer</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
&nbsp;
<span style="color: #666666; font-style: italic;">//Creamos una variable Set Generica en base al Pojo Servicio</span>
	Set<span style="color: #339933;">&lt;</span>BmServicio<span style="color: #339933;">&gt;</span> setServicio <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>BmServicio<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Igual para Variable</span>
		Set<span style="color: #339933;">&lt;</span>BmVariable<span style="color: #339933;">&gt;</span> setVariable <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>BmVariable<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setVariable .<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>variable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		setServicio .<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>servicio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		BmVariable .<span style="color: #006633;">setBmServicio</span><span style="color: #009900;">&#40;</span>setServicio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">//Luego hacemos un session.save(), eso es lo que hacen estas funciones respectivamente para cada tabla.</span>
                crearVariable<span style="color: #009900;">&#40;</span>variable<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		crearSerivicio<span style="color: #009900;">&#40;</span>servicio<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
&nbsp;
<span style="color: #666666; font-style: italic;">//Pueden crear un crearRegistro mas generico, para reutilizar codigo, (Gracias Comando 83)</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> crearRegistro<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> tabla<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		Transaction tx <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		Session session <span style="color: #339933;">=</span> HibernateUtil.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
			tx <span style="color: #339933;">=</span> session.<span style="color: #006633;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			session.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>tabla<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">//JOptionPane.showConfirmDialog(null, &quot;codigo: &quot;+((Sistema)tabla).getCodigo());</span>
			tx.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>HibernateException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tx <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> tx.<span style="color: #006633;">isActive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
				tx.<span style="color: #006633;">rollback</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Y listo, se guarda el registro en Variable_Sesion.</p>
 <img src="http://blog.jotadeveloper.com/wp-content/plugins/feed-statistics.php?view=1&post_id=583" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://blog.jotadeveloper.com/2008/10/04/relacion-muchos-a-muchos-en-hibernate-32/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
