Spring 无状态 API 的基于 http 的摘要安全基本身份验证

Spring Security Basic Auth with digest over http for stateless APIs

有些 clients/customers 会使用网络应用程序通过用户管理系统进行离线大规模使用。在这方面,他们为部署支持 HTTPS 的应用程序提供了真正的痛苦。更不用说浏览器对自签名证书的 UN-trust 警告,这让客户抱怨。

我一直在尝试使用 Spring 安全性和 Spring 引导来使用基本身份验证来保护无状态 API,但我想控制 base64 header encryption/decryption 避免以非常容易解密的 base64 字符串发送凭据。

@Override
    protected void configure(HttpSecurity http) throws Exception {
      http.csrf()
          .disable()
          .cors()
          .and()
          .authorizeRequests()
          .antMatchers("/login")
          .permitAll()
          .and()
          .httpBasic();
    }

此方法配置扩展 WebSecurityConfigurerAdapter。我试图查看 BasicAuthenticationFilter 的源代码,发现它使用 BasicAuthenticationConverter 创建新的 object 所以我无法提供自定义转换器作为 bean 来控制 base64 解密有更强大的替代品(或额外的替代品)。

此外,这也违反了基本身份验证标准。 Digest Auth 将密码存储为文本,这对我来说不是一个选项。

所以,

除了回答您的问题,别无选择,只能使用 HTTP,是的,您可以自己控制发送加密数据,并创建一个 servlet 过滤器,用于对每个请求或敏感请求发送的数据进行解密。

Is there anyway to use Basic Auth with HTTP controlling the base64 decryption trying to reach a bit to what HTTPS offers ?

是的。这里以Basic Authentication为例进行扩展。您需要根据您的请求创建过滤器,以便使用 header 中的 base64 编码做任何您想做的事情。您可以在编码为 base64.

之前加密包含 username:password 值的授权 header

username:password >> 加密 >> 编码为 base64

然后在那个过滤器中,你进行解密并重新创建 base64 纯形式

请求header授权加密>>从base64解码>>解密>>编码username:password到base64>>继续过滤链到BasicAuthenticationFilter

您可以像这样在 BasicAuthenticationFilter 之前添加过滤器

http.addFilterBefore(tokenFilter(), BasicAuthenticationFilter.class);

请注意,这是扩展基本身份验证的自定义解决方法,但它不是处理基本身份验证的标准方式。

Or using Digest Auth with encrypted stored passwords ?

Digest Auth 不是通过 HTTP 保护的,但为了更好地了解它的工作原理,您可以查看此答案

Concept of Digest authentication - does it really work?

使用第一个选项比第二个选项更安全。但最好的解决方案是使用 HTTP。安全是主要关注点和主题,您应该花一些时间来找出最适合您的情况。