我在我的Spring MVC应用程序中使用Spring Security 3.2.3并获得一些意外行为.
根据这里的文档,应该可以${_csrf.token}
在我的html的meta标签中使用:
从中我使用JQuery提取"内容"的值,并使用AJAX将其放入Request Header.
出于某种原因,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:",如下所示:
但它导致一个例外,无法评估:
评估SpringEL表达式的异常:"_csrf.token"
我认为这里的关键可能是弄清楚如何在我看来正确评估表达式.