如何在Spring Boot应用程序中禁用/忽略@PreAuthorize

时间:2019-09-27 13:16:40

标签: spring spring-boot spring-security authorization

我有一个提供REST API的Spring Boot应用程序。所有的API都由Spring Security保护。我还使用 @PreAuthorize 注释添加了方法授权。

对于本地开发,我想通过配置或其他方式完全禁用安全性。我想同时禁用身份验证和授权,以便我可以轻松地调用API,而不必在每次调用API时都获取新令牌。

禁用身份验证很容易,我只是将其添加到配置方法中,一切都很好。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/api/**");
}

但是,当我击中身份验证排除的端点时,这会导致 AuthenticationCredentialsNotFoundException 。仅当我删除 @PreAuthorize 批注时,该异常才会消失,显然,当我要进行一些本地开发工作时,我不想这样做。似乎仅通过在方法上添加注释,Spring AOP就会在Spring Security Context中启动并检查身份验证对象,没有办法禁用它而不是删除注释。

如何让Spring完全忽略 @PreAuthorize 注释?我尝试删除 @EnableGlobalMethodSecurity ,但是它对异常没有帮助。

0 个答案:

没有答案