新版Spring Security6.2 - Digest Authentication

前言:

书接上文,上次翻译basic的这页,这次翻译Digest Authentication这页

摘要认证-Digest Authentication

官网的警告提示:不应在应用程序中使用摘要式身份验证,因为它不被认为是安全的。最明显的问题是您必须以明文或加密或 MD5 格式存储密码。所有这些存储格式都被认为是不安全的。相反,您应该使用单向自适应密码散列(bCrypt、PBKDF2、SCrypt等)来存储凭证,摘要身份验证不支持这种方式。

摘要身份验证试图解决基本身份验证的许多弱点,特别是通过确保凭证永远不会以明文形式通过网络发送。许多浏览器都支持摘要身份验证。
管理HTTP摘要身份验证的标准由RFC 2617定义,它更新了RFC 2069规定的摘要身份验证标准的早期版本。大多数用户代理实现RFC 2617。Spring Security Digest认证支持与RFC 2617规定的认证质量保护(qop)兼容,并提供与RFC 2069的向后兼容。如果您需要使用未加密的HTTP(没有TLS或HTTPS)并希望最大限度地提高身份验证过程的安全性,那么摘要身份验证被视为更有吸引力的选择。但是,每个人都应该使用HTTPS。

摘要式身份验证的核心是“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

请确保使用NoOpPasswordEncoder配置了不安全的明文密码存储。(参见Javadoc中的NoOpPasswordEncoder类。)下面提供了一个使用Java Configuration配置摘要身份验证的示例

@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();
}

配置方面:

官网给的配置总体是按照之前总体架构,当然上面配置不完整的,甚至说有点问题,后续再补上完整例子。

参考文献

《spring boot官网》

猜你喜欢

转载自blog.csdn.net/u012895183/article/details/135005809
今日推荐