package com.zbkj.admin.manager;
import cn.hutool.core.util.ObjectUtil;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.utils.CrmebUtil;
import com.zbkj.common.vo.LoginUserVo;
import com.zbkj.service.service.impl.UserDetailServiceImpl;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;
/**
* 自定义验证(admin登录)
*/
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private UserDetailServiceImpl userDetailsService;
public CustomAuthenticationProvider(UserDetailServiceImpl userDetailsService) {
this.userDetailsService = userDetailsService;
}
// private final UserDetailServiceImpl userDetailsService = new UserDetailServiceImpl();
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String name = authentication.getPrincipal().toString();
String password = authentication.getCredentials().toString();
//以下自定义方法,判断是否登录成功
LoginUserVo userDetails = (LoginUserVo) userDetailsService.loadUserByUsername(name);
if (ObjectUtil.isNull(userDetails)) {
throw new CrmebException("用户名不存在");
}
// base64加密获取真正密码
String encryptPassword = CrmebUtil.encryptPassword(password, name);
if (!userDetails.getUser().getPwd().equals(encryptPassword)) {
throw new CrmebException("账号或者密码不正确");
}
return new UsernamePasswordAuthenticationToken(userDetails, password, userDetails.getAuthorities());
}
@Override
public boolean supports(Class<?> authentication) {
//确保authentication能转成该类
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
计算机毕业设计-CRMEB-Java版商城系统-manager.zip (4个子文件)
manager
CustomAccessDeniedHandler.java 1KB
CusAuthenticationManager.java 1KB
CustomAuthenticationProvider.java 2KB
AuthenticationEntryPointImpl.java 1KB
共 4 条
- 1
资源评论
BinaryStarXin
- 粉丝: 8865
- 资源: 287
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功