spring-boot - 基本身份验证在邮递员中返回 403,但在浏览器中工作正常
问题描述
我已将 securityConfig 定义如下:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication()
.dataSource(dataSource)
.passwordEncoder(passwordEncoder())
.usersByUsernameQuery(\\some code\\)
.authoritiesByUsernameQuery(\\some code\\)
.getUserDetailsService();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic().authenticationEntryPoint(entryPoint);
}
当我在浏览器的表单中输入凭据时,它可以正常工作,
我收到 403 错误。我究竟做错了什么?
解决方案
这很可能是由CSRF 保护引起的。
从文档:
我们的建议是对普通用户可以通过浏览器处理的任何请求使用 CSRF 保护。如果您只创建非浏览器客户端使用的服务,您可能希望禁用 CSRF 保护。
CSRF 保护可以按如下方式禁用:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic().authenticationEntryPoint(entryPoint)
.and()
.csrf().csrf().disable()
}
推荐阅读
- ecdsa - 使用 Google Cloud Key Management Service 签署以太坊交易
- azure - Azure Powershell DevOps Task 与 Azure 中国对话时出现问题
- php - 意外数据发现日期格式?
- r - 如何在 geom_raster 中设置一个常量“ylim”?
- flutter - 如何使用提供者更新具有多个实例的类?
- version-control - 微服务和版本控制如何处理部署
- google-cloud-platform - 删除克隆新服务器的快照是否会导致服务器崩溃?
- java - 如何使用 java WebDriver 在 Firefox 中打开“about:preferences”
- swift - 如何在 CognitoAuth - Swift 中制作 globalSignOut?
- microsoft-graph-api - 同步特定邮件文件夹中的消息时持续出现“503 UnknownError”(通过增量)