我在 Spring MVC 应用程序中使用 Spring Security 3.2.3 并得到一些意外的行为。
根据文档在这里,应该可以使用${_csrf.token}
在我的 html 的元标记中:
<meta name="_csrf" content="${_csrf.token}" />
<!-- default header name is X-CSRF-TOKEN -->
<meta name="_csrf_header" content="${_csrf.headerName}" />
我使用 JQuery 从中提取“内容”的值,并使用 AJAX 将其放入请求标头中。
但出于某种原因,Spring Security 不会将其“转换”为实际令牌,它只是作为文字字符串“${_csrf.token}”发送到标头中。
尝试使用替代路线${_csrf.token}
在根据文档的隐藏输入中,然后我尝试通过检查输入的值来检查令牌的计算结果,但它仍然只是纯文本“${_csrf.token}”。
由于 Spring Security 似乎未生效,我是否缺少某种配置?我目前正在使用准系统 Spring Security Java 配置(而不是 xml),如下所示:
import org.springframework.context.annotation.*;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.*;
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf();
}
}
我知道配置被调用,因为我在其中放入了调试语句,所以我假设 CSRF 保护确实已启用,因为它应该是默认的。
我意识到语法“${}”是JSP表达式语言,我目前成功地使用它通过Thymeleaf将上下文评估为对象,例如:
th:object="${context}"
所以我尝试在元标记的“内容”前面添加“th:”,如下所示:
<meta name="_csrf" th:content="${_csrf.token}"/>
但这会导致无法评估的异常:
计算 SpringEL 表达式时出现异常:“_csrf.token”
我认为这里的关键可能是弄清楚如何让表达式在我看来正确评估。