sábado, 10 de agosto de 2019

Spring Boot Web Security thymeleaf

Spring Boot Web Security thymeleaf

   referencia Securing a Web Application https://spring.io/guides/gs/securing-web/

   Thymeleaf es una librería java para elaborar aplicaciones web. Se crea un proyecto con una estructura que se contruye a mano. de momento no se encontró un template para elaborar la estructura del proyecto, así que se hace partiendo del ejemplo de la referencia. Crearemos una estructura básica en spring boot web thymeleaf sin seguridad y después le aplicaremos seguridad con spring.

Creamos un nuevo proyecto maven, para posteriormente ajustar el pom de acuderdo al prototipo de referencia. se agrega al pom parent, dependencies, properties, plugins.

sin seguridad https://github.com/jalbertomr/springBootWebSecurityThymeleaf/commit/e51721693a119a9511de976944311b0e1492d04a

pom.xml con spring security deshabilitado.
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bext</groupId>
    <artifactId>springBootWebSecurityThymeleaf</artifactId>
    <version>1</version>
    <name>springBootWebSecurityThymeleaf</name>
    <description>springBoot Web Security Thymeleaf</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- tag::security[]
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        -->
        <!-- end::security[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
         -->
    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


Estructura del proyecto


La configuración del flujo de las páginas se hace por medio de códificación java a diferencia de hacerlo en xml. en MvcConfig.java, en /resources/templates tenemos las páginas html. home y paso2.

Ejecutamos el proyecto



Ahora implementamos la seguridad. Se habilitan las dependencias de seguridad en el pom.xml. Se crea la página login.html, se modifican las otras para dar lugar al login/logout. se crea WebSecurityConfig.java

Al ejecutarlo tenemos









https://github.com/jalbertomr/springBootWebSecurityThymeleaf/commit/37e69250a842893dbe0200ac75a4a7d4fd4a494f

eot

No hay comentarios:

Publicar un comentario