跳到主要内容

摘要认证

QWen Max 中英对照 Digest Digest Authentication

本节详细介绍了Spring Security如何通过DigestAuthenticationFilter提供对Digest Authentication的支持。

注意

你不应在现代应用程序中使用Digest认证,因为它不被认为是安全的。最明显的问题是,你必须以明文、加密或MD5格式存储你的密码。所有这些存储格式都被认为是不安全的。相反,你应该使用单向自适应密码哈希(如bCrypt、PBKDF2、SCrypt等)来存储凭证,而Digest认证并不支持这种做法。

Digest Authentication 试图解决 Basic authentication 的许多弱点,特别是通过确保凭证永远不会以明文形式在网络上传输。许多 浏览器支持 Digest Authentication

HTTP Digest Authentication 的标准由 RFC 2617 定义,该文件更新了之前由 RFC 2069 规定的 Digest Authentication 标准的早期版本。大多数用户代理实现了 RFC 2617。Spring Security 的 Digest Authentication 支持与 RFC 2617 规定的“auth”质量保护(qop)兼容,这也提供了向后兼容 RFC 2069 的功能。如果你需要使用未加密的 HTTP(无 TLS 或 HTTPS),并且希望最大化认证过程的安全性,那么 Digest Authentication 曾被视为一个更具吸引力的选择。然而,每个人都应该使用 HTTPS

Digest Authentication的核心是一个“nonce”。这是服务器生成的一个值。Spring Security的nonce采用以下格式:

base64(expirationTime + ":" + md5Hex(expirationTime + ":" + key))
expirationTime: The date and time when the nonce expires, expressed in milliseconds
key: A private key to prevent modification of the nonce token
txt

你需要确保使用 NoOpPasswordEncoder 配置不安全的纯文本密码存储。(请参阅 Javadoc 中的 NoOpPasswordEncoder 类。)以下是一个使用 Java 配置来配置摘要认证的示例:

@Autowired
UserDetailsService userDetailsService;

DigestAuthenticationEntryPoint authenticationEntryPoint() {
DigestAuthenticationEntryPoint result = new DigestAuthenticationEntryPoint();
result.setRealmName("My App Realm");
result.setKey("3028472b-da34-4501-bfd8-a355c42bdf92");
return result;
}

DigestAuthenticationFilter digestAuthenticationFilter() {
DigestAuthenticationFilter result = new DigestAuthenticationFilter();
result.setUserDetailsService(userDetailsService);
result.setAuthenticationEntryPoint(authenticationEntryPoint());
return result;
}

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
// ...
.exceptionHandling(e -> e.authenticationEntryPoint(authenticationEntryPoint()))
.addFilter(digestAuthenticationFilter());
return http.build();
}
java