Spring Security 自定义AuthenticationProvider认证类
Spring Security中进行身份验证的是AuthenticationManager接口,ProviderManager是它的一个默认实现,但它并不用来处理身份认证,而是委托给配置好的AuthenticationProvider,每个AuthenticationProvider会轮流检查身份认证。
具体流程可参考Spring Security认证流程
此次记录在Spring Security 连接数据库实现认证的基础上增加。
自定义AuthenticationProvider:
package cn.sivan.provider;
import cn.sivan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
@Component
public class LoginAuthenticationProvider implements AuthenticationProvider {
@Autowired
private UserService userService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//用户名 密码
String username = authentication.getName();
String password = (String) authentication.getCredentials();
//通过用户名获取用户信息
UserDetails userDetails = userService.loadUserByUsername(username);
if (userDetails == null) {
throw new UsernameNotFoundException("用户不存在!");
}
if (!passwordEncoder.matches(password, userDetails.getPassword())) {
throw new BadCredentialsException("密码不正确!");
}
return new UsernamePasswordAuthenticationToken(username, password, userDetails.getAuthorities());
}
/**
* AuthenticationManager 本身不包含认证逻辑,其核心是用来管理所有的 AuthenticationProvider,通过交由合适的 AuthenticationProvider 来实现认证。
* ProviderManager 是AuthenticationProvider的实现类
* AuthenticationManager获取所有AuthenticationProvider的实现
* 通过该方法判断是否支持当前方式的认证
* 这里支持验证UsernamePasswordAuthenticationToken
* @param authentication
* @return
*/
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
SpringSecurity配置:
package cn.sivan.config;
import cn.sivan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@EnableWebSecurity
public class SpringSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Autowired
private AuthenticationProvider loginValidateAuthenticationProvider;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//指定userDetailsService
auth.userDetailsService(userService).passwordEncoder(passwordEncoder);
//注册自定义认证
auth.authenticationProvider(loginValidateAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/favicon.ico").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login/user")
.loginProcessingUrl("/login")
.defaultSuccessUrl("/")
.failureUrl("/login/failure")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/login/user")
.logoutUrl("/logout")
.invalidateHttpSession(true)
.permitAll()
.and()
.csrf()
.disable();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Spring Security 自定义AuthenticationProvider认证类
原文:https://www.cnblogs.com/sivanchan/p/13643157.html