sms-message/src/main/java/cn/com/tenlion/smessage/login/KeXieUserAuthenticationProvider.java
java_cuibaocheng@163.com 13dd81a5c0 1. 短信发送修改模糊搜索功能
2. 短信模板页面填充问题修改
2023-12-13 12:46:05 +08:00

96 lines
4.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package cn.com.tenlion.smessage.login;
import cn.com.tenlion.projectconfig.util.ProjectConfigUtil;
import cn.com.tenlion.smessage.login.entity.KeXieUserInfo;
import cn.com.tenlion.smessage.util.SpringSmessageUtil;
import ink.wgink.interfaces.expand.login.ILoginHandlerService;
import ink.wgink.interfaces.role.IRoleBaseService;
import ink.wgink.login.base.authentication.user.UserAuthenticationToken;
import ink.wgink.login.base.enums.LoginTypeEnum;
import ink.wgink.login.base.exceptions.UserAuthenticationException;
import ink.wgink.login.base.service.user.UserLoginService;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.service.role.service.IRoleUserService;
import ink.wgink.service.user.enums.UserStateEnum;
import ink.wgink.service.user.enums.UserTypeEnum;
import ink.wgink.service.user.pojo.pos.UserPO;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.service.user.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
/**
* @ClassName: KeXieUserAuthenticationProvider
* @Description:
* @Author: wanggeng
* @Date: 2023/2/10 09:21
* @Version: 1.0
*/
public class KeXieUserAuthenticationProvider implements AuthenticationProvider {
private static final Logger LOG = LoggerFactory.getLogger(KeXieUserAuthenticationProvider.class);
private IUserService userService;
@Autowired
private IRoleUserService iRoleUserService;
private UserLoginService userLoginService;
private ILoginHandlerService loginHandler;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
KeXieUserAuthenticationToken keXieUserAuthenticationToken = (KeXieUserAuthenticationToken) authentication;
String username = keXieUserAuthenticationToken.getPrincipal().toString();
KeXieUserInfo keXieUserInfo = (KeXieUserInfo) keXieUserAuthenticationToken.getCredentials();
// 查找 openid 用户名
UserPO userPO = userService.getPOByUsername(username.toLowerCase());
if (userPO == null) {
System.out.println(username);
LOG.debug("用户不存在,创建用户, openid{}, mobile: {}", username, keXieUserInfo.getMobile());
// 本地创建用户
userService.saveDefaultUserAndReturnId(username, keXieUserInfo.getNickname(), UserStateEnum.NORMAL.getValue(), UserTypeEnum.SYSTEM.getValue());
userPO = userService.getPOByUsername(username);
// 默认的角色
IRoleUserService roleUserService = SpringSmessageUtil.getBean(IRoleUserService.class);
roleUserService.save(ProjectConfigUtil.getText("defaultRoleId"), userPO.getUserId());
}
LoginUser loginUser = UserUtil.createLoginUser(userPO);
userLoginService.setLoginUserInfo(loginUser);
loginHandler(loginUser);
userLoginService.updateUserLoginInfo(loginUser.getUserId(), loginUser.getUserName(), LoginTypeEnum.USERNAME_AND_PASSWORD.getValue());
UserAuthenticationToken userAuthenticationTokenResult = new UserAuthenticationToken(loginUser, null, loginUser.getAuthorities());
userAuthenticationTokenResult.setDetails(keXieUserAuthenticationToken.getDetails());
return userAuthenticationTokenResult;
}
private void loginHandler(LoginUser loginUser) {
if (loginHandler != null) {
try {
loginHandler.handle(loginUser);
} catch (Exception e) {
LOG.error(e.getMessage(), e);
throw new UserAuthenticationException("登录异常");
}
}
}
@Override
public boolean supports(Class<?> authentication) {
return KeXieUserAuthenticationToken.class.isAssignableFrom(authentication);
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public void setUserLoginService(UserLoginService userLoginService) {
this.userLoginService = userLoginService;
}
public void setLoginHandler(ILoginHandlerService loginHandler) {
this.loginHandler = loginHandler;
}
}