wip: 微信登录重构临时提交

This commit is contained in:
Ray.Hao
2025-05-30 00:01:07 +08:00
parent 7d5b7f0a63
commit be9faa2445
3 changed files with 65 additions and 40 deletions

View File

@@ -63,18 +63,37 @@ public class AuthController {
return Result.success(authenticationToken);
}
@Operation(summary = "微信小程序授权登录")
@PostMapping("/login/wechat-mini-program")
@Log(value = "微信登录", module = LogModuleEnum.LOGIN)
public Result<AuthenticationToken> loginByWechatMiniProgram(
@Parameter(description = "微信授权码", example = "code") @RequestParam String code
@Operation(
summary = "微信小程序授权码登录",
description = "通过微信临时授权码(code)快速登录,自动注册未绑定用户"
)
@PostMapping("/wx/miniapp/code-login")
@Log(value = "微信授权码登录", module = LogModuleEnum.LOGIN)
public Result<AuthenticationToken> loginByWechatMiniCode(
@Parameter(description = "微信临时登录凭证", example = "071XHa000ABCdefGHI1234567890XHa3") @RequestParam String code
) {
AuthenticationToken loginResult = authService.loginByWechatMiniProgram(code);
AuthenticationToken loginResult = authService.loginByWechatMiniCode(code);
return Result.success(loginResult);
}
@Operation(
summary = "微信小程序手机号一键登录",
description = "通过加密数据(encryptedData+iv)获取用户手机号并登录"
)
@PostMapping("/wx/miniapp/phone-login")
@Log(value = "微信手机号一键登录", module = LogModuleEnum.LOGIN)
public Result<AuthenticationToken> loginByWechatMiniPhone(
@Parameter(description = "微信临时登录凭证", example = "071XHa000ABCdefGHI1234567890XHa3") @RequestParam String code,
@Parameter(description = "加密的手机号数据", example = "CiyLU1Aw2KjvrjMdj8YKli...") @RequestParam String encryptedData,
@Parameter(description = "解密算法初始向量", example = "r7BXXKkLb8qrSNn05n0qiA==") @RequestParam String iv
) {
AuthenticationToken loginResult = authService.loginByWechatMiniPhone(code, encryptedData, iv);
return Result.success(loginResult);
}
@Operation(summary = "发送登录短信验证码")
@PostMapping("/login/sms/code")
@PostMapping("/sms/code")
public Result<Void> sendLoginVerifyCode(
@Parameter(description = "手机号", example = "18812345678") @RequestParam String mobile
) {
@@ -83,7 +102,7 @@ public class AuthController {
}
@Operation(summary = "短信验证码登录")
@PostMapping("/login/sms")
@PostMapping("/sms")
@Log(value = "短信验证码登录", module = LogModuleEnum.LOGIN)
public Result<AuthenticationToken> loginBySms(
@Parameter(description = "手机号", example = "18812345678") @RequestParam String mobile,

View File

@@ -40,14 +40,6 @@ public interface AuthService {
*/
AuthenticationToken refreshToken(String refreshToken);
/**
* 微信小程序登录
*
* @param code 微信登录code
* @return 登录结果
*/
AuthenticationToken loginByWechatMiniProgram(String code);
/**
* 发送短信验证码
*
@@ -63,4 +55,20 @@ public interface AuthService {
* @return 登录结果
*/
AuthenticationToken loginBySms(String mobile, String code);
/**
* 微信小程序登录
*
* @param code 微信登录code
* @return 登录结果
*/
AuthenticationToken loginByWechatMiniCode(String code);
/**
* 微信小程序登录
*
* @param code 微信登录code
* @return 登录结果
*/
AuthenticationToken loginByWechatMiniPhone( String code,String encryptedData, String iv);
}

View File

@@ -78,26 +78,6 @@ public class AuthServiceImpl implements AuthService {
return authenticationTokenResponse;
}
/**
* 微信一键授权登录
*
* @param code 微信登录code
* @return 访问令牌
*/
@Override
public AuthenticationToken loginByWechatMiniProgram(String code) {
// 1. 创建用户微信认证的令牌(未认证)
WechatAuthenticationToken wechatAuthenticationToken = new WechatAuthenticationToken(code);
// 2. 执行认证(认证中)
Authentication authentication = authenticationManager.authenticate(wechatAuthenticationToken);
// 3. 认证成功后生成 JWT 令牌,并存入 Security 上下文,供登录日志 AOP 使用(已认证)
AuthenticationToken authenticationToken = tokenManager.generateToken(authentication);
SecurityContextHolder.getContext().setAuthentication(authentication);
return authenticationToken;
}
/**
* 发送登录短信验证码
@@ -108,10 +88,8 @@ public class AuthServiceImpl implements AuthService {
public void sendSmsLoginCode(String mobile) {
// 随机生成4位验证码
// String code = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
// TODO 为了方便测试,验证码固定为 1234实际开发中在配置了厂商短信服务后可以使用上面的随机验证码
String code = "1234";
String code = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
log.info("【调试模式】手机号 {} 的验证码为:{}", mobile, code);
// 发送短信验证码
Map<String, String> templateParams = new HashMap<>();
templateParams.put("code", code);
@@ -146,6 +124,26 @@ public class AuthServiceImpl implements AuthService {
return authenticationToken;
}
@Override
public AuthenticationToken loginByWechatMiniCode(String code) {
// 1. 创建用户微信认证的令牌(未认证)
WechatAuthenticationToken wechatAuthenticationToken = new WechatAuthenticationToken(code);
// 2. 执行认证(认证中)
Authentication authentication = authenticationManager.authenticate(wechatAuthenticationToken);
// 3. 认证成功后生成 JWT 令牌,并存入 Security 上下文,供登录日志 AOP 使用(已认证)
AuthenticationToken authenticationToken = tokenManager.generateToken(authentication);
SecurityContextHolder.getContext().setAuthentication(authentication);
return authenticationToken;
}
@Override
public AuthenticationToken loginByWechatMiniPhone(String code, String encryptedData, String iv) {
return null;
}
/**
* 注销登录
*/