0

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);
    }

}
Community
  • 1
  • 1
jmm
  • 1,044
  • 2
  • 12
  • 38
  • To clarify the question is that your home or "/" pages are not being rendered properly and not the fact you are getting "Whitelabel Error Page"? – Aeseir Jan 15 '16 at 00:27
  • Neither the home (http://localhost:8080/) nor other pages, like http://localhost:8080/hello – jmm Jan 15 '16 at 00:33
  • How have you set the application context configuration? – Aeseir Jan 15 '16 at 00:51
  • To be honest, I'm not sure, I guess using the `@SpringBootApplication` annotation. I'm adding my Application class to the post, it's the only file I didn't list yet – jmm Jan 15 '16 at 01:11

2 Answers2

2

You should add @Configuration annotation, so your MvcConfig class will be taken into account while loading configuration

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter{
...
}
wcislo
  • 1,338
  • 1
  • 9
  • 14
  • While this is true, the OP should be using the `spring-boot-starter-web` if they want MVC stuff. Let spring boot do its autoconfiguration magic like it's supposed to and manually configure stuff only where necessary. – james_s_tayler Jan 15 '16 at 01:53
  • 2
    This is not the case in this issue. I would also recommend using `spring-boot-started-web`, but the root of the problem OP has is that his mappings are not loaded by Spring. Your answer is also good, but it's not the approach OP suggested in his post – wcislo Jan 15 '16 at 01:57
  • Thank you both. Besides this annotation-missing problem (a bad copy-paste), I made a terribly stupid mistake: somehow, I put `MvcConfig` in the test folder, instead of main – jmm Jan 15 '16 at 01:57
2

You don't need the tomcat or javax.servlet in your pom.xml instead take them out and replace them with

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Which will bring in the embedded tomcat instance and autoconfigure the MVC stuff for you.

Then add an @Controller annotated class and set up a @RequestMapping

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() { 
        return "redirect:/home";
    }

    @RequestMapping("/home")
    public String home() {  
        return "home";
    }

    @RequestMapping("/login")
    public String login() { 
        return "login";
    }

}

Also, as a #protip - when you get a whitelabel error like that, go to eclipse (or STS, whatever you're using) and look at the output on the console. It will usually dump a full stack trace which has much more explicit error messages and you can pinpoint where the problem is.

james_s_tayler
  • 1,823
  • 1
  • 15
  • 20
  • Also, it's just an opinion but freemarker is much much nicer than thymeleaf. I tried thymeleaf because it was part of some tutorial too and It has a nasty syntax to include other templates and you waste time writing dummy html so you could render it in a browser during dev and see what it's going to look like. Really, spring boot applications are fast to deploy (especially with the [DevTools](https://spring.io/blog/2015/06/17/devtools-in-spring-boot-1-3) that this is a pointless feature! Also it makes your template code harder to read because there is random irrelevant stuff everywhere. – james_s_tayler Jan 15 '16 at 01:45
  • Thanks for both your answer and comment. I'll check freemarker, and I don't think Spring Boot is so great anyway, but I just wanted to have a glimpse of Spring Security, and I decided to follow this guide step by step, to have the minimum amount of configuration problems – jmm Jan 15 '16 at 01:55
  • I just read the extra tip. I decided to post a question because either running the application from Eclipse or from the console wouldn't render any error in the logs – jmm Jan 15 '16 at 02:06
  • 1
    Spring boot takes a little bit of getting used to, it pays to learn what auto-configuration is happening and what things are being set to. But once you get used to it and you are familiar with that stuff it cuts your ramp up time on any new spring project dramatically. And I mean d-r-a-m-a-t-i-c-a-l-l-y. – james_s_tayler Jan 15 '16 at 02:12