MSCode微服务平台社区
源码
源码
登录
注册
首页
提问
分享
建议
讨论
公告
动态
需求
+
发表新帖
手机验证码登陆,如何自定义异常
提问
1
650
邮箱用户_9sc6b
LV2
2021年7月23日 16:59
发表
点击群号免费加入社区交流群:
367346704
。
<p><br></p>
收藏(
0
)
分享
相关标签:
spring
1个回复
站长
站长
2021年7月23日 19:26
1、自定义验证器的代码示例: [pre] import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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.UserDetailsService; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; @Service public class AuthenticationProviderImpl implements AuthenticationProvider { @Autowired @Qualifier("userDetailsServiceImpl") private UserDetailsService userDetailsService; @Override public Authentication authenticate(Authentication authentication) throws AuthenticationException { String username=authentication.getName(); String password=authentication.getCredentials().toString(); Account account=(Account)userDetailsService.loadUserByUsername(username); if(account.isLocked()){ throw new BadCredentialsException("帐号已锁定!"); } if(account.isExpire()){ throw new BadCredentialsException("帐号已过期!"); } if(!new BCryptPasswordEncoder().matches(password,account.getPassword())){ throw new BadCredentialsException("密码错误!"); } return new UsernamePasswordAuthenticationToken(account,account.getPassword(),account.getAuthorities()); } @Override public boolean supports(Class<?> aClass) { return UsernamePasswordAuthenticationToken.class.equals(aClass); } } [/pre] 2、在WebSecurityConfig里使用自定义验证器: [pre] @Autowired @Qualifier("authenticationProviderImpl") private AuthenticationProvider authenticationProvider; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { // auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); auth.authenticationProvider(authenticationProvider); } [/pre]
0
0
评论
回复
所在专栏
提问
分享
建议
讨论
动态
需求
阅读权限
默认,所有用户可见
LV1及以上用户可见
LV2及以上用户可见
LV3及以上用户可见
LV4及以上用户可见
LV5及以上用户可见
LV6及以上用户可见
会员专享
管理员可见
确认修改
相关帖子
Spring认证中国教育管理中心-Apache Cassandra 的 Spring 数据
0
如何修改登陆信息
1
获取用户信息出错
1
windows中运行springboot后台报错,但在idea运行则正常?
1
springboot项目运行在tomcat中出错
1
「Spring认证」Spring 框架概述
0
mongodb链接报错
8
持续集成自动化部署教程
1
登陆时,用户名或密码错误如何优化
1
同时请求不同的接口,有时返回相同的数据?
3
精选阿里云爆款特惠
点击图片或者QQ扫码加入社区官方交流群
友情链接
Spring Cloud
申请友链