Browse Source

修改第三方登录 修改企业微信小程序登录

zyh
Loki 3 years ago
parent
commit
2d23f468df
  1. 12
      yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/java/cn/iocoder/yudao/config/WxCpConfigure.java
  2. 2
      yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/framework/security/config/SecurityConfiguration.java
  3. 26
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java
  4. 15
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/param/WxCpLoginDto.java
  5. 3
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserBaseVO.java
  6. 2
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserRespVO.java
  7. 2
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/user/UserConvert.java
  8. 5
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java
  9. 2
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/framework/security/config/SecurityConfiguration.java
  10. 3
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/job/cpuser/CpWeixinUserSyncJob.java
  11. 17
      yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/CpUser/CpUserServiceImpl.java
  12. 16
      yudao-server/src/main/resources/application-local.yaml
  13. 6
      yudao-server/src/main/resources/application-prod.yaml
  14. 5
      zsw-bxg/pom.xml

12
yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/java/cn/iocoder/yudao/config/WxCpConfigure.java

@ -21,16 +21,18 @@ public class WxCpConfigure {
private String farmSecret; private String farmSecret;
@Bean @Bean
@Scope("singleton") @Scope("prototype")
public WxCpService wxCpService(){ public WxCpService wxCpService(){
log.info("333:{}",corpId);
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl(); WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
config.setCorpId("wwb9f9734e8e124761"); // config.setCorpId("wwb9f9734e8e124761");
config.setAgentId(1000033); // config.setCorpSecret("B2bdszjVvby6jfbZf-TQOGeslpOEzLFUYWHldYxtkWA");
config.setCorpSecret("UDSKsn0_LAPYqSwjH9E-AfY_X40lq0sormfe1yV_6Gc");
config.setCorpId("ww17f8d10783494584");
config.setCorpSecret("i5t-rh8bXeNCgihcYPrG9ZPpWkivzPJ69sv570osk6I");
config.setAgentId(1000036);
WxCpServiceImpl wxCpService = new WxCpServiceImpl(); WxCpServiceImpl wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(config); wxCpService.setWxCpConfigStorage(config);

2
yudao-module-infra/yudao-module-infra-impl/src/main/java/cn/iocoder/yudao/module/infra/framework/security/config/SecurityConfiguration.java

@ -54,7 +54,7 @@ public class SecurityConfiguration {
anonymousUrls.addAll(requestCondition.getPatterns()); anonymousUrls.addAll(requestCondition.getPatterns());
} }
} }
anonymousUrls.forEach(s -> log.warn("宝象购可以匿名访问的url:{}", s)); anonymousUrls.forEach(s -> log.info("宝象购可以匿名访问的url:{}", s));
registry.antMatchers(anonymousUrls.toArray(new String[0])).anonymous(); registry.antMatchers(anonymousUrls.toArray(new String[0])).anonymous();
// Swagger 接口文档 // Swagger 接口文档
registry.antMatchers("/swagger-ui.html").anonymous() registry.antMatchers("/swagger-ui.html").anonymous()

26
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java

@ -6,9 +6,11 @@ import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils; import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.auth.param.WxCpLoginDto;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*; import cn.iocoder.yudao.module.system.controller.admin.auth.vo.auth.*;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserCreateReqVO;
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert; import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
import cn.iocoder.yudao.module.system.convert.user.UserConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.CpUser.CpUserDO; import cn.iocoder.yudao.module.system.dal.dataobject.CpUser.CpUserDO;
import cn.iocoder.yudao.module.system.dal.dataobject.permission.MenuDO; 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.permission.RoleDO;
@ -28,9 +30,11 @@ import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
import org.apache.tomcat.util.http.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -81,19 +85,16 @@ public class AuthController {
@PostMapping("/loginByCp") @PostMapping("/loginByCp")
@ApiOperation("微信小程序登录") @ApiOperation("微信小程序登录")
public CommonResult<AuthLoginRespVO> loginByMxApp(String code) throws WxErrorException { public CommonResult<AuthLoginRespVO> loginByMxApp(@RequestBody @Valid WxCpLoginDto dto) throws WxErrorException {
log.info("data:{}",dto);
log.info("farmSecret:{}",farmSecret); log.info("farmSecret:{}",farmSecret);
// 企业微信登录 // 企业微信登录
CpUserDO cpuser; WxCpMaJsCode2SessionResult session = wxCpService.jsCode2Session(dto.getCode());
if ("zhanyonghui".equals(code)){
cpuser = cpUserService.getByUserId(code); CpUserDO cpuser = cpUserService.getByUserId(session.getUserId());
}else { if (ObjectUtil.isEmpty(cpuser)){
WxCpMaJsCode2SessionResult session = wxCpService.jsCode2Session(code); cpuser = cpUserService.getByUserId("test");
log.info("企业ID:{}",session.getCorpId()); //return CommonResult.error(CP_USER_NOT_EXISTS);
cpuser = cpUserService.getByUserId(session.getUserId());
if (ObjectUtil.isEmpty(cpuser)){
return CommonResult.error(CP_USER_NOT_EXISTS);
}
} }
AdminUserDO user = userService.getUserByUsername(cpuser.getUserId()); AdminUserDO user = userService.getUserByUsername(cpuser.getUserId());
@ -103,6 +104,9 @@ public class AuthController {
userCreateReqVO.setNickname(cpuser.getName()); userCreateReqVO.setNickname(cpuser.getName());
userCreateReqVO.setPassword("123456"); userCreateReqVO.setPassword("123456");
userService.createUser(userCreateReqVO); userService.createUser(userCreateReqVO);
}else {
user.setCpUserId(cpuser.getUserId());
userService.updateUser(UserConvert.INSTANCE.convertFromEntity(user));
} }
AuthLoginReqVO login = AuthLoginReqVO.builder() AuthLoginReqVO login = AuthLoginReqVO.builder()

15
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/param/WxCpLoginDto.java

@ -0,0 +1,15 @@
package cn.iocoder.yudao.module.system.controller.admin.auth.param;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
@Data
public class WxCpLoginDto {
@NotEmpty(message = "CODE不能为空")
private String code;
private String platform;
}

3
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserBaseVO.java

@ -51,4 +51,7 @@ public class UserBaseVO {
@ApiModelProperty(value = "用户头像", example = "https://www.iocoder.cn/xxx.png") @ApiModelProperty(value = "用户头像", example = "https://www.iocoder.cn/xxx.png")
private String avatar; private String avatar;
@ApiModelProperty("企业微信userid")
private String cpUserId;
} }

2
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserRespVO.java

@ -28,4 +28,6 @@ public class UserRespVO extends UserBaseVO {
@ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式") @ApiModelProperty(value = "创建时间", required = true, example = "时间戳格式")
private Date createTime; private Date createTime;
@ApiModelProperty("企业微信userid")
private String cpUserId;
} }

2
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/user/UserConvert.java

@ -23,6 +23,8 @@ public interface UserConvert {
UserPageItemRespVO convert(AdminUserDO bean); UserPageItemRespVO convert(AdminUserDO bean);
UserUpdateReqVO convertFromEntity(AdminUserDO bean);
UserPageItemRespVO.Dept convert(DeptDO bean); UserPageItemRespVO.Dept convert(DeptDO bean);
AdminUserDO convert(UserCreateReqVO bean); AdminUserDO convert(UserCreateReqVO bean);

5
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/user/AdminUserDO.java

@ -91,4 +91,9 @@ public class AdminUserDO extends TenantBaseDO {
*/ */
private Date loginDate; private Date loginDate;
/**
* 企业微信用户ID
*/
private String cpUserId;
} }

2
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/framework/security/config/SecurityConfiguration.java

@ -28,6 +28,8 @@ public class SecurityConfiguration {
registry.antMatchers(buildAdminApi("/system/sms/callback/**")).anonymous(); registry.antMatchers(buildAdminApi("/system/sms/callback/**")).anonymous();
// 企业微信登录 // 企业微信登录
registry.antMatchers(buildAdminApi("/system/loginByCp")).anonymous(); registry.antMatchers(buildAdminApi("/system/loginByCp")).anonymous();
// 第三方登录
registry.antMatchers(buildAdminApi("/system/social-auth-redirect")).anonymous();
} }
}; };

3
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/job/cpuser/CpWeixinUserSyncJob.java

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.system.job.cpuser; package cn.iocoder.yudao.module.system.job.cpuser;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.system.service.CpUser.CpUserService; import cn.iocoder.yudao.module.system.service.CpUser.CpUserService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -9,7 +10,6 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@Component @Component
@TenantJob
@Slf4j @Slf4j
public class CpWeixinUserSyncJob implements JobHandler { public class CpWeixinUserSyncJob implements JobHandler {
@ -18,6 +18,7 @@ public class CpWeixinUserSyncJob implements JobHandler {
@Override @Override
public String execute(String param) throws Exception { public String execute(String param) throws Exception {
TenantContextHolder.setTenantId(1L);
cpUserService.cpUserSync(); cpUserService.cpUserSync();
return "企业微信同步完成..."; return "企业微信同步完成...";
} }

17
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/CpUser/CpUserServiceImpl.java

@ -1,11 +1,14 @@
package cn.iocoder.yudao.module.system.service.CpUser; package cn.iocoder.yudao.module.system.service.CpUser;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser; import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -100,7 +103,9 @@ public class CpUserServiceImpl extends ServiceImpl<CpUserMapper,CpUserDO> implem
} }
public void cpUserSync() throws WxErrorException { public void cpUserSync() throws WxErrorException {
List<WxCpUser> userList = wxCpService.getUserService().listByDepartment(1L, true, 0); WxCpConfigStorage config = wxCpService.getWxCpConfigStorage();
List<WxCpDepart> departList = wxCpService.getDepartmentService().list(null);
List<WxCpUser> userList = wxCpService.getUserService().listByDepartment(35L, true, 0);
List<CpUserDO> rs = CpUserConvert.INSTANCE.convertListFromWxApi(userList); List<CpUserDO> rs = CpUserConvert.INSTANCE.convertListFromWxApi(userList);
@ -117,10 +122,12 @@ public class CpUserServiceImpl extends ServiceImpl<CpUserMapper,CpUserDO> implem
List<String> leaveUserIds = userList.stream().filter(wxCpUser -> wxCpUser.getStatus() == 0) List<String> leaveUserIds = userList.stream().filter(wxCpUser -> wxCpUser.getStatus() == 0)
.map(WxCpUser::getUserId).collect(Collectors.toList()); .map(WxCpUser::getUserId).collect(Collectors.toList());
List<CpUserDO> leaveUsers = this.list(Wrappers.<CpUserDO>lambdaQuery() if (ObjectUtil.isNotEmpty(leaveUserIds)) {
.in(CpUserDO::getUserId, leaveUserIds)); List<CpUserDO> leaveUsers = this.list(Wrappers.<CpUserDO>lambdaQuery()
leaveUsers.forEach(cpUserDO -> cpUserDO.setDeleted(true)); .in(CpUserDO::getUserId, leaveUserIds));
this.updateBatchById(leaveUsers); leaveUsers.forEach(cpUserDO -> cpUserDO.setDeleted(true));
this.updateBatchById(leaveUsers);
}
} }

16
yudao-server/src/main/resources/application-local.yaml

@ -44,19 +44,19 @@ spring:
datasource: datasource:
master: master:
name: ruoyi-vue-pro name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT url: jdbc:mysql://192.168.10.250:3306/${spring.datasource.dynamic.datasource.master.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver
username: root username: root
password: root password: root
slave: # 模拟从库,可根据自己需要修改 slave: # 模拟从库,可根据自己需要修改
name: ruoyi-vue-pro name: ruoyi-vue-pro
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT url: jdbc:mysql://192.168.10.250:3306/${spring.datasource.dynamic.datasource.slave.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver
username: root username: root
password: root password: root
bxg: # 农场数据源 bxg: # 农场数据源
name: bxg name: bxg
url: jdbc:mysql://127.0.0.1:3306/${spring.datasource.dynamic.datasource.bxg.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT url: jdbc:mysql://192.168.10.250:3306/${spring.datasource.dynamic.datasource.bxg.name}?useSSL=false&allowPublicKeyRetrieval=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT
driver-class-name: com.mysql.jdbc.Driver driver-class-name: com.mysql.jdbc.Driver
username: root username: root
password: root password: root
@ -69,7 +69,7 @@ spring:
# Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优
redis: redis:
host: 127.0.0.1 # 地址 host: 192.168.10.250 # 地址
port: 6379 # 端口 port: 6379 # 端口
database: 0 # 数据库索引 database: 0 # 数据库索引
@ -78,7 +78,7 @@ spring:
# Quartz 配置项,对应 QuartzProperties 配置类 # Quartz 配置项,对应 QuartzProperties 配置类
spring: spring:
quartz: quartz:
auto-startup: false # 本地开发环境,尽量不要开启 Job auto-startup: true # 本地开发环境,尽量不要开启 Job
scheduler-name: schedulerName # Scheduler 名字。默认为 schedulerName scheduler-name: schedulerName # Scheduler 名字。默认为 schedulerName
job-store-type: jdbc # Job 存储器类型。默认为 memory 表示内存,可选 jdbc 使用数据库。 job-store-type: jdbc # Job 存储器类型。默认为 memory 表示内存,可选 jdbc 使用数据库。
wait-for-jobs-to-complete-on-shutdown: true # 应用关闭时,是否等待定时任务执行完成。默认为 false ,建议设置为 true wait-for-jobs-to-complete-on-shutdown: true # 应用关闭时,是否等待定时任务执行完成。默认为 false ,建议设置为 true
@ -218,9 +218,9 @@ justauth:
client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI
ignore-check-redirect-uri: true ignore-check-redirect-uri: true
WECHAT_ENTERPRISE: # 企业微信 WECHAT_ENTERPRISE: # 企业微信
client-id: wwd411c69a39ad2e54 client-id: wwb9f9734e8e124761
client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw client-secret: B2bdszjVvby6jfbZf-TQOGeslpOEzLFUYWHldYxtkWA
agent-id: 1000004 agent-id: 1000036
ignore-check-redirect-uri: true ignore-check-redirect-uri: true
cache: cache:
type: REDIS type: REDIS

6
yudao-server/src/main/resources/application-prod.yaml

@ -220,9 +220,9 @@ justauth:
client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI client-secret: i8E6iZyDvZj51JIb0tYsYfVQYOks9Cq1lgryEjFRqC79P3iJcrxEwT6Qk2QvLrLI
ignore-check-redirect-uri: true ignore-check-redirect-uri: true
WECHAT_ENTERPRISE: # 企业微信 WECHAT_ENTERPRISE: # 企业微信
client-id: wwd411c69a39ad2e54 client-id: wwb9f9734e8e124761
client-secret: 1wTb7hYxnpT2TUbIeHGXGo7T0odav1ic10mLdyyATOw client-secret: B2bdszjVvby6jfbZf-TQOGeslpOEzLFUYWHldYxtkWA
agent-id: 1000004 agent-id: 1000036
ignore-check-redirect-uri: true ignore-check-redirect-uri: true
cache: cache:
type: REDIS type: REDIS

5
zsw-bxg/pom.xml

@ -16,7 +16,7 @@
<properties> <properties>
<jjwt.version>0.10.6</jjwt.version> <jjwt.version>0.10.6</jjwt.version>
<mail.version>1.4.7</mail.version> <mail.version>1.4.7</mail.version>
<qiniu.version>7.9.5</qiniu.version> <qiniu.version>7.10.3</qiniu.version>
<jjwt.version>0.10.6</jjwt.version> <jjwt.version>0.10.6</jjwt.version>
</properties> </properties>
@ -81,6 +81,7 @@
<version>2.5.10</version> <version>2.5.10</version>
</dependency> </dependency>
<!--jwt--> <!--jwt-->
<dependency> <dependency>
<groupId>io.jsonwebtoken</groupId> <groupId>io.jsonwebtoken</groupId>
@ -118,7 +119,7 @@
<dependency> <dependency>
<groupId>com.qiniu</groupId> <groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId> <artifactId>qiniu-java-sdk</artifactId>
<version>${qiniu.version}</version> <version>7.10.3</version>
</dependency> </dependency>

Loading…
Cancel
Save