Browse Source

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/TokenValidatorService.java
master
DX 2 months ago
parent
commit
7a33c977fb
  1. 56
      yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
  2. 3
      yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java
  3. 2
      yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
  4. 3
      yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/TokenValidatorService.java
  5. 8
      yudao-server/src/main/resources/application-dev.yaml

56
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java

@ -1,10 +1,15 @@
package cn.iocoder.yudao.module.system.controller.admin.auth;
import cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.security.config.SecurityProperties;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
@ -12,11 +17,14 @@ import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
import cn.iocoder.yudao.module.system.service.auth.AdminAuthService;
import cn.iocoder.yudao.module.system.service.oauth2.OAuth2TokenService;
import cn.iocoder.yudao.module.system.service.permission.MenuService;
import cn.iocoder.yudao.module.system.service.permission.PermissionService;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
@ -28,6 +36,7 @@ import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -38,10 +47,13 @@ import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.AUTH_THIRD_LOGIN_NOT_BIND;
@Tag(name = "管理后台 - 认证")
@RestController
@ -70,6 +82,12 @@ public class AuthController {
private SocialUserService socialUserService;
@Resource
private StringRedisTemplate stringRedisTemplate;
@Resource
private OAuth2TokenService oAuth2TokenService;
@PostMapping("/login")
@PermitAll
@Operation(summary = "使用账号密码登录")
@ -99,7 +117,7 @@ public class AuthController {
}
@PermitAll
@PostMapping("/web_login")
@PostMapping("/web_code_login")
@Operation(summary = "后台调用小程序微信扫码登录")
public CommonResult<byte[]> webLogin(@RequestBody @Valid SocialWxQrcodeReqDTO reqVO) {
return success(socialClientService.getWxaQrcode(reqVO));
@ -198,4 +216,40 @@ public class AuthController {
return success(authService.socialLogin(reqVO));
}
@PostMapping("/social-login-openid")
@PermitAll
@Operation(summary = "社交快捷登录,使用 code 授权码", description = "适合未登录的用户,但是社交账号已绑定用户")
public void socialQuickLoginOpenId(@RequestBody AuthSocialLoginReqVO reqVO) {
OAuth2AccessTokenDO accessToken = oAuth2TokenService.getAccessToken(reqVO.getOpenid());
Long userId = accessToken.getUserId();
List<SocialUserDO> socialUserList = socialUserService.getSocialUserList(userId, UserTypeEnum.MEMBER.getValue());
//判断是否为对象空
if(!CollUtil.isEmpty(socialUserList)){
stringRedisTemplate.opsForValue().set(reqVO.getCode(), JsonUtils.toJsonString(socialUserList.get(0)),10, TimeUnit.MINUTES);
}
}
@PostMapping("/social-qr-login-openid")
@PermitAll
@Operation(summary = "社交快捷登录,使用 code 授权码", description = "适合未登录的用户,但是社交账号已绑定用户")
public CommonResult<AuthLoginRespVO> qrLoginCode(@RequestBody AuthSocialLoginReqVO reqVO) {
String json = stringRedisTemplate.opsForValue().get(reqVO.getCode());
//判断是否为对象空
if(StrUtil.isNotEmpty(json)){
SocialUserDO socialUserDO = JsonUtils.parseObject(json, SocialUserDO.class);
AuthSocialLoginReqVO authSocialLoginReqVO=new AuthSocialLoginReqVO();
authSocialLoginReqVO.setCode(socialUserDO.getCode());
authSocialLoginReqVO.setType(socialUserDO.getType());
authSocialLoginReqVO.setState(socialUserDO.getState());
authSocialLoginReqVO.setOpenid(socialUserDO.getOpenid());
return success(authService.socialLogin(authSocialLoginReqVO));
}
else{
return success(null);
}
}
}

3
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/vo/AuthSocialLoginReqVO.java

@ -34,4 +34,7 @@ public class AuthSocialLoginReqVO {
@Schema(description = "用户类型" )
private Integer userType;
@Schema(description = "社交 openid", example = "oz-Jdt0kd_jdhUxJHQdBJMlOFN7w")
private String openid;
}

2
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java

@ -174,7 +174,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
@Override
public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) {
// 使用 code 授权码,进行登录。然后,获得到绑定的用户编号
SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.ADMIN.getValue(), reqVO.getType(),
SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.MEMBER.getValue(), reqVO.getType(),
reqVO.getCode(), reqVO.getState());
if (socialUser == null || socialUser.getUserId() == null) {
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);

3
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/weixin/TokenValidatorService.java

@ -5,6 +5,7 @@ import org.springframework.stereotype.Service;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
@Service
public class TokenValidatorService {
public static boolean validateToken(String token, String signature, String timestamp, String nonce) {
@ -38,4 +39,4 @@ public class TokenValidatorService {
}
return null;
}
}
}

8
yudao-server/src/main/resources/application-dev.yaml

@ -145,16 +145,16 @@ logging:
wx: # 参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-mp-spring-boot-starter/README.md 文档
mp:
# 公众号配置(必填)
app-id: wx041349c6f39b268b
secret: 5abee519483bc9f8cb37ce280e814bd0
app-id: wx6d80755768234f3b
secret: c8180f2ab1b8454d403c7aa336782e21
# 存储配置,解决 AccessToken 的跨节点的共享
config-storage:
type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取
key-prefix: wx # Redis Key 的前缀
http-client-type: HttpClient # 采用 HttpClient 请求微信公众号平台
miniapp: # 小程序配置(必填),参见 https://github.com/Wechat-Group/WxJava/blob/develop/spring-boot-starters/wx-java-miniapp-spring-boot-starter/README.md 文档
appid: wx63c280fe3248a3e7
secret: 6f270509224a7ae1296bbf1c8cb97aed
appid: wx6d80755768234f3b
secret: c8180f2ab1b8454d403c7aa336782e21
config-storage:
type: RedisTemplate # 采用 RedisTemplate 操作 Redis,会自动从 Spring 中获取
key-prefix: wa # Redis Key 的前缀

Loading…
Cancel
Save