[英]InMemoryAuthentication cannot verify the provided CSRF token and return Whitelabel error page
在Spring Boot securityproject中,当尝试从登录名(键入用户名和密码后)重定向到所选页面时,出现白色标签错误页面。 详细信息是从jsp文件的视图开始的。 start.jsp
只有一个用途,即重定向到test.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Startpage</title>
</head>
<body>
<p>Click <a href="/test">here</a> Start.</p>
</body>
</html>
start.jsp
的安全性设置为permitAll,而test.jsp
的设置已通过身份验证,因此在调用test.jsp
login.jsp
之前将其键入用户名和密码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Login</title>
</head>
<body>
<form method="POST" action="/login">
User Name : <input type="text" name="username"/>
Password: <input type="password" name="password"/>
<button type="submit">Submit</button>
</form>
</body>
</html>
最终的test.jsp
看起来像这样
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<title>Insert title here</title>
</head>
<body>
Hi
</body>
</html>
错误消息是
Whitelabel Error Page此应用程序没有针对/ error的显式映射,因此您将其视为后备。 2018年3月1日星期四21:43:40 CET发生意外错误(类型为禁止,状态为403)。 由于找不到您的会话,因此无法验证提供的CSRF令牌。
除了在pom.xml-file中的Spring Boot Securitydependency之外,没有选择任何ssl或任何其他安全设置,它作为http在我的本地主机上运行。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Mvc由
package com.proj.db_proj;
import org.springframework.context.annotation.Configuration;
import
org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.addViewController("/startpage").setViewName("startpage");
registry.addViewController("/test").setViewName("test");
registry.addViewController("/").setViewName("start");
}
}
以及带有身份验证和配置的Websecurity
package com.proj.db_proj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.
builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.
web.builders.HttpSecurity;
import org.springframework.security.config.annotation.
web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.¨
web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/start").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.permitAll();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("user").roles("USER");
}
}
任何人都可以看到任何错误,或者知道为什么会出现Whitelabel错误页面? 我遵循了手册并检查了教程,并在此处对stackoverflow提出了问题,但没有任何答案。
在错误消息中,您没有附加csrf令牌。
Could not verify the provided CSRF token because your session was not found.
在春季启动安全性下,默认情况下启用csrf。
如果要禁用csrf,请将此代码附加到HttpSecurity配置中。 (不要错过.and())
.csrf().disable();
您应该使用Thymeleaf命名空间实际使用CSRF(默认情况下已启用)。
更改:
<form method="POST" action="/login">
至:
<form method="POST" th:action="@{/login}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.