【问题标题】:How to make Spring ignores token in the endpoint for botframework如何让 Spring 忽略 botframework 端点中的令牌
【发布时间】:2021-09-17 09:35:50
【问题描述】:

我有一个与 OAuth2AuthenticationProcessingFilter 相关的问题,我的应用程序需要来自 Microsoft Teams 机器人框架 (/api/messages) 的传入消息。

看起来好像消息被过滤了,因为它包含一个无效的令牌,但是这个端点应该是公共的,因为它是在配置中设置的 (noSecurityAllowedUrlPatterns 包含端点):

http.requestMatchers()
          .and()
          .authorizeRequests()
          .antMatchers(noSecurityAllowedUrlPatterns.toArray(new String[0]))
          .permitAll()
          .anyRequest()
          .authenticated();

这是过滤器中引发异常的行:

Authentication authResult = this.authenticationManager.authenticate(authentication);

这是端点:

@PostMapping({"/messages"})
@PreAuthorize("permitAll()")
public CompletableFuture<ResponseEntity<Object>> incoming(
      @RequestBody Activity activity,
      @RequestHeader(value = "Authorization", defaultValue = "") String authHeader) {
...
}

如何成功设置配置以避免此端点中的 oauth,或者如何将传入令牌添加到有效的一个列表中(以某种方式)? 过滤器列表顺序可能有问题,但是

【问题讨论】:

标签: spring-security oauth-2.0 botframework


【解决方案1】:

IMO 您的 HttpSecurity 配置看起来不正确。如果应该公开的端点是/api/messages 并且所有其他端点应该是安全的,那么您应该按如下方式更改配置:

http.authorizeRequests()
    .antMatchers("/api/messages")
    .permitAll()
    .and()
    .authorizeRequests()
    .anyRequest()
    .authenticated();

IIRC 通过这种方式,您可以定义从上到下应用的过滤器。意味着如果请求到达您的后端并且配置的第一部分与请求匹配,则第二部分不再相关。另一方面,如果您的后端请求与第一部分不匹配,则将检查第二部分,依此类推。

本指南解释了一般行为:https://spring.io/guides/topicals/spring-security-architecture

让我知道代码是否有效(sn-p 是从内存中创建的)。 :)

【讨论】:

    猜你喜欢
    • 2019-04-26
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2021-04-21
    • 2016-10-03
    • 2020-12-24
    相关资源
    最近更新 更多