chore: 移除微信授权登录和AI 模块

This commit is contained in:
Ray.Hao
2026-02-11 11:35:04 +08:00
parent 0fb278f6ff
commit e735f768b5
37 changed files with 39 additions and 2104 deletions

View File

@@ -1,69 +0,0 @@
package com.youlai.boot.security.model;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import java.io.Serial;
import java.util.Collection;
/**
* 微信小程序Code认证Token
*
* @author 有来技术团队
* @since 2.0.0
*/
public class WxMiniAppCodeAuthenticationToken extends AbstractAuthenticationToken {
@Serial
private static final long serialVersionUID = 621L;
private final Object principal;
/**
* 微信小程序Code认证Token (未认证)
*
* @param principal 微信code
*/
public WxMiniAppCodeAuthenticationToken(Object principal) {
// 没有授权信息时,设置为 null
super((Collection<? extends GrantedAuthority>) null);
this.principal = principal;
// 默认未认证
this.setAuthenticated(false);
}
/**
* 微信小程序Code认证Token (已认证)
*
* @param principal 微信用户信息
* @param authorities 授权信息
*/
public WxMiniAppCodeAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
// 认证通过
super.setAuthenticated(true);
}
/**
* 认证通过
*
* @param principal 微信用户信息
* @param authorities 授权信息
* @return 已认证的Token
*/
public static WxMiniAppCodeAuthenticationToken authenticated(Object principal, Collection<? extends GrantedAuthority> authorities) {
return new WxMiniAppCodeAuthenticationToken(principal, authorities);
}
@Override
public Object getCredentials() {
// 微信认证不需要密码
return null;
}
@Override
public Object getPrincipal() {
return this.principal;
}
}

View File

@@ -1,89 +0,0 @@
package com.youlai.boot.security.model;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import java.io.Serial;
import java.util.Collection;
/**
* 微信小程序手机号认证Token
*
* @author 有来技术团队
* @since 2.0.0
*/
public class WxMiniAppPhoneAuthenticationToken extends AbstractAuthenticationToken {
@Serial
private static final long serialVersionUID = 622L;
private final Object principal; // code
private String encryptedData;
private String iv;
/**
* 微信小程序手机号认证Token (未认证)
*
* @param code 微信登录code
* @param encryptedData 加密数据
* @param iv 初始向量
*/
public WxMiniAppPhoneAuthenticationToken(String code, String encryptedData, String iv) {
super((Collection<? extends GrantedAuthority>) null);
this.principal = code;
this.encryptedData = encryptedData;
this.iv = iv;
this.setAuthenticated(false);
}
/**
* 微信小程序手机号认证Token (已认证)
*
* @param principal 用户信息
* @param authorities 授权信息
*/
public WxMiniAppPhoneAuthenticationToken(Object principal, Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
super.setAuthenticated(true);
}
/**
* 认证通过
*
* @param principal 用户信息
* @param authorities 授权信息
* @return 认证通过的Token
*/
public static WxMiniAppPhoneAuthenticationToken authenticated(Object principal, Collection<? extends GrantedAuthority> authorities) {
return new WxMiniAppPhoneAuthenticationToken(principal, authorities);
}
@Override
public Object getCredentials() {
// 微信小程序手机号认证不需要密码
return null;
}
@Override
public Object getPrincipal() {
return this.principal;
}
/**
* 获取加密数据
*
* @return 加密数据
*/
public String getEncryptedData() {
return encryptedData;
}
/**
* 获取初始向量
*
* @return 初始向量
*/
public String getIv() {
return iv;
}
}

View File

@@ -1,95 +0,0 @@
package com.youlai.boot.security.provider;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.youlai.boot.security.model.SysUserDetails;
import com.youlai.boot.security.model.UserAuthInfo;
import com.youlai.boot.security.model.WxMiniAppCodeAuthenticationToken;
import com.youlai.boot.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
/**
* 微信小程序Code认证Provider
*
* @author 有来技术团队
* @since 2.0.0
*/
@Slf4j
public class WxMiniAppCodeAuthenticationProvider implements AuthenticationProvider {
private final UserService userService;
private final WxMaService wxMaService;
public WxMiniAppCodeAuthenticationProvider(UserService userService, WxMaService wxMaService) {
this.userService = userService;
this.wxMaService = wxMaService;
}
/**
* 微信认证逻辑,参考 Spring Security 认证密码校验流程
*
* @param authentication 认证对象
* @return 认证后的 Authentication 对象
* @throws AuthenticationException 认证异常
* @see org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider#authenticate(Authentication)
*/
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String code = (String) authentication.getPrincipal();
// 通过微信服务端验证 code 并获取用户会话信息
WxMaJscode2SessionResult sessionInfo;
try {
sessionInfo = wxMaService.getUserService().getSessionInfo(code);
} catch (WxErrorException e) {
throw new CredentialsExpiredException("微信登录 code 无效或已失效,请重新获取");
}
String openId = sessionInfo.getOpenid();
if (StrUtil.isBlank(openId)) {
throw new UsernameNotFoundException("未能获取到微信 OpenID请稍后重试");
}
// 根据微信 OpenID 查询用户信息
UserAuthInfo userAuthInfo = userService.getAuthInfoByOpenId(openId);
if (userAuthInfo == null) {
// 用户不存在则注册
userService.registerOrBindWechatUser(openId);
// 再次查询用户信息,确保用户注册成功
userAuthInfo = userService.getAuthInfoByOpenId(openId);
if (userAuthInfo == null) {
throw new UsernameNotFoundException("用户注册失败,请稍后重试");
}
}
// 检查用户状态是否有效
if (ObjectUtil.notEqual(userAuthInfo.getStatus(), 1)) {
throw new DisabledException("用户已被禁用");
}
// 构建认证后的用户详情信息
SysUserDetails userDetails = new SysUserDetails(userAuthInfo);
// 创建已认证的Token
return WxMiniAppCodeAuthenticationToken.authenticated(
userDetails,
userDetails.getAuthorities()
);
}
@Override
public boolean supports(Class<?> authentication) {
return WxMiniAppCodeAuthenticationToken.class.isAssignableFrom(authentication);
}
}

View File

@@ -1,115 +0,0 @@
package com.youlai.boot.security.provider;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.youlai.boot.security.model.SysUserDetails;
import com.youlai.boot.security.model.UserAuthInfo;
import com.youlai.boot.security.model.WxMiniAppPhoneAuthenticationToken;
import com.youlai.boot.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.CredentialsExpiredException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
/**
* 微信小程序手机号认证Provider
*
* @author 有来技术团队
* @since 2.0.0
*/
@Slf4j
public class WxMiniAppPhoneAuthenticationProvider implements AuthenticationProvider {
private final UserService userService;
private final WxMaService wxMaService;
public WxMiniAppPhoneAuthenticationProvider(UserService userService, WxMaService wxMaService) {
this.userService = userService;
this.wxMaService = wxMaService;
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
WxMiniAppPhoneAuthenticationToken authenticationToken = (WxMiniAppPhoneAuthenticationToken) authentication;
String code = (String) authenticationToken.getPrincipal();
String encryptedData = authenticationToken.getEncryptedData();
String iv = authenticationToken.getIv();
// 1. 通过code获取session_key
WxMaJscode2SessionResult sessionInfo;
try {
sessionInfo = wxMaService.getUserService().getSessionInfo(code);
} catch (WxErrorException e) {
log.error("获取微信session_key失败", e);
throw new CredentialsExpiredException("微信登录code无效或已过期");
}
String sessionKey = sessionInfo.getSessionKey();
String openId = sessionInfo.getOpenid();
if (StrUtil.isBlank(sessionKey) || StrUtil.isBlank(openId)) {
throw new CredentialsExpiredException("获取微信会话信息失败");
}
// 2. 解密手机号信息
WxMaPhoneNumberInfo phoneNumberInfo;
try {
if (StrUtil.isNotBlank(encryptedData) && StrUtil.isNotBlank(iv)) {
phoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
} else {
throw new IllegalArgumentException("缺少手机号加密数据");
}
} catch (Exception e) {
log.error("解密微信手机号失败", e);
throw new CredentialsExpiredException("解密手机号信息失败");
}
if (phoneNumberInfo == null || StrUtil.isBlank(phoneNumberInfo.getPhoneNumber())) {
throw new CredentialsExpiredException("获取手机号失败");
}
String phoneNumber = phoneNumberInfo.getPhoneNumber();
// 3. 根据手机号查询用户,不存在则创建新用户
UserAuthInfo userAuthInfo = userService.getAuthInfoByMobile(phoneNumber);
if (userAuthInfo == null) {
// 用户不存在,注册新用户
boolean registered = userService.registerUserByMobileAndOpenId(phoneNumber, openId);
if (!registered) {
throw new UsernameNotFoundException("用户注册失败");
}
// 重新获取用户信息
userAuthInfo = userService.getAuthInfoByMobile(phoneNumber);
} else {
// 用户存在绑定openId如果未绑定
userService.bindUserOpenId(userAuthInfo.getUserId(), openId);
}
// 4. 检查用户状态
if (ObjectUtil.notEqual(userAuthInfo.getStatus(), 1)) {
throw new DisabledException("用户已被禁用");
}
// 5. 构建认证后的用户详情
SysUserDetails userDetails = new SysUserDetails(userAuthInfo);
// 6. 创建已认证的Token
return WxMiniAppPhoneAuthenticationToken.authenticated(
userDetails,
userDetails.getAuthorities()
);
}
@Override
public boolean supports(Class<?> authentication) {
return WxMiniAppPhoneAuthenticationToken.class.isAssignableFrom(authentication);
}
}