spring-boot - 未经身份验证的调用返回 200 状态
问题描述
我有一个弹簧启动应用程序。我添加了一个安全层,如下所示:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${allowed.paths}")
private List<String> allowedPaths;
@Autowired
private TestCenterAuthProvider authProvider;
@Override
public void configure(AuthenticationManagerBuilder auth){
auth.authenticationProvider(authProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.cors().disable()
.authorizeRequests()
.antMatchers(allowedPaths.toArray(new String[0]))
.permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.permitAll();
}
}
这默认为POST localhost:8080/login
端点提供了默认的 HTML 表示。
现在,我所做的任何未经身份验证的调用都会返回 200 OK,并带有登录页面的 HTML 响应。我需要这个来返回 403。我无法弄清楚这一点。
解决方案
插入
.exceptionHandling()
.defaultAuthenticationEntryPointFor(
new Http403ForbiddenEntryPoint(),
new RequestHeaderRequestMatcher("X-Requested-With", "XMLHttpRequest"))
.and()
.authorizeRequests()
启用 403
推荐阅读
- dom - 如何使用 XPATH 访问 iframe 的属性/属性?
- java - 将 LocalDateTime 持久化到 DB2 TIMESTAMP 列中
- php - 为什么当我创建一个页面时,它会显示在 wordpress 的导航菜单中?
- r - 对 xts 对象列表执行计算并存储结果
- ruby-on-rails - 如何解决此 Postgresql Invalid Paramater 错误?
- angular - 谷歌自动完成建议在角度不合适
- laravel - 将图像传递给引导模式
- openapi - 禁用跨控制器名称冲突的枚举行为
- sorting - 内部单个查询的弹性搜索排序结果应该 bool 查询
- java - Java java.lang.ClassCastException:javax.xml.bind.JAXBElement 在使用 IntelliJ 生成的代码时无法强制转换异常