2015-03-25 76 views
5

我的彈簧應用程序出現問題,因爲我嘗試了一些安全措施。Spring身份驗證 - 登錄頁面未找到(404)

建立一個包括angularJS的小工作應用程序後,我跟着這個spring security tutorial但我無法啓動它。當我嘗試訪問應用程序的任何部分時,安全模塊想要重定向到http://localhost:8080/login ...但找不到它。

出現意外錯誤(type = Not Found,status = 404)。 無

也許我只是缺少一個很小的事情,但我無法弄清楚它是什麼^^

這裏是我的代碼信息...

文件夾結構:

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> 

回答

5

我跟着我的提示在上面的註釋,想通了,我忘了extends WebMvcConfigurerAdapterApplication.class

這解決了這個問題!

0

您的login.html在哪裏?你能直接在瀏覽器中獲得/login嗎?我懷疑你沒有正確設置你的ViewResolver。如果你不能直接去/login,那就是你的問題。查看application.properties值spring.view.prefixspring.view.suffix

如果你可以直接到達那裏,你可能正在處理一個隱藏的錯誤。春季啓動包括吞嚥異常,並拋出一個404你可能希望將它排除在外(改變@EnableAutoConfiguration@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)的自動配置。這將使你看到的請求處理過程中出現的任何錯誤。

+0

我'login.html'北京時間位於'的src /主/資源/ templates',我也其複製到' src/main/resources/static'(其中所有其他.html文件都是) 但我不能去迪爾無論如何也要''登錄'。本教程 - 部分** _註冊登錄操作_ **表示我只需要在我的應用程序配置中指定此映射......就像我一樣。 唯一的是,當我想使用'@ Override'時,我得到一個Eclipse錯誤 'Application類型的方法addViewControllers(ViewControllerRegistry)必須覆蓋或實現一個超類型方法\t Application.java'。所以我只是刪除了@覆蓋 – 2015-03-25 12:33:20