【问题标题】:@PreAuthorize(permitAll) still requires authentication@PreAuthorize(permitAll) 仍然需要身份验证
【发布时间】:2015-11-05 10:05:37
【问题描述】:

我的Repository 中有以下示例方法(带有@RepositoryRestResource 注释):

@Override
@PreAuthorize("permitAll")
@PostAuthorize("permitAll")
public Iterable<User> findAll();

但是当我将这些 permitAll 注释添加到整个存储库界面时,我仍然收到 401 Unauthorized,事件。

这是我的WebSecurityConfigurerAdapter

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().fullyAuthenticated().and().httpBasic().and().csrf().disable();
    }
}

我想这优先于那些方法注释,但我不知道如何解决这个问题。

【问题讨论】:

  • 也许你错过了permitAll 中的括号。试试permitAll()
  • 你真的需要这些注释吗?删除它们后会发生什么?

标签: spring spring-security


【解决方案1】:

在网络安全过滤器之后应用方法安全性。

由于您的配置中有anyRequest().fullyAuthenticated(),因此您的findAll 方法将永远不会被命中。 anyRequest().fullyAuthenticated() 表示所有尝试访问没有完全用户身份验证的 Web 端点的所有尝试都将失败。

来自 JavaDoc

指定已通过身份验证的用户允许 URL 没有“记住”。

你需要在你的网络安全中添加一个额外的路径,比如。

protected void configure(final HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .antMatchers(HttpMethod.GET, '/somePath').permitAll()
         .and()
            .httpBasic()
         .and()
            .csrf().disable();
}

【讨论】:

  • 真的只有这样吗?我的意思是在两个地方保持路径是混乱的。有没有办法注释不安全的端点?
  • 问题在于它是两个完全独立的安全系统,在不同的时间运行。
  • 现在还有办法两者兼得吗?这基本上意味着我们可以完全忘记@PreAuthorize()等。
  • 这个答案已经很老了,也许过去 3 年事情发生了变化?你可以两者兼得,但你需要了解什么在哪个阶段运行
猜你喜欢
  • 1970-01-01
  • 2021-06-03
  • 2019-08-06
  • 2022-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2022-01-08
相关资源
最近更新 更多