2015-03-25 10 views
5

Ich habe ein Problem mit meiner Feder-Anwendung, da ich versucht habe, einige Sicherheits-Dinge zu integrieren.Spring Boot mit Authentifizierung - Login-Seite nicht gefunden (404)

Nachdem ich eine kleine App mit angularJS erstellt habe, folgte ich diesem spring security tutorial, aber ich kann es nicht starten. Wenn ich versuche, auf einen Teil der App zuzugreifen, möchte das Sicherheitsmodul auf http://localhost:8080/login umleiten ... kann es aber nicht finden.

Es ist ein unerwarteter Fehler aufgetreten (Typ = Nicht gefunden, Status = 404). keine Nachricht vorhanden

Vielleicht ... Ich vermisse nur eine kleine Sache, aber ich kann nicht herausfinden, was es ist ^^

Hier ist mein Code

Ordnerstruktur:

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> 

Antwort

5

Ich folgte meinem eigenen Hinweis in dem Kommentar oben und herausgefunden habe ich vergessen extends WebMvcConfigurerAdapter an meinem Application.class

, dass das Problem gelöst!

0

Wo befindet sich Ihr login.html? Können Sie direkt in einem Browser zu /login gelangen? Ich vermute, du hast deine ViewResolver nicht richtig eingerichtet. Wenn Sie nicht direkt zu /login gehen können, ist das Ihr Problem. Sehen Sie sich die Werte für application.properties spring.view.prefix und spring.view.suffix an.

Wenn Sie können direkt dort ankommen, haben Sie möglicherweise mit einem versteckten Fehler zu tun. Spring Boot enthält eine AutoConfiguration, die Ausnahmen verschluckt und einen 404 auslöst. Sie möchten es vielleicht ausschließen (ändern Sie @EnableAutoConfiguration zu @EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class). Das wird Ihnen ermöglichen, alle Fehler zu sehen, die während der Anfrageverarbeitung ausgelöst werden.)

+0

Mein 'login.html' ist befindet sich in' src/main/resources/templates' und ich es auch 'kopiert src/main/resources/static' (wo alle anderen .html-Dateien sind) Aber ich kann nicht dir gehen genau zu '/ login' entweder. Das Tutorial - Teil ** _ Registrierung der Login-Aktion _ ** sagt, ich muss nur diese Zuordnung in meiner Anwendungskonfiguration angeben ... wie ich es getan habe. Die einzige Sache ist, wenn ich '@ Override' verwenden möchte, bekomme ich einen Eclipse-Fehler 'Die Methode addViewControllers (ViewControllerRegistry) vom Typ Application muss überschreiben oder implementieren eine übergeordnete Methode \t Application.java'. Also habe ich einfach den '@ Override' entfernt –

Verwandte Themen