...
| Bloque de código | ||
|---|---|---|
| ||
<dependencies> <!-- JWT --> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.9.1</version> </dependency> <!-- Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-jpamodelgen</artifactId> <version>5.4.12.Final</version> <scope>provided</scope> </dependency> <!-- Real DB --> <dependency> <groupId>com.oracle</groupId> <artifactId>jdbc.driver</artifactId> <version>11.2.0.3.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- UNDERTOW --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starterstarte-tomcat<logging</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging<tomcat</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- SECURITY --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- TEST --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junitspringframework.vintage<boot</groupId> <artifactId>junit<artifactId>spring-boot-vintagestarte-engine<logging</artifactId> </exclusion> <exclusion> <groupId>org.springframeworkjunit.boot<vintage</groupId> <artifactId>spring<artifactId>junit-boot-vintage-engine</artifactId> </exclusion> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Test DB --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> </dependency> <!-- Selenium --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <!-- <version>3.141.59</version> --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- Log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!-- Lombok --> <dependency> <groupId>org.apache.logging.projectlombok<log4j</groupId> <artifactId>lombok</artifactId><artifactId>log4j-layout-template-json</artifactId> <optional>true</optional><version>2.14.0</version> </dependency> </dependencies> <repositories> <repository> <id>archiva.atica.umu.es</id> <name>ATICA - UMU Repository</name> <url>https://archiva.um.es/archiva/repository/FundeWeb/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.hibernate.orm.tooling</groupId> <artifactId>hibernate-enhance-maven-plugin</artifactId> <version>${hibernate.version}</version> <executions> <execution> <configuration> <failOnError>true</failOnError> <enableLazyInitialization>true</enableLazyInitialization> <enableDirtyTracking>true</enableDirtyTracking> <enableAssociationManagement>true</enableAssociationManagement> <enableExtendedEnhancement>false</enableExtendedEnhancement> </configuration> <goals> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- Lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> <repositories> <repository> <id>archiva.atica.umu.es</id> <name>ATICA - UMU Repository</name> <url>https://archiva.um.es/archiva/repository/FundeWeb/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.hibernate.orm.tooling</groupId> <artifactId>hibernate-enhance-maven-plugin</artifactId> <version>${hibernate.version}</version> <executions> <execution> <configuration> <goal>enhance<<failOnError>true</goal>failOnError> < <enableLazyInitialization>true</goals>enableLazyInitialization> < <enableDirtyTracking>true</execution>enableDirtyTracking> <<enableAssociationManagement>true</executions>enableAssociationManagement> <<enableExtendedEnhancement>false</plugin>enableExtendedEnhancement> <!-- JaCoCo configuration --> /configuration> <plugin><goals> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId><goal>enhance</goal> <version>0.7.7.201606060606</version> <executions> </goals> <execution></execution> <goals></executions> </plugin> <goal>prepare-agent</goal> <!-- JaCoCo configuration --> <plugin> <<groupId>org.jacoco</goals>groupId> </execution><artifactId>jacoco-maven-plugin</artifactId> <execution><version>0.7.7.201606060606</version> <id>report</id><executions> <phase>prepare-package</phase><execution> <goals> <goal>report<<goal>prepare-agent</goal> </goals> </execution> </executions><execution> </plugin> <!-- Enunciate --> <plugin> <id>report</id> <groupId>com.webcohesion.enunciate</groupId><phase>prepare-package</phase> <artifactId>enunciate-maven-plugin</artifactId> <version>2.13.0</version> <dependencies> <goals> <dependency> <groupId>com.webcohesion.enunciate</groupId><goal>report</goal> <artifactId>enunciate-lombok<</artifactId>goals> <version>2.9.1</version> </execution> </dependency>executions> </dependencies>plugin> <executions> <!-- Enunciate --> <execution><plugin> <groupId>com.webcohesion.enunciate</groupId> <id>fdwjs</id><artifactId>enunciate-maven-plugin</artifactId> <version>2.13.0</version> <configuration> <dependencies> <docsDir>${basedir}/src/main/resources/</docsDir><dependency> <docsSubdir>static/docs/</docsSubdir><groupId>com.webcohesion.enunciate</groupId> <<artifactId>enunciate-lombok</configuration>artifactId> </execution> <version>2.9.1</version> </executions>dependency> </plugin>dependencies> <!-- FIN Enunciate --> </plugins> </build> |
application.properties
Como hemos visto en el apartado anterior, en el archivo application.properties incluiremos algunas variables de configuración del proyecto:
| Bloque de código |
|---|
########## RUTA BASE REST ##########
server.servlet.context-path=/api
########## BASE DE DATOS ##########
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto = validate
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@hydra-prescan.atica.um.es:1526/ZEUSDESA
spring.datasource.username=USER
spring.datasource.password=PASSWORD |
<executions>
<execution>
<id>fdwjs</id>
<configuration>
<docsDir>${basedir}/src/main/resources/</docsDir>
<docsSubdir>static/docs/</docsSubdir>
</configuration>
</execution>
</executions>
</plugin>
<!-- FIN Enunciate -->
</plugins>
</build> |
application.properties
Como hemos visto en el apartado anterior, en el archivo application.properties incluiremos algunas variables de configuración del proyecto:
| Bloque de código |
|---|
########## RUTA BASE REST ##########
server.servlet.context-path=/api
########## BASE DE DATOS ##########
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.ddl-auto = validate
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@hydra-prescan.atica.um.es:1526/ZEUSDESA
spring.datasource.username=USER
spring.datasource.password=PASSWORD |
Si queremos cambiar el puerto que utiliza nuestra aplicación, que por defecto es el 8080, podemos hacerlo con la propiedad server.port. Otras propiedades que haya que definirse para otras funcionalidades, como para el login, se incluyen en su página de documentación correspondiente. Si queremos ver las propiedades de configuración que ofrece Spring podemos consultarlas aquí.
log4j2.xml
Debemos crear el fichero log4j2.xml en la carpeta /src/main/resources con el siguiente contenido:
| Bloque de código | ||
|---|---|---|
| ||
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorinterval="60" >
<Properties>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
<Property name="LOG_DATEFORMAT_PATTERN">dd-MM-yyyy HH:mm:ss.SSS</Property>
<Property name="CONSOLE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
</Properties>
<Appenders>
<Console name="local" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" />
</Console>
<Console name="json" target="SYSTEM_OUT" follow="true">
<JsonTemplateLayout eventTemplateUri="classpath:LogstashJsonEventLayoutV1.json" locationInfoEnabled="true" stackTraceEnabled="true"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.catalina.startup.DigesterFactory" level="error" />
<Logger name="org.apache.catalina.util.LifecycleBase" level="error" />
<Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
<logger name="org.apache.sshd.common.util.SecurityUtils" level="warn"/>
<Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
<Logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="error" />
<Logger name="org.hibernate.validator.internal.util.Version" level="warn" />
<logger name="org.springframework.boot.actuate.endpoint.jmx" level="warn"/>
<Root level="debug">
<AppenderRef ref="local" />
</Root>
</Loggers>
</Configuration> |
Este es un archivo de configuración para los logs, que vienen explicados más detalladamente en esta páginaSi queremos cambiar el puerto que utiliza nuestra aplicación, que por defecto es el 8080, podemos hacerlo con la propiedad server.port. Otras propiedades que haya que definirse para otras funcionalidades, como para el login, se incluyen en su página de documentación correspondiente. Si queremos ver las propiedades de configuración que ofrece Spring podemos consultarlas aquí.
WebSecurity
Por otro lado, nos haremos una clase java para especificar la configuración de seguridad web, que debe extender a WebSecurityConfigurerAdapter e incluir la anotación @EnableWebSecurity:
...
Generar certificado autofirmado
En local podemos trabajar perfectamente sin necesidad de restringir las peticiones a que sean sólo por HTTPS, este apartado es sólo en el caso de que sí queramos restringirlo.
Para generar un certificado autofirmado, utilizaremos el comando keytool desde la terminal de Windows (:
| Bloque de código |
|---|
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 2048 -storetype PKCS12 -keystore keystore.p12 -validity 3650 -storepass password |
...