I'm following this Spring Security guide, and I have already reached the section called Create an unsecured web application. At the end of that section, this is stated:
At this point, you could jump ahead to Make the application executable and run the application without having to login to anything.
With the base simple web application created, you can add security to it.
I tried to follow the steps described in Make the application executable, to be able to create an unsecured version of the application. However, the views aren't processed properly.
For example, if I navigate to http://localhost:8080/home
I get this error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jan 14 20:49:56 ART 2016
There was an unexpected error (type=Not Found, status=404).
No message available
I found this question saying I should add Thymeleaf's dependency, and this other one saying I should add jasper and jslt, but both of them didn't work. Then, I found this issue, saying that I should copy my resources from src/main/resources/templates
to src/main/resources/static
.
Doing so does a little change: navigating to http://localhost:8080/home.html
renders the html, but the view is not proccessed, so the link is not generated.
This is my pom.xml
:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.juanmougan.samples</groupId>
<artifactId>spring-security</artifactId>
<!-- <packaging>jar</packaging> -->
<version>1.0-SNAPSHOT</version>
<name>spring-security</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</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>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
My MVC config class:
package com.github.juanmougan.samples;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* Configures Spring MVC and sets up view controllers to expose the templates.
*
* @author juanma
*
*/
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/home").setViewName("home");
registry.addViewController("/").setViewName("home");
registry.addViewController("/hello").setViewName("hello");
registry.addViewController("/login").setViewName("login");
}
}
And the template:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
Any ideas why the templates aren't being processed properly?
Thanks in advance
EDIT: Added the missing Application
class.
package com.github.juanmougan.samples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}