【问题标题】:Spring security antMatcher not working as expectedSpring security antMatcher 未按预期工作
【发布时间】:2020-01-18 06:10:30
【问题描述】:

我有我的自定义控制器 "/my-endpoint" 和 spring 应用程序,配置如下:

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/my-endpoint", "/health")
                .permitAll()
                .antMatchers(DENY_RESOURCE_PATTERNS)
                .denyAll()
                .anyRequest()
                .authenticated()

    }

似乎对于一致的用户来说它工作正常。但是,如果我已经授权(使用 oauth2)并且我的会话(或令牌)已过期 -> spring 试图将我重定向到登录页面。

我不想要这个,我想允许任何用户连接到“/my-endpoint”端点。

我忘了配置什么?

有趣的是,内置端点 "/health" 按预期工作,即使会话已过期。

【问题讨论】:

  • my-endpoint 是什么?
  • @chaoluo,我的自定义控制器,它只是返回静态 json 对象
  • 添加您要调用的确切 URL。
  • @DarrenForsythe, localhost:3030/my-endpoint

标签: spring spring-boot spring-security


【解决方案1】:

您可以使用configure(WebSecurity web)。它将绕过 Spring Security Filters 并允许任何用户访问端点。见HttpSecurity vs WebSecurity

@Override
    public void configure(WebSecurity web) throws Exception {
        web
          .ignoring()
            .antMatchers(HttpMethod.yourMethod, "/health")
            .antMatchers(HttpMethod.yourMethod, "/my-endpoint");
    }

【讨论】:

    猜你喜欢
    • 2012-05-15
    • 2018-09-23
    • 2018-06-26
    • 2015-04-01
    • 2015-10-02
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多