From 450ca8f907f6e6416d8edb9353a1a00e5a0ceb04 Mon Sep 17 00:00:00 2001
From: timfruit
Date: Wed, 8 Dec 2021 00:17:01 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=89=A9=E4=B8=8B=E7=9A=84?=
=?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?=
=?UTF-8?q?=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../controller/auth/SysAuthController.java | 16 +---
.../auth/vo/MbrAuthSocialLogin2ReqVO.java | 19 ++--
.../system/service/auth/SysAuthService.java | 2 +-
.../service/auth/impl/SysAuthServiceImpl.java | 16 ++--
.../main/resources/static/social-login.html | 6 +-
.../main/resources/static/social-login2.html | 87 +++++++++++++++++++
6 files changed, 112 insertions(+), 34 deletions(-)
create mode 100644 yudao-user-server/src/main/resources/static/social-login2.html
diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java
index fbca500f7..5eb0e533d 100644
--- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java
+++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java
@@ -81,19 +81,6 @@ public class SysAuthController {
return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri));
}
- // TODO @timfruit:这个接口,是要删除的么?
- @GetMapping("/social-login-get")
- @ApiOperation("微信公众号授权回调地址,输出social-login2的必要参数用于测试,使用 code 授权码")
- @ResponseBody
- @Deprecated
- public CommonResult socialLoginGet(HttpServletRequest request,String code,String state) {
- // 返回结果
- MbrAuthSocialLoginReqVO reqVO = MbrAuthSocialLoginReqVO.builder().state(state).code(code).build();
- reqVO.setType(12);
- //输出social-login2的必要参数用于测试
- System.out.println(JSON.toJSON(reqVO));
- return success(reqVO);
- }
@PostMapping("/social-login")
@ApiOperation("社交登录,使用 code 授权码")
@@ -102,10 +89,9 @@ public class SysAuthController {
return success(SysAuthLoginRespVO.builder().token(token).build());
}
- // TODO @timfruit:社交登陆时,使用手机验证码来验证哈。这块我当时没设计好,改改,嘿嘿。
@PostMapping("/social-login2")
- @ApiOperation("社交登录,使用 code 授权码 + 账号密码")
+ @ApiOperation("社交登录,使用 手机号 + 手机验证码")
public CommonResult socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) {
String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent());
return success(SysAuthLoginRespVO.builder().token(token).build());
diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java
index 584df7ed1..1da763ed3 100644
--- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java
+++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java
@@ -34,15 +34,16 @@ public class MbrAuthSocialLogin2ReqVO {
@NotEmpty(message = "state 不能为空")
private String state;
- @ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma")
- @NotEmpty(message = "登录账号不能为空")
- @Length(min = 4, max = 16, message = "账号长度为 4-16 位")
- @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母")
- private String username;
+ @ApiModelProperty(value = "手机号", required = true, example = "15119100000")
+ @NotEmpty(message = "手机号不能为空")
+ @Length(min = 11, max = 11, message = "手机号是11位数字")
+ private String mobile;
+
+ @ApiModelProperty(value = "手机验证码", required = true, example = "1024")
+ @NotEmpty(message = "手机验证码不能为空")
+ @Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位")
+ @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字")
+ private String smsCode;
- @ApiModelProperty(value = "密码", required = true, example = "buzhidao")
- @NotEmpty(message = "密码不能为空")
- @Length(min = 4, max = 16, message = "密码长度为 4-16 位")
- private String password;
}
diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java
index 628f95c80..3b6b73691 100644
--- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java
+++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/SysAuthService.java
@@ -46,7 +46,7 @@ public interface SysAuthService extends SecurityAuthFrameworkService {
String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent);
/**
- * 社交登录,使用 code 授权码 + 账号密码
+ * 社交登录,使用 手机号 + 手机验证码
*
* @param reqVO 登录信息
* @param userIp 用户 IP
diff --git a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
index c5c512890..bd9100b85 100644
--- a/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
+++ b/yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/service/auth/impl/SysAuthServiceImpl.java
@@ -140,19 +140,21 @@ public class SysAuthServiceImpl implements SysAuthService {
@Override
public String socialLogin2(MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) {
- // 使用 code 授权码,进行登录
AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState());
org.springframework.util.Assert.notNull(authUser, "授权用户不为空");
- // 使用账号密码,进行登录。
- LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword());
-// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表
+ // 使用手机号、手机验证码登录
+ SysAuthSmsLoginReqVO loginReqVO = SysAuthSmsLoginReqVO
+ .builder()
+ .mobile(reqVO.getMobile())
+ .code(reqVO.getSmsCode())
+ .build();
+ String sessionId = this.smsLogin(loginReqVO, userIp, userAgent);
+ LoginUser loginUser = userSessionCoreService.getLoginUser(sessionId);
// 绑定社交用户(新增)
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
-
- // 缓存登录用户到 Redis 中,返回 sessionId 编号
- return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
+ return sessionId;
}
@Override
diff --git a/yudao-user-server/src/main/resources/static/social-login.html b/yudao-user-server/src/main/resources/static/social-login.html
index 2570d7084..7cf09f00e 100644
--- a/yudao-user-server/src/main/resources/static/social-login.html
+++ b/yudao-user-server/src/main/resources/static/social-login.html
@@ -13,14 +13,16 @@
+点击如下按钮,授权登录
+
+ 手机号
+ 手机验证码
+
+
+
+
+