跳到主要内容
版本:7.0.2

用户名/密码认证

DeepSeek V3 中英对照 Username/Password Username/Password Authentication

用户身份验证最常见的方式之一是通过验证用户名和密码。Spring Security 为基于用户名和密码的身份验证提供了全面的支持。

您可以通过以下方式配置用户名和密码认证:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().authenticated()
)
.httpBasic(Customizer.withDefaults())
.formLogin(Customizer.withDefaults());

return http.build();
}

@Bean
public UserDetailsService userDetailsService() {
UserDetails userDetails = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();

return new InMemoryUserDetailsManager(userDetails);
}

}
注意

User#withDefaultPasswordEncoder 被认为在生产环境中不安全,仅适用于示例应用程序。更多详情请参阅 User#withDefaultPasswordEncoder

上述配置会自动将内存中的 UserDetailsService 注册到 SecurityFilterChain,将 DaoAuthenticationProvider 注册到默认的 AuthenticationManager,并启用表单登录HTTP Basic 认证。

要了解更多关于用户名/密码认证的信息,请参考以下用例:

发布 AuthenticationManager Bean

一个相当常见的需求是发布一个 AuthenticationManager bean,以实现自定义身份验证,例如在 @Service 或 Spring MVC @Controller 中。例如,您可能希望通过 REST API 对用户进行身份验证,而不是使用表单登录

您可以通过以下配置发布这样的 AuthenticationManager,以用于自定义身份验证场景:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/login").permitAll()
.anyRequest().authenticated()
);

return http.build();
}

@Bean
public AuthenticationManager authenticationManager(
UserDetailsService userDetailsService,
PasswordEncoder passwordEncoder) {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(userDetailsService);
authenticationProvider.setPasswordEncoder(passwordEncoder);

return new ProviderManager(authenticationProvider);
}

@Bean
public UserDetailsService userDetailsService() {
UserDetails userDetails = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();

return new InMemoryUserDetailsManager(userDetails);
}

@Bean
public PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

}

配置完成后,您可以创建一个使用 AuthenticationManager@RestController,如下所示:

@RestController
public class LoginController {

private final AuthenticationManager authenticationManager;

public LoginController(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}

@PostMapping("/login")
public ResponseEntity<Void> login(@RequestBody LoginRequest loginRequest) {
Authentication authenticationRequest =
UsernamePasswordAuthenticationToken.unauthenticated(loginRequest.username(), loginRequest.password());
Authentication authenticationResponse =
this.authenticationManager.authenticate(authenticationRequest);
// ...
}

public record LoginRequest(String username, String password) {
}

}
备注

在此示例中,如果需要,您有责任将已认证的用户保存到 SecurityContextRepository 中。例如,如果使用 HttpSession 在请求之间持久化 SecurityContext,您可以使用 HttpSessionSecurityContextRepository

自定义 AuthenticationManager

通常情况下,Spring Security 会在内部构建一个由 DaoAuthenticationProvider 组成的 AuthenticationManager,用于用户名/密码认证。在某些情况下,可能仍然需要自定义 Spring Security 所使用的 AuthenticationManager 实例。例如,您可能只需要为缓存的用户简单地禁用凭证擦除

为此,你可以利用一个事实:用于构建 Spring Security 全局 AuthenticationManagerAuthenticationManagerBuilder 会作为一个 bean 发布。你可以按如下方式配置该构建器:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// ...
return http.build();
}

@Bean
public UserDetailsService userDetailsService() {
// Return a UserDetailsService that caches users
// ...
}

@Autowired
public void configure(AuthenticationManagerBuilder builder) {
builder.eraseCredentials(false);
}

}

或者,你也可以配置一个本地的 AuthenticationManager 来覆盖全局的配置。

@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().authenticated()
)
.httpBasic(Customizer.withDefaults())
.formLogin(Customizer.withDefaults())
.authenticationManager(authenticationManager());

return http.build();
}

private AuthenticationManager authenticationManager() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider(userDetailsService());
authenticationProvider.setPasswordEncoder(passwordEncoder());

ProviderManager providerManager = new ProviderManager(authenticationProvider);
providerManager.setEraseCredentialsAfterAuthentication(false);

return providerManager;
}

private UserDetailsService userDetailsService() {
UserDetails userDetails = User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();

return new InMemoryUserDetailsManager(userDetails);
}

private PasswordEncoder passwordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

}

章节总结