Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot with authentication - login page not found (404)

I have a problem with my spring application since I've tried to include some security things.

After building a small working app including angularJS I followed this spring security tutorial but I can't get it started. When I try to access any part of the app, the security modul wants to redirect to http://localhost:8080/login... but can't find it.

There was an unexpected error (type=Not Found, status=404). No message available

Maybe I'm just missing a small thing but I can't figure out what it is ^^

Here is my code...

folder structure:

src/main/java
 +-Application.java
 +-SecurityConfiguration.java

src/main/resources
 +-static
  +-index.html
 +-templates
  +-login.html

pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.BUILD-SNAPSHOT</version>
</parent>

<!-- Additional lines to be added here... -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.3-1100-jdbc4</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
    </dependency>

</dependencies>

Application.java:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }
}

SecurityConfiguration.java:

    import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().anyRequest().authenticated();
        http
                .formLogin().failureUrl("/login?error")
                .defaultSuccessUrl("/")
                .loginPage("/login")
                .permitAll()
                .and()
                .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/login")
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }
}

login.html:

    <!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>Spring Security Example</title>
</head>
<body>
    <div th:if="${param.error}">Invalid username and password.</div>
    <div th:if="${param.logout}">You have been logged out.</div>
    <form th:action="@{/login}" method="post">
        <div>
            <label> User Name : <input type="text" name="username" />
            </label>
        </div>
        <div>
            <label> Password: <input type="password" name="password" />
            </label>
        </div>
        <div>
            <input type="submit" value="Sign In" />
        </div>
    </form>
</body>
</html>
like image 353
Daniel Pinkpank Avatar asked Mar 25 '15 09:03

Daniel Pinkpank


People also ask

What is Error 404 in spring boot?

As with any web application or website, Spring MVC returns the HTTP 404 response code when the requested resource can't be found.

Why is WebSecurityConfigurerAdapter deprecated?

0-M2 we deprecated the WebSecurityConfigurerAdapter , as we encourage users to move towards a component-based security configuration. To assist with the transition to this new style of configuration, we have compiled a list of common use-cases and the suggested alternatives going forward.


2 Answers

I followed my own hint in the comment above and figured out I forgot extends WebMvcConfigurerAdapter at my Application.class

That solved the Problem!

like image 169
Daniel Pinkpank Avatar answered Sep 23 '22 00:09

Daniel Pinkpank


Where is your login.html located? Can you get to /login directly in a browser? I suspect you don't have your ViewResolver set up properly. If you can't go directly to /login, that's your problem. Look at application.properties values spring.view.prefix and spring.view.suffix.

If you can get there directly, you might be dealing with a hidden error. Spring Boot includes an AutoConfiguration that swallows exceptions and throws a 404. You might want to exclude it (change @EnableAutoConfiguration to @EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class). That will enable you to see any errors that are thrown during the request processing.

like image 28
Josh Ghiloni Avatar answered Sep 24 '22 00:09

Josh Ghiloni