110 lines
4.2 KiB
Java
110 lines
4.2 KiB
Java
package com.xkrs.common.account;
|
|
|
|
import com.xkrs.model.entity.SysAuthorityEntity;
|
|
import com.xkrs.model.entity.SysUserEntity;
|
|
import com.xkrs.service.SysAuthorityService;
|
|
import com.xkrs.service.SysRoleService;
|
|
import com.xkrs.service.SysUserService;
|
|
import com.xkrs.utilsold.DateTimeUtil;
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
import org.springframework.security.authentication.DisabledException;
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.core.AuthenticationException;
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.Resource;
|
|
import java.time.LocalDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static com.xkrs.utilsnew.EncryptDecryptUtil.encry256;
|
|
|
|
/**
|
|
* 自定义认证Provider
|
|
*/
|
|
@Component
|
|
public class CustomAuthenticationProvider implements AuthenticationProvider {
|
|
|
|
@Resource
|
|
private SysUserService sysUserService;
|
|
|
|
@Resource
|
|
private SysRoleService sysRoleService;
|
|
|
|
@Resource
|
|
private SysAuthorityService sysAuthorityService;
|
|
|
|
/**
|
|
* 初使化时将已静态化的Service实例化
|
|
*/
|
|
protected static CustomAuthenticationProvider customAuthenticationProvider;
|
|
|
|
/**
|
|
* 通过@PostConstruct实现初始化bean之前进行的操作
|
|
*/
|
|
@PostConstruct
|
|
public void init() {
|
|
customAuthenticationProvider = this;
|
|
customAuthenticationProvider.sysUserService = this.sysUserService;
|
|
customAuthenticationProvider.sysRoleService = this.sysRoleService;
|
|
customAuthenticationProvider.sysAuthorityService = this.sysAuthorityService;
|
|
}
|
|
|
|
/**
|
|
* 用户认证授权
|
|
*
|
|
* @param authentication
|
|
* @return
|
|
* @throws AuthenticationException
|
|
*/
|
|
@Override
|
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
|
// 获取认证的用户名 & 密码
|
|
String userName = authentication.getName();
|
|
String password = authentication.getCredentials().toString();
|
|
SysUserEntity userEntity = customAuthenticationProvider.sysUserService.getSysUserByUserName(userName);
|
|
|
|
// 检查用户是否存在
|
|
if (userEntity == null) {
|
|
throw new BadCredentialsException("您的账号不存在,请您前往注册。");
|
|
}
|
|
// 检查用户是否激活
|
|
if (userEntity.getActiveFlag() != 0) {
|
|
throw new DisabledException("您的账号未激活,详情请联系客服人员。");
|
|
}
|
|
|
|
// 认证逻辑
|
|
String encryptPassword = encry256(password + userEntity.getSalt());
|
|
if (encryptPassword.equals(userEntity.getPassword())) {
|
|
// 设置权限列表
|
|
ArrayList<GrantedAuthority> permissions = new ArrayList<>();
|
|
List<Long> longs = customAuthenticationProvider.sysAuthorityService.selectAuthorityByUserId(userEntity.getId());
|
|
List<SysAuthorityEntity> permissionList = customAuthenticationProvider.sysAuthorityService.findAllByIdIn(longs);
|
|
for (SysAuthorityEntity sysAuthorityEntity : permissionList) {
|
|
permissions.add(new GrantedAuthorityImpl(sysAuthorityEntity.getAuthorityName()));
|
|
}
|
|
customAuthenticationProvider.sysUserService.updateLoginNum(userEntity.getId(), userEntity.getLoginNum() + 1);
|
|
customAuthenticationProvider.sysUserService.updateLoginLastTime(userEntity.getId(), DateTimeUtil.dateTimeToString(LocalDateTime.now()));
|
|
// 生成令牌
|
|
return new UsernamePasswordAuthenticationToken(userName, encryptPassword, permissions);
|
|
} else {
|
|
throw new BadCredentialsException("用户密码错误,请重新输入");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 是否可以提供输入类型的认证服务
|
|
*
|
|
* @param authentication
|
|
* @return
|
|
*/
|
|
@Override
|
|
public boolean supports(Class<?> authentication) {
|
|
return authentication.equals(UsernamePasswordAuthenticationToken.class);
|
|
}
|
|
}
|