spring - Spring 5 中的 .csrf() 方法现在位于哪个包中?
问题描述
我正在尝试编写 IT。
mockMvc.perform( post( "/my_endpoint" )
.contentType( MediaType.APPLICATION_JSON )
.header("Authorization", my_credentials)
.with(csrf())
.content( jsonPayload )
)
.andExpect( status().isOk() );
我需要导入csrf()
静态方法,但是通常创建的包(org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors...)不再存在。
关于如何编写 csrf 保护以避免403
测试中的一些新想法?
谢谢。
解决方案
要使用 Spring Security 测试支持,您必须包含spring-security-test-5.1.2.RELEASE.jar作为项目的依赖项。并在下面试一试:-
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProce ssors.*;
mvc.perform(post("/").with(csrf()))
或者,
mvc.perform(post("/").with(csrf().asHeader()))
或者,
mvc.perform(post("/").with(csrf().useInvalidToken()))
推荐阅读
- python - HackerRank 频率查询错误 Python
- google-play-console - Play 主机突然开始审核内测
- excel - 需要帮助将 Excel 工作表转换为 PDF 但不断获取文档可能会打开错误
- r - geom_line 不显示线
- docker - 使用 Nginx 的子域从端点获取 JSON
- reactjs - 选中时如何只给MUI Switch边框样式?
- matlab - Matlab中的多变量积分问题
- ruby-on-rails - Heroku deplpy 失败并显示“..bin/bundle: line 11: require: command not found... Failed to install gems via Bundler.”
- git - package.json 中的 git 依赖项因“访问被拒绝(公钥)”而出错?
- ios - 如何以编程方式检查 iPhone/iPad 设备是否有原深感摄像头?