Commit 450ca8f907f6e6416d8edb9353a1a00e5a0ceb04
1 parent
13a94050
完成剩下的公众号登录功能修改
Showing
6 changed files
with
113 additions
and
35 deletions
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/SysAuthController.java
| @@ -81,19 +81,6 @@ public class SysAuthController { | @@ -81,19 +81,6 @@ public class SysAuthController { | ||
| 81 | return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); | 81 | return CommonResult.success(socialService.getAuthorizeUrl(type, redirectUri)); |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | - // TODO @timfruit:这个接口,是要删除的么? | ||
| 85 | - @GetMapping("/social-login-get") | ||
| 86 | - @ApiOperation("微信公众号授权回调地址,输出social-login2的必要参数用于测试,使用 code 授权码") | ||
| 87 | - @ResponseBody | ||
| 88 | - @Deprecated | ||
| 89 | - public CommonResult<MbrAuthSocialLoginReqVO> socialLoginGet(HttpServletRequest request,String code,String state) { | ||
| 90 | - // 返回结果 | ||
| 91 | - MbrAuthSocialLoginReqVO reqVO = MbrAuthSocialLoginReqVO.builder().state(state).code(code).build(); | ||
| 92 | - reqVO.setType(12); | ||
| 93 | - //输出social-login2的必要参数用于测试 | ||
| 94 | - System.out.println(JSON.toJSON(reqVO)); | ||
| 95 | - return success(reqVO); | ||
| 96 | - } | ||
| 97 | 84 | ||
| 98 | @PostMapping("/social-login") | 85 | @PostMapping("/social-login") |
| 99 | @ApiOperation("社交登录,使用 code 授权码") | 86 | @ApiOperation("社交登录,使用 code 授权码") |
| @@ -102,10 +89,9 @@ public class SysAuthController { | @@ -102,10 +89,9 @@ public class SysAuthController { | ||
| 102 | return success(SysAuthLoginRespVO.builder().token(token).build()); | 89 | return success(SysAuthLoginRespVO.builder().token(token).build()); |
| 103 | } | 90 | } |
| 104 | 91 | ||
| 105 | - // TODO @timfruit:社交登陆时,使用手机验证码来验证哈。这块我当时没设计好,改改,嘿嘿。 | ||
| 106 | 92 | ||
| 107 | @PostMapping("/social-login2") | 93 | @PostMapping("/social-login2") |
| 108 | - @ApiOperation("社交登录,使用 code 授权码 + 账号密码") | 94 | + @ApiOperation("社交登录,使用 手机号 + 手机验证码") |
| 109 | public CommonResult<SysAuthLoginRespVO> socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) { | 95 | public CommonResult<SysAuthLoginRespVO> socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) { |
| 110 | String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent()); | 96 | String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent()); |
| 111 | return success(SysAuthLoginRespVO.builder().token(token).build()); | 97 | return success(SysAuthLoginRespVO.builder().token(token).build()); |
yudao-user-server/src/main/java/cn/iocoder/yudao/userserver/modules/system/controller/auth/vo/MbrAuthSocialLogin2ReqVO.java
| @@ -34,15 +34,16 @@ public class MbrAuthSocialLogin2ReqVO { | @@ -34,15 +34,16 @@ public class MbrAuthSocialLogin2ReqVO { | ||
| 34 | @NotEmpty(message = "state 不能为空") | 34 | @NotEmpty(message = "state 不能为空") |
| 35 | private String state; | 35 | private String state; |
| 36 | 36 | ||
| 37 | - @ApiModelProperty(value = "账号", required = true, example = "yudaoyuanma") | ||
| 38 | - @NotEmpty(message = "登录账号不能为空") | ||
| 39 | - @Length(min = 4, max = 16, message = "账号长度为 4-16 位") | ||
| 40 | - @Pattern(regexp = "^[A-Za-z0-9]+$", message = "账号格式为数字以及字母") | ||
| 41 | - private String username; | ||
| 42 | - | ||
| 43 | - @ApiModelProperty(value = "密码", required = true, example = "buzhidao") | ||
| 44 | - @NotEmpty(message = "密码不能为空") | ||
| 45 | - @Length(min = 4, max = 16, message = "密码长度为 4-16 位") | ||
| 46 | - private String password; | 37 | + @ApiModelProperty(value = "手机号", required = true, example = "15119100000") |
| 38 | + @NotEmpty(message = "手机号不能为空") | ||
| 39 | + @Length(min = 11, max = 11, message = "手机号是11位数字") | ||
| 40 | + private String mobile; | ||
| 41 | + | ||
| 42 | + @ApiModelProperty(value = "手机验证码", required = true, example = "1024") | ||
| 43 | + @NotEmpty(message = "手机验证码不能为空") | ||
| 44 | + @Length(min = 4, max = 6, message = "手机验证码长度为 4-6 位") | ||
| 45 | + @Pattern(regexp = "^[0-9]+$", message = "手机验证码必须都是数字") | ||
| 46 | + private String smsCode; | ||
| 47 | + | ||
| 47 | 48 | ||
| 48 | } | 49 | } |
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 { | @@ -46,7 +46,7 @@ public interface SysAuthService extends SecurityAuthFrameworkService { | ||
| 46 | String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent); | 46 | String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent); |
| 47 | 47 | ||
| 48 | /** | 48 | /** |
| 49 | - * 社交登录,使用 code 授权码 + 账号密码 | 49 | + * 社交登录,使用 手机号 + 手机验证码 |
| 50 | * | 50 | * |
| 51 | * @param reqVO 登录信息 | 51 | * @param reqVO 登录信息 |
| 52 | * @param userIp 用户 IP | 52 | * @param userIp 用户 IP |
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 { | @@ -140,19 +140,21 @@ public class SysAuthServiceImpl implements SysAuthService { | ||
| 140 | 140 | ||
| 141 | @Override | 141 | @Override |
| 142 | public String socialLogin2(MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { | 142 | public String socialLogin2(MbrAuthSocialLogin2ReqVO reqVO, String userIp, String userAgent) { |
| 143 | - // 使用 code 授权码,进行登录 | ||
| 144 | AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); | 143 | AuthUser authUser = socialService.getAuthUser(reqVO.getType(), reqVO.getCode(), reqVO.getState()); |
| 145 | org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); | 144 | org.springframework.util.Assert.notNull(authUser, "授权用户不为空"); |
| 146 | 145 | ||
| 147 | - // 使用账号密码,进行登录。 | ||
| 148 | - LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); | ||
| 149 | -// loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 | 146 | + // 使用手机号、手机验证码登录 |
| 147 | + SysAuthSmsLoginReqVO loginReqVO = SysAuthSmsLoginReqVO | ||
| 148 | + .builder() | ||
| 149 | + .mobile(reqVO.getMobile()) | ||
| 150 | + .code(reqVO.getSmsCode()) | ||
| 151 | + .build(); | ||
| 152 | + String sessionId = this.smsLogin(loginReqVO, userIp, userAgent); | ||
| 153 | + LoginUser loginUser = userSessionCoreService.getLoginUser(sessionId); | ||
| 150 | 154 | ||
| 151 | // 绑定社交用户(新增) | 155 | // 绑定社交用户(新增) |
| 152 | socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM); | 156 | socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM); |
| 153 | - | ||
| 154 | - // 缓存登录用户到 Redis 中,返回 sessionId 编号 | ||
| 155 | - return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); | 157 | + return sessionId; |
| 156 | } | 158 | } |
| 157 | 159 | ||
| 158 | @Override | 160 | @Override |
yudao-user-server/src/main/resources/static/social-login.html
| @@ -13,14 +13,16 @@ | @@ -13,14 +13,16 @@ | ||
| 13 | </div> | 13 | </div> |
| 14 | </body> | 14 | </body> |
| 15 | <script> | 15 | <script> |
| 16 | - let server = 'http://127.0.0.1:28080'; | 16 | + // let server = 'http://127.0.0.1:28080'; |
| 17 | + let server = 'http://192.168.1.2:28080'; | ||
| 18 | + | ||
| 17 | 19 | ||
| 18 | // 微信公众号 | 20 | // 微信公众号 |
| 19 | $( "#wx_pub").on( "click", function() { | 21 | $( "#wx_pub").on( "click", function() { |
| 20 | // 获得授权链接 | 22 | // 获得授权链接 |
| 21 | $.ajax({ | 23 | $.ajax({ |
| 22 | url: server + "/api/social-auth-redirect?type=31&redirectUri=" + | 24 | url: server + "/api/social-auth-redirect?type=31&redirectUri=" + |
| 23 | - encodeURIComponent(server + '/api/social-login-get'), | 25 | + encodeURIComponent(server + '/static/social-login2.html'), //重定向地址 |
| 24 | method: 'GET', | 26 | method: 'GET', |
| 25 | success: function( result ) { | 27 | success: function( result ) { |
| 26 | if (result.code !== 0) { | 28 | if (result.code !== 0) { |
yudao-user-server/src/main/resources/static/social-login2.html
0 → 100644
| 1 | +<!DOCTYPE html> | ||
| 2 | +<html> | ||
| 3 | +<head> | ||
| 4 | + <meta charset="UTF-8"> | ||
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/> | ||
| 6 | + <title>社交登陆测试页</title> | ||
| 7 | + <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script> | ||
| 8 | +</head> | ||
| 9 | +<body> | ||
| 10 | +<div>点击如下按钮,授权登录</div> | ||
| 11 | +<div> | ||
| 12 | + 手机号<input id="mobile" value="15601691300"><br> | ||
| 13 | + 手机验证码<input id="smsCode"> | ||
| 14 | + <button id="send_sms_code">发送手机验证码</button> | ||
| 15 | + <br> | ||
| 16 | + <button id="wx_pub">微信公众号授权登录</button> | ||
| 17 | +</div> | ||
| 18 | +</body> | ||
| 19 | +<script> | ||
| 20 | + // let server = 'http://127.0.0.1:28080'; | ||
| 21 | + let server = 'http://192.168.1.2:28080'; | ||
| 22 | + | ||
| 23 | + let type = 31; //登录类型 微信公众号 | ||
| 24 | + | ||
| 25 | + // 微信公众号 | ||
| 26 | + $("#wx_pub").on("click", function () { | ||
| 27 | + let code = getUrlParam("code"); // 访问授权连接后,会回调本页面地址,参数在本页面url后面 | ||
| 28 | + let state = getUrlParam("state"); | ||
| 29 | + console.log("获取code: " + code + ", state: " + state) | ||
| 30 | + | ||
| 31 | + let data = { | ||
| 32 | + 'mobile': $('#mobile').val(), | ||
| 33 | + 'smsCode': $('#smsCode').val(), | ||
| 34 | + 'code': code, | ||
| 35 | + 'state': state, | ||
| 36 | + 'type': type | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + // 调用授权登录接口 | ||
| 40 | + $.ajax({ | ||
| 41 | + url: server + "/api/social-login2", | ||
| 42 | + method: 'POST', | ||
| 43 | + data: JSON.stringify(data), | ||
| 44 | + contentType: "application/json;charset=utf-8", | ||
| 45 | + dataType: "json", | ||
| 46 | + success: function( result ) { | ||
| 47 | + if (result.code !== 0) { | ||
| 48 | + alert('调用授权登录接口失败,原因:' + result.msg) | ||
| 49 | + return; | ||
| 50 | + } | ||
| 51 | + alert("授权登录成功, token: "+result.data.token) | ||
| 52 | + } | ||
| 53 | + }) | ||
| 54 | + }); | ||
| 55 | + | ||
| 56 | + // 发送手机验证码 | ||
| 57 | + $("#send_sms_code").on("click", function () { | ||
| 58 | + let data = { | ||
| 59 | + 'mobile': $('#mobile').val(), | ||
| 60 | + 'scene': 1 // 手机号登陆 类型 | ||
| 61 | + } | ||
| 62 | + $.ajax({ | ||
| 63 | + url: server + "/api/send-sms-code", | ||
| 64 | + method: 'POST', | ||
| 65 | + data: JSON.stringify(data), | ||
| 66 | + contentType: "application/json;charset=utf-8", | ||
| 67 | + dataType: "json", | ||
| 68 | + success: function (result) { | ||
| 69 | + if (result.code !== 0) { | ||
| 70 | + alert('发送手机验证码失败,原因:' + result.msg) | ||
| 71 | + return; | ||
| 72 | + } | ||
| 73 | + alert("发送成功, 请查看日志"); | ||
| 74 | + } | ||
| 75 | + }) | ||
| 76 | + }) | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + //获取url中的参数 | ||
| 80 | + function getUrlParam(name) { | ||
| 81 | + var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象 | ||
| 82 | + var r = window.location.search.substr(1).match(reg); //匹配目标参数 | ||
| 83 | + if (r != null) return unescape(r[2]); | ||
| 84 | + return null; //返回参数值 | ||
| 85 | + } | ||
| 86 | +</script> | ||
| 87 | +</html> |