spring security支持传统的查询数据库方式,也即根据用户名查询出用户信息,然后再比较前端传来的密码和数据库中的密码。
这种方式很常用。但不适合通过第三方进行的授权认证模式。这时候,我们需要自己处理登录验证过程。
针对这些情况,我们需要自定义Authentication Provider
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private UserDetailsService userDetailsService;
@Override
public Authentication authenticate(Authentication authentication)
throws AuthenticationException {
String name = authentication.getName();
String password = authentication.getCredentials().toString();
//以下自定义方法,判断是否登录成功
if (isAuthenticated()) {
// 下面的权限,可以通过UserDetailsService获取得到
return new UsernamePasswordAuthenticationToken(
name, password, new ArrayList<>());
} else {
return null;
}
}
@Override
public boolean supports(Class<?> authentication) {
return

本文介绍了如何在Spring Boot应用中整合Spring Security,并实现自定义验证方式,以适应第三方授权认证的需求。通过创建自定义Authentication Provider,并在配置类中注入该provider,重写WebSecurityConfigurerAdapter的configure方法,可以实现非传统数据库查询密码的登录验证过程。
最低0.47元/天 解锁文章
528

被折叠的 条评论
为什么被折叠?



