2015-03-25 56 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