解决冲突

This commit is contained in:
宋天
2021-12-24 23:32:00 +08:00
223 changed files with 6570 additions and 1091 deletions

View File

@ -1,9 +0,0 @@
package cn.iocoder.yudao.userserver.framework.async.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
@Configuration
@EnableAsync
public class AsyncConfiguration {
}

View File

@ -1,4 +0,0 @@
/**
* 异步执行,基于 Spring @Async 实现
*/
package cn.iocoder.yudao.userserver.framework.async;

View File

@ -38,8 +38,8 @@ public class SysUserProfileController {
@PutMapping("/update-nickname")
@ApiOperation("修改用户昵称")
@PreAuthenticated
public CommonResult<Boolean> updateNickname(@RequestParam("nickName") String nickName) {
userService.updateNickname(getLoginUserId(), nickName);
public CommonResult<Boolean> updateNickname(@RequestParam("nickname") String nickname) {
userService.updateNickname(getLoginUserId(), nickname);
return success(true);
}

View File

@ -51,9 +51,9 @@ public interface MbrUserService {
/**
* 修改用户昵称
* @param userId 用户id
* @param nickName 用户新昵称
* @param nickname 用户新昵称
*/
void updateNickname(Long userId, String nickName);
void updateNickname(Long userId, String nickname);
/**
* 修改用户头像

View File

@ -96,15 +96,15 @@ public class MbrUserServiceImpl implements MbrUserService {
}
@Override
public void updateNickname(Long userId, String nickName) {
public void updateNickname(Long userId, String nickname) {
MbrUserDO user = this.checkUserExists(userId);
// 仅当新昵称不等于旧昵称时进行修改
if (nickName.equals(user.getNickname())){
if (nickname.equals(user.getNickname())){
return;
}
MbrUserDO userDO = new MbrUserDO();
userDO.setId(user.getId());
userDO.setNickname(nickName);
userDO.setNickname(nickname);
userMapper.updateById(userDO);
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.userserver.modules.system.controller.auth;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
@ -38,7 +38,7 @@ public class SysAuthController {
@Resource
private SysSmsCodeService smsCodeService;
@Resource
private SysSocialService socialService;
private SysSocialCoreService socialService;
@PostMapping("/login")
@ApiOperation("使用手机 + 密码登录")
@ -107,19 +107,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<MbrAuthSocialLoginReqVO> 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 授权码")
@ -128,10 +115,9 @@ public class SysAuthController {
return success(SysAuthLoginRespVO.builder().token(token).build());
}
// TODO @timfruit社交登陆时使用手机验证码来验证哈。这块我当时没设计好改改嘿嘿。
@PostMapping("/social-login2")
@ApiOperation("社交登录,使用 code 授权码 + 账号密")
@ApiOperation("社交登录,使用 手机号 + 手机验证")
public CommonResult<SysAuthLoginRespVO> socialLogin2(@RequestBody @Valid MbrAuthSocialLogin2ReqVO reqVO) {
String token = authService.socialLogin2(reqVO, getClientIP(), getUserAgent());
return success(SysAuthLoginRespVO.builder().token(token).build());

View File

@ -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;
}

View File

@ -46,7 +46,7 @@ public interface SysAuthService extends SecurityAuthFrameworkService {
String socialLogin(@Valid MbrAuthSocialLoginReqVO reqVO, String userIp, String userAgent);
/**
* 社交登录,使用 code 授权码 + 账号密
* 社交登录,使用 手机号 + 手机验证
*
* @param reqVO 登录信息
* @param userIp 用户 IP

View File

@ -9,7 +9,7 @@ import cn.iocoder.yudao.coreservice.modules.system.enums.logger.SysLoginResultEn
import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService;
import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService;
import cn.iocoder.yudao.coreservice.modules.system.service.logger.dto.SysLoginLogCreateReqDTO;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
@ -40,7 +40,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
@ -57,6 +56,8 @@ import static cn.iocoder.yudao.userserver.modules.system.enums.SysErrorCodeConst
@Slf4j
public class SysAuthServiceImpl implements SysAuthService {
private static final UserTypeEnum USER_TYPE_ENUM = UserTypeEnum.MEMBER;
@Resource
@Lazy // 延迟加载,因为存在相互依赖的问题
private AuthenticationManager authenticationManager;
@ -70,7 +71,8 @@ public class SysAuthServiceImpl implements SysAuthService {
@Resource
private SysUserSessionCoreService userSessionCoreService;
@Resource
private SysSocialService socialService;
private SysSocialCoreService socialService;
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
@ -127,7 +129,7 @@ public class SysAuthServiceImpl implements SysAuthService {
// 如果未绑定 SysSocialUserDO 用户,则无法自动登录,进行报错
String unionId = socialService.getAuthUserUnionId(authUser);
List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, userTypeEnum);
List<SysSocialUserDO> socialUsers = socialService.getAllSocialUserList(reqVO.getType(), unionId, USER_TYPE_ENUM);
if (CollUtil.isEmpty(socialUsers)) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
}
@ -143,7 +145,7 @@ public class SysAuthServiceImpl implements SysAuthService {
LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user);
// 绑定社交用户(更新)
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum);
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
// 缓存登录用户到 Redis 中,返回 sessionId 编号
return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
@ -151,19 +153,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, userTypeEnum);
// 缓存登录用户到 Redis 中,返回 sessionId 编号
return userSessionCoreService.createUserSession(loginUser, userIp, userAgent);
socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, USER_TYPE_ENUM);
return sessionId;
}
@Override
@ -173,7 +177,7 @@ public class SysAuthServiceImpl implements SysAuthService {
org.springframework.util.Assert.notNull(authUser, "授权用户不为空");
// 绑定社交用户(新增)
socialService.bindSocialUser(userId, reqVO.getType(), authUser, userTypeEnum);
socialService.bindSocialUser(userId, reqVO.getType(), authUser, USER_TYPE_ENUM);
}
private LoginUser login0(String username, String password) {
@ -340,7 +344,7 @@ public class SysAuthServiceImpl implements SysAuthService {
reqDTO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType());
reqDTO.setTraceId(TracerUtils.getTraceId());
reqDTO.setUserId(userId);
reqDTO.setUserType(userTypeEnum.getValue());
reqDTO.setUserType(USER_TYPE_ENUM.getValue());
reqDTO.setUsername(username);
reqDTO.setUserAgent(ServletUtils.getUserAgent());
reqDTO.setUserIp(getClientIP());

View File

@ -63,6 +63,11 @@ spring:
--- #################### 定时任务相关配置 ####################
# Quartz 配置项,对应 QuartzProperties 配置类
spring:
quartz:
auto-startup: false # 无需使用 Quartz交给 yudao-admin-server 环境
--- #################### 配置中心相关配置 ####################
# Apollo 配置中心
@ -145,24 +150,16 @@ yudao:
justauth:
enabled: true
type: # TODO @timfruitGITEE、DINGTALK、WECHAT_ENTERPRISE 这个几个,对于用户端是不需要的哈,可以删除噢
GITEE: # Gitee
client-id: ee61f0374a4c6c404a8717094caa7a410d76950e45ff60348015830c519ba5c1
client-secret: 7c044a5671be3b051414db0cf2cec6ad702dd298d2416ba24ceaf608e6fa26f9
ignore-check-redirect-uri: true
DINGTALK: # 钉钉
client-id: dingvrnreaje3yqvzhxg
client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI
ignore-check-redirect-uri: true
WECHAT_ENTERPRISE: # 企业微信
client-id: wwd411c69a39ad2e54
client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw
agent-id: 1000004
ignore-check-redirect-uri: true
WECHAT_MP: # 微信公众平台 - H5 https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
type:
WECHAT_MP: # 微信公众平台 - 移动端 H5 https://www.yuque.com/docs/share/a795bef6-ee8a-494a-8dc4-2ef41927743b?#%20%E3%80%8A%E5%BE%AE%E4%BF%A1%E5%85%AC%E4%BC%97%E5%8F%B7%E6%B5%8B%E8%AF%95%E3%80%8B
client-id: wxa5a05b85ac361f96
client-secret: 247073c7cebb67f27f0e769195c2a57e
ignore-check-redirect-uri: true
WECHAT_MINI_PROGRAM: # 微信小程序 https://www.yuque.com/docs/share/88e3d30a-6830-45fc-8c25-dae485aef3aa?#%20%E3%80%8A%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%8E%88%E6%9D%83%E7%99%BB%E5%BD%95%E3%80%8B
client-id: wx44d047d87e6284d8
client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
ignore-check-redirect-uri: true
ignore-check-state: true
cache:
type: REDIS
prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE::

View File

@ -63,6 +63,11 @@ spring:
--- #################### 定时任务相关配置 ####################
# Quartz 配置项,对应 QuartzProperties 配置类
spring:
quartz:
auto-startup: false # 无需使用 Quartz交给 yudao-admin-server 环境
--- #################### 配置中心相关配置 ####################
# Apollo 配置中心
@ -165,17 +170,13 @@ justauth:
# client-id: wx5b23ba7a5589ecbb # TODO 芋艿:自己的测试,后续可以删除
# client-secret: 2a7b3b20c537e52e74afd395eb85f61f
ignore-check-redirect-uri: true
extend:
enum-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthExtendSource
config:
WECHAT_MINI_PROGRAM: # 微信小程序 https://www.yuque.com/docs/share/88e3d30a-6830-45fc-8c25-dae485aef3aa?#%20%E3%80%8A%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%8E%88%E6%9D%83%E7%99%BB%E5%BD%95%E3%80%8B
request-class: cn.iocoder.yudao.coreservice.modules.system.compent.justauth.AuthWeChatMiniProgramRequest
client-id: wx44d047d87e6284d8
client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
# client-id: wx63c280fe3248a3e7 # TODO 芋艿:自己的测试,后续可以删除
# client-secret: 6f270509224a7ae1296bbf1c8cb97aed
ignore-check-redirect-uri: true
ignore-check-state: true
WECHAT_MINI_PROGRAM: # 微信小程序 https://www.yuque.com/docs/share/88e3d30a-6830-45fc-8c25-dae485aef3aa?#%20%E3%80%8A%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%8E%88%E6%9D%83%E7%99%BB%E5%BD%95%E3%80%8B
client-id: wx44d047d87e6284d8
client-secret: 21c3b7a8a51ee1b8f5cf875848ed4466
# client-id: wx63c280fe3248a3e7 # TODO 芋艿:自己的测试,后续可以删除
# client-secret: 6f270509224a7ae1296bbf1c8cb97aed
ignore-check-redirect-uri: true
ignore-check-state: true
cache:
type: REDIS
prefix: 'social_auth_state:' # 缓存前缀,目前只对 Redis 缓存生效,默认 JUSTAUTH::STATE::

View File

@ -0,0 +1,13 @@
## 微信公众号
参考文章https://www.yuque.com/docs/share/0e2966dd-89f8-4b69-980d-b876168725df
① 访问 social-login.html 选择【微信公众号】
② 微信公众号授权完成后,跳转回 social-login2.html输入手机号 + 密码,进行绑定
## 微信小程序
参考文章https://www.yuque.com/docs/share/88e3d30a-6830-45fc-8c25-dae485aef3aa
① 暂时使用 mini-program-test 项目

View File

@ -13,14 +13,16 @@
</div>
</body>
<script>
let server = 'http://127.0.0.1:28080';
// let server = 'http://127.0.0.1:28080';
let server = 'http://192.168.1.2:28080';
// 微信公众号
$( "#wx_pub").on( "click", function() {
// 获得授权链接
$.ajax({
url: server + "/api/social-auth-redirect?type=31&redirectUri=" +
encodeURIComponent(server + '/api/social-login-get'),
encodeURIComponent(server + '/static/social-login2.html'), //重定向地址
method: 'GET',
success: function( result ) {
if (result.code !== 0) {

View File

@ -0,0 +1,87 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
<title>社交登陆测试页</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
<div>点击如下按钮,授权登录</div>
<div>
手机号<input id="mobile" value="15601691300"><br>
手机验证码<input id="smsCode">
<button id="send_sms_code">发送手机验证码</button>
<br>
<button id="wx_pub">微信公众号授权登录</button>
</div>
</body>
<script>
// let server = 'http://127.0.0.1:28080';
let server = 'http://192.168.1.2:28080';
let type = 31; //登录类型 微信公众号
// 微信公众号
$("#wx_pub").on("click", function () {
let code = getUrlParam("code"); // 访问授权连接后会回调本页面地址参数在本页面url后面
let state = getUrlParam("state");
console.log("获取code: " + code + ", state: " + state)
let data = {
'mobile': $('#mobile').val(),
'smsCode': $('#smsCode').val(),
'code': code,
'state': state,
'type': type
}
// 调用授权登录接口
$.ajax({
url: server + "/api/social-login2",
method: 'POST',
data: JSON.stringify(data),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function( result ) {
if (result.code !== 0) {
alert('调用授权登录接口失败,原因:' + result.msg)
return;
}
alert("授权登录成功, token: "+result.data.token)
}
})
});
// 发送手机验证码
$("#send_sms_code").on("click", function () {
let data = {
'mobile': $('#mobile').val(),
'scene': 1 // 手机号登陆 类型
}
$.ajax({
url: server + "/api/send-sms-code",
method: 'POST',
data: JSON.stringify(data),
contentType: "application/json;charset=utf-8",
dataType: "json",
success: function (result) {
if (result.code !== 0) {
alert('发送手机验证码失败,原因:' + result.msg)
return;
}
alert("发送成功, 请查看日志");
}
})
})
//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]);
return null; //返回参数值
}
</script>
</html>

View File

@ -3,7 +3,7 @@ package cn.iocoder.yudao.userserver.modules.system.service;
import cn.iocoder.yudao.coreservice.modules.member.dal.dataobject.user.MbrUserDO;
import cn.iocoder.yudao.coreservice.modules.system.service.auth.SysUserSessionCoreService;
import cn.iocoder.yudao.coreservice.modules.system.service.logger.SysLoginLogCoreService;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialService;
import cn.iocoder.yudao.coreservice.modules.system.service.social.SysSocialCoreService;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils;
import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
@ -53,7 +53,7 @@ public class SysAuthServiceTest extends BaseDbAndRedisUnitTest {
@MockBean
private SysUserSessionCoreService userSessionCoreService;
@MockBean
private SysSocialService socialService;
private SysSocialCoreService socialService;
@Resource
private StringRedisTemplate stringRedisTemplate;
@MockBean

View File

@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS "mbr_user" (
"updater" varchar(64) NULL DEFAULT '' COMMENT '更新者',
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
"deleted" bit(1) NOT NULL DEFAULT '0' COMMENT '是否删除',
"tenant_id" bigint not null default '0',
PRIMARY KEY ("id")
) COMMENT '会员表';