对于最新的稳定版本,请使用 Spring Security 6.5.3spring-doc.cadn.net.cn

摘要认证

本节详细介绍了 Spring Security 如何为 Digest Authentication 提供支持,该DigestAuthenticationFilter.spring-doc.cadn.net.cn

不应在新式应用程序中使用摘要式身份验证,因为它不被认为是安全的。 最明显的问题是您必须以纯文本或加密或 MD5 格式存储密码。 所有这些存储格式都被认为是不安全的。 相反,您应该使用单向自适应密码哈希(bCrypt、PBKDF2、SCrypt 等)来存储凭据,摘要式身份验证不支持这种哈希。spring-doc.cadn.net.cn

摘要身份验证试图解决基本身份验证的许多弱点,特别是通过确保凭据永远不会以明文形式通过网络发送。 许多浏览器都支持摘要式身份验证spring-doc.cadn.net.cn

管理 HTTP 摘要身份验证的标准由 RFC 2617 定义,该标准更新了 RFC 2069 规定的摘要身份验证标准的早期版本。 大多数用户代理都实现 RFC 2617。 Spring Security 的 Digest Authentication 支持与“auth”保护质量兼容(qop)的 RFC 2617 规定,该协议还提供了与 RFC 2069 的向后兼容性。 如果您需要使用未加密的 HTTP(无 TLS 或 HTTPS)并希望最大限度地提高身份验证过程的安全性,摘要身份验证被视为更具吸引力的选择。 但是,每个人都应该使用 HTTPSspring-doc.cadn.net.cn

摘要身份验证的核心是一个“随机数”。 这是服务器生成的值。 Spring Security 的随机数采用以下格式:spring-doc.cadn.net.cn

摘要语法
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

您需要确保使用 NoOpPasswordEncoder. (请参阅NoOpPasswordEncoder类。 下面提供了使用 Java 配置配置摘要式身份验证的示例:spring-doc.cadn.net.cn

摘要认证
@Autowired
UserDetailsService userDetailsService;

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

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

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
	http
		// ...
		.exceptionHandling(e -> e.authenticationEntryPoint(authenticationEntryPoint()))
		.addFilterBefore(digestFilter());
	return http.build();
}
<b:bean id="digestFilter"
        class="org.springframework.security.web.authentication.www.DigestAuthenticationFilter"
    p:userDetailsService-ref="jdbcDaoImpl"
    p:authenticationEntryPoint-ref="digestEntryPoint"
/>

<b:bean id="digestEntryPoint"
        class="org.springframework.security.web.authentication.www.DigestAuthenticationEntryPoint"
    p:realmName="My App Realm"
	p:key="3028472b-da34-4501-bfd8-a355c42bdf92"
/>

<http>
	<!-- ... -->
	<custom-filter ref="userFilter" position="DIGEST_AUTH_FILTER"/>
</http>