前言
前置阅读
spring boot security快速使用示例
spring boot security之前后端分离配置
说明
实际场景,我们一般是把用户信息保存在db中(也可能是调用三方接口),需要自定义用户信息加载或认证部分的逻辑,下面提供一个示例。
代码示例
定义用户bean
@AllArgsConstructor
@Data
public class User {
private String username;
private String password;
}
定义Mapper
示例,代码写死了,并不是实际从数据库或某个存储查询用户信息:
@Component
public class UserMapper {
public User select(String username) {
return new User(username, "pass");
}
}
定义加载用户数据的类
UserDetailsService 是spring security内置的加载用户信息的接口,我们只需要实现这个接口:
@Slf4j
@Component
public class UserDetailsServiceImpl implements UserDetailsService {
public static final UserDetails INVALID_USER =
new org.springframework.security.core.userdetails.User("invalid_user", "invalid_password", Collections.emptyList());
private final UserMapper userMapper;
public UserDetailsServiceImpl(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userMapper.select(username);
if (user == null) {
return INVALID_USER;
}
return new org.springframework.security.core.userdetails.User(username, user.getPassword(), Collections.emptyList());
}
}
自定义认证的bean配置
@Configuration
public class WebConfiguration {
@Bean
public PasswordEncoder passwordEncoder() {
return NoOpPasswordEncoder.getInstance();
}
@Bean
public AuthenticationManager authenticationManager(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(passwordEncoder);
return new ProviderManager(Arrays.asList(provider));
}
}
注意,因为这个是示例,AuthenticationProvider使用的是spring security的DaoAuthenticationProvider ,在实际场景中,如果不满足可以自定义实现或者继承DaoAuthenticationProvider ,重写其中的:additionalAuthenticationChecks方法,主要就是认证检查的,默认实现如下:
@Override
@SuppressWarnings("deprecation")
protected void additionalAuthenticationChecks(UserDetails userDetails,
UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
if (authentication.getCredentials() == null) {
this.logger.debug("Failed to authenticate since no credentials provided");
throw new BadCredentialsException(this.messages
.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
}
String presentedPassword = authentication.getCredentials().toString();
if (!this.passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
this.logger.debug("Failed to authenticate since password does not match stored value");
throw new BadCredentialsException(this.messages
.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
}
}
定义登录接口
@RequestMapping("/login")
@RestController
public class LoginController {
private final AuthenticationManager authenticationManager;
public LoginController(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@PostMapping()
public Object login(@RequestBody User user) {
try {
Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authenticate);
return "login success";
}catch (Exception e) {
return "login failed";
}
}
@GetMapping("/captcha")
public Object captcha() {
return "1234";
}
}
自定义HttpSecurity
@Component
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/login/**").permitAll()
.anyRequest()
.authenticated()
.and()
.exceptionHandling()
.authenticationEntryPoint((request, response, authException) ->
response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase()))
.and().cors()
.and().csrf().disable()
.httpBasic();
}
}
测试
示例中,用户密码写死是:pass,
用一个错误的密码试一下,响应登录失败:

使用正确的密码,响应登录成功:
