java 带有身份验证的 Spring Boot - 未找到登录页面 (404)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29251980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot with authentication - login page not found (404)
提问by Daniel Pinkpank
I have a problem with my spring application since I've tried to include some security things.
我的 spring 应用程序有问题,因为我试图包含一些安全内容。
After building a small working app including angularJS I followed this spring security tutorialbut 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.
在构建了一个包括 angularJS 的小型工作应用程序后,我遵循了这个spring 安全教程,但我无法开始。当我尝试访问应用程序的任何部分时,安全模块想要重定向到http://localhost:8080/login...但找不到它。
There was an unexpected error (type=Not Found, status=404). No message available
出现意外错误(类型=未找到,状态=404)。没有可用的消息
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:
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:
应用程序.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:
安全配置.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:
登录.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>
采纳答案by Daniel Pinkpank
I followed my own hint in the comment above and figured out I forgot extends WebMvcConfigurerAdapter
at my Application.class
我在上面的评论中遵循了我自己的提示,发现我忘记extends WebMvcConfigurerAdapter
了我的Application.class
That solved the Problem!
这解决了问题!
回答by Josh Ghiloni
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
.
你在哪里login.html
?可以/login
直接在浏览器中访问吗?我怀疑你没有ViewResolver
正确设置。如果你不能直接去/login
,那是你的问题。查看 application.properties 值spring.view.prefix
和spring.view.suffix
.
If you canget 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.
如果您可以直接到达那里,那么您可能正在处理一个隐藏的错误。Spring Boot 包含一个 AutoConfiguration,它会吞下异常并抛出 404。您可能想要排除它(更改@EnableAutoConfiguration
为@EnableAutoConfiguration(exclude=ErrorMvcAutoConfiguration.class)
。这将使您能够看到在请求处理期间抛出的任何错误。