Я следую учебник по яровой безопасности, и я следующий до сих пор: я получаю следующее сообщение об ошибке при попытке войти с моей страницы входа:CSRF токен не найден
HTTP Status 403 - ожидаемый CSRF маркер не найденный. Срок действия вашей сессии истек?
Решение, которое я нашел в stackoverflow, указано, чтобы добавить скрытый атрибут в форму, и я сделал это. это атрибут:
<input type="hidden" name="${_csrf.parameterName}" value="{_csrf.token}"/>
Я перепроверил код несколько раз, и я получаю ту же ошибку 403, даже если мой код, кажется, точно соответствовать учебник Udemy.
Заранее спасибо.
Моя страница login.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>Please login</title>
</head>
<body>
<c:url value="/login" var="loginProcessingUrl"/>
<form action="${loginProcessingUrl}" method="post">
<fieldset>
<legend>Please Login</legend>
<!-- use param.error assuming FormLoginConfigurer#failureUrl contains the query parameter error -->
<c:if test="${param.error != null}">
<div>
Failed to login.
<c:if test="${SPRING_SECURITY_LAST_EXCEPTION != null}">
Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
</c:if>
</div>
</c:if>
<!-- the configured LogoutConfigurer#logoutSuccessUrl is /login?logout and contains the query param logout -->
<c:if test="${param.logout != null}">
<div>
You have been logged out.
</div>
</c:if>
<p>
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
</p>
<p>
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
</p>
<!-- if using RememberMeConfigurer make sure remember-me matches RememberMeConfigurer#rememberMeParameter -->
<p>
<label for="remember-me">Remember Me?</label>
<input type="checkbox" id="remember-me" name="remember-me"/>
</p>
<input type="hidden" name="${_csrf.parameterName}" value="{_csrf.token}"/>
<div>
<button type="submit" class="btn">Log in</button>
</div>
</fieldset>
</form>
</body>
</html>
Мой index.jsp с формой выхода из системы:
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<c:url value="/showMessage.html" var="messageUrl" />
<a href="${messageUrl}">Click to enter</a>
<form action="logout" method="post">
<input type="submit" value="logout"/>
<input type="hidden" name="${_csrf.parameterName}" value="{_csrf.token}"/>
</form>
</body>
</html>
Моя конфигурация безопасности файла:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void congigureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
public void configure(HttpSecurity http) throws Exception{
http.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.and()
.httpBasic();
}
}
Пустой WebApplicationInitializer:
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
}
Контроллер Логин:
@Controller
public class LoginController {
@RequestMapping(value="/login", method=RequestMethod.GET)
public String loginPage(ModelMap map){
//prefix: /WEB-INF/view
//postfix .jsp
//prefix + view + postfix
// /WEB-INF/view/login.jsp
return "login";
}
Это код в моем приложении-config.xml
<context:component-scan
base-package="com.springsecurityexample"/>
Это код в моем файле MVC-config.xml:
<context:component-scan
base-package="com.springsecurityexample.web"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
и мой web.xml файл:
<display-name>SpringSecurityTut1</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Насколько я знаю о конфигурации Java, вам не нужно добавлять параметр токена CSRF в свой общедоступный метод настройки void. –
На видеоролике репетитор этого не сделал. Как бы ты это сделал. Я готов попробовать. – abujafar
Ознакомьтесь с документацией Spring-security, а не жалуйтесь, что у преподавателя ее нет. Также правильный ответ, приведенный ниже wpj. –