1. 概述
在本教程中,我们将学习如何在 Spring Security 中设置Authentication Provider,与使用简单UserDetailsService
的标准场景相比,它具有更多的灵活性。
2. 认证提供者
Spring Security为执行身份验证提供了多种选项。这些选项遵循一个简单的契约;AuthenticationProvider处理身份验证请求,并返回具有完整凭证的完全身份验证对象。
标准的、最常见的实现是DaoAuthenticationProvider,它从一个简单的只读用户DAO UserDetailsService检索用户详细信息。为了检索完整的用户实体,这个用户详细信息服务只能访问用户名,这对于大多数场景来说就足够了。
更多的自定义场景仍然需要访问完整的Authentication请求,以便能够执行身份验证过程。例如,当对一些外部第三方服务(如Crowd)进行身份验证时,来自身份验证请求的用户名和密码都是必需的。
对于这些更高级的场景,我们需要定义一个自定义的 Authentication Provider:
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
if (shouldAuthenticateAgainstThirdPartySystem()) {
// use the credentials
// and authenticate against the third-party system
return new UsernamePasswordAuthenticationToken(
name, password, new ArrayList<>());
} else {
return null;
}
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
注意,在返回的Authentication对象上设置的授予权限为空。当然,这是因为权限是特定于应用程序的。
3. 注册认证提供程序
现在我们已经定义了身份验证提供程序,我们需要使用可用的命名空间支持在 XML 安全配置中指定它:
<http use-expressions="true">
<intercept-url pattern="/**" access="isAuthenticated()"/>
<http-basic/>
</http>
<authentication-manager>
<authentication-provider
ref="customAuthenticationProvider" />
</authentication-manager>
4. Java配置
接下来我们看看对应的Java配置:
@Configuration
@EnableWebSecurity
@ComponentScan("com.baeldung.security")
public class SecurityConfig {
@Autowired
private CustomAuthenticationProvider authProvider;
@Bean
public AuthenticationManager authManager(HttpSecurity http) throws Exception {
AuthenticationManagerBuilder authenticationManagerBuilder =
http.getSharedObject(AuthenticationManagerBuilder.class);
authenticationManagerBuilder.authenticationProvider(authProvider);
return authenticationManagerBuilder.build();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.httpBasic();
return http.build();
}
}
5. 执行认证
无论是否在后端使用此自定义身份验证提供程序,从客户端请求身份验证基本上是相同的。
我们将使用一个简单的curl命令来发送经过身份验证的请求:
curl --header "Accept:application/json" -i --user user1:user1Pass
http://localhost:8080/spring-security-custom/api/foo/1
出于本示例的目的,我们使用基本身份验证保护 REST API。
我们从服务器返回预期的 200 OK:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=B8F0EFA81B78DE968088EBB9AFD85A60; Path=/spring-security-custom/; HttpOnly
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sun, 02 Jun 2013 17:50:40 GMT
6. 结论
在本文中,我们探讨了 Spring Security 的自定义身份验证提供程序的示例。
可以在GitHub 项目中找到本文的完整实现。