96 lines
4.3 KiB
Java
96 lines
4.3 KiB
Java
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;
|
||
}
|
||
}
|