在Spring Controller中获取_csrf [英] Get _csrf in spring controller

查看:96
本文介绍了在Spring Controller中获取_csrf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在spring控制器中获取_csrf对象(?!)?我已经配置了Spring Security,可以在jsp文件中获得$ {_ csrf}请求属性.我尝试过:

How can I get _csrf object (?!) in spring controller? I've configured Spring Security and can get ${_csrf} request attribute in jsp files. I've tried:

CsrfToken _csrf = (CsrfToken) session.getAttribute("CsrfToken");
CsrfToken _csrf = (CsrfToken) session.getAttribute("_csrf");

结果为空;

提前谢谢!

推荐答案

在调试中,我看到了带有键"org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.CSRF_TOKEN"的会话属性.我查看了 HttpSessionCsrfTokenRepository 类.它具有一种从传入的HttpServletRequest对象加​​载令牌的方法.

In debug I saw a session attribute with a key "org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.CSRF_TOKEN". I viewed the HttpSessionCsrfTokenRepository class. It has a method for loading token from incoming HttpServletRequest object.

最后这对我有用:

CsrfToken token = new HttpSessionCsrfTokenRepository().loadToken(request);

如果有人向我解释它的工作原理,我将不胜感激.

I will be grateful if someone explains me how this works.

这篇关于在Spring Controller中获取_csrf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆