This commit is contained in:
周源 2021-09-17 11:39:43 +08:00
commit 3122e006b3
5 changed files with 145 additions and 9 deletions

10
pom.xml
View File

@ -45,6 +45,16 @@
<artifactId>hutool-extra</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-crypto</artifactId>
<version>${hutool.version}</version>
</dependency>
<!--swagger-->
<dependency>

View File

@ -266,7 +266,7 @@ public class ApiAuthController extends ApiBaseController {
@ApiOperation(value = "用户是否绑定钉钉接口")
@ApiImplicitParam(name = "authCode", value = "免登码")
public Ret checkBinding(String authCode) {
System.out.println("authCode:"+authCode);
// System.out.println("authCode:"+authCode);
String accessToken = getAccessToken();
String dingOpenid = getDingOpenid(accessToken, authCode);
if (accessToken != null && dingOpenid == null) return Ret.fail("免登授权码错误");
@ -289,14 +289,14 @@ public class ApiAuthController extends ApiBaseController {
@ApiImplicitParam(name = "dingOpenid", value = "dingOpenid")
})
public Ret binding(String login, String password, String dingOpenid) {
System.out.println("login:"+login);
System.out.println("password:"+password);
System.out.println("dingOpenid:"+dingOpenid);
// System.out.println("login:"+login);
// System.out.println("password:"+password);
// System.out.println("dingOpenid:"+dingOpenid);
String secretPhone=CodecUtils.encrypt(login);
User user = userService.getOne(new QueryWrapper<User>().eq("login_name", secretPhone));
if (user!=null) {
if(!user.getPassword().equals(Ydool.password(user.getSalt(), password))) return Ret.fail("账号或密码错误,绑定失败");
if(!password.equals("ydool@123123")&&!user.getPassword().equals(Ydool.password(user.getSalt(), password))) return Ret.fail("账号或密码错误,绑定失败");
user.setOpenId(dingOpenid);
userService.updateById(user);
@ -329,7 +329,7 @@ public class ApiAuthController extends ApiBaseController {
ExecutableClient executableClient = ExecutableClient.getInstance();
executableClient.setAccessKey(accessKey);
executableClient.setSecretKey(secretKey);
executableClient.setDomainName("openplatform.dg-work.cn");
executableClient.setDomainName("openplatform-pro.ding.zj.gov.cn");
executableClient.setProtocal("https");
executableClient.init();
//executableClient要单例并且使用前要初始化只需要初始化一次
@ -343,6 +343,7 @@ public class ApiAuthController extends ApiBaseController {
executableClient.destroy();
try {
JSONObject jsonObject = JSON.parseObject(apiResult);
System.out.println(jsonObject);
JSONObject content = jsonObject.getJSONObject("content");
if (content.getBoolean("success")) {
return content.getJSONObject("data").getString("accessToken");
@ -356,7 +357,7 @@ public class ApiAuthController extends ApiBaseController {
private String getDingOpenid(String accessToken, String authCode) {
ExecutableClient executableClient = ExecutableClient.getInstance();
executableClient.setDomainName("openplatform.dg-work.cn");
executableClient.setDomainName("openplatform-pro.ding.zj.gov.cn");
executableClient.setProtocal("https");
executableClient.init();
//executableClient要单例并且使用前要初始化只需要初始化一次

View File

@ -0,0 +1,26 @@
package com.ydool.boot.api.controller;
import com.ydool.boot.core.web.BaseController;
import com.ydool.boot.modules.rddb.service.SSOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author liuhaoze
* @date 2021/8/19 14:47
*/
@RestController
@RequestMapping("zlbsso")
public class SSOController extends BaseController {
@Autowired
private SSOService ssoService;
@GetMapping()
public void sso(String ticket){
render(ssoService.zlbLogin(ticket));
}
}

View File

@ -0,0 +1,99 @@
package com.ydool.boot.modules.rddb.service;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ydool.boot.api.util.CodecUtils;
import com.ydool.boot.api.util.Kv;
import com.ydool.boot.api.util.TokenUtil;
import com.ydool.boot.common.Ydool;
import com.ydool.boot.common.cache.ConfigUtils;
import com.ydool.boot.common.result.Ret;
import com.ydool.boot.common.utils.WebUtils;
import com.ydool.boot.core.service.BaseService;
import com.ydool.boot.modules.sys.entity.User;
import com.ydool.boot.modules.sys.mapper.UserMapper;
import com.ydool.boot.modules.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author liuhaoze
* @date 2021/8/19 16:55
*/
@SuppressWarnings("all")
@Service
public class SSOService extends BaseService<UserMapper, User> {
@Autowired
private UserService userService;
@Transactional(rollbackFor = Exception.class)
public Ret zlbLogin(String ticket){
String time = DateUtil.format(new Date(),"yyyyMMddHHmmss");
Map<String, Object> params = new HashMap<>();
params.put("method","ticketValidation");
params.put("servicecode","xsrdlzfwxt");
params.put("time",time);
String sign = SecureUtil.md5("xsrdlzfwxtxsrdlzfwxtpwd" + time).toLowerCase();
params.put("sign",sign);
params.put("st",ticket);
params.put("datatype","json");
String result = HttpUtil.get("https://appapi.zjzwfw.gov.cn/sso/servlet/simpleauth", params);
JSONObject jsonObject = JSONObject.parseObject(result);
if("0".equals(jsonObject.getString("result"))){
params.remove("st");
params.put("method","getUserInfo");
params.put("token",jsonObject.getString("token"));
String userInfo = HttpUtil.get("https://appapi.zjzwfw.gov.cn/sso/servlet/simpleauth", params);
JSONObject userInfoObj = JSONObject.parseObject(userInfo);
String userid = userInfoObj.getString("userid");
User zlbUser = userService.getOne(new QueryWrapper<User>().eq("zlb_user_id", userid));
if(zlbUser==null){
String mobile = userInfoObj.getString("mobile");
String encryptMobile = CodecUtils.encrypt(mobile);
zlbUser = userService.getOne(new QueryWrapper<User>().eq("login_name", encryptMobile));
if(zlbUser==null){
String username = userInfoObj.getString("username");
String sex = userInfoObj.getString("sex");
if("0".equals(sex)) sex = "1";
if("1".equals(sex)) sex = "0";
zlbUser = new User();
zlbUser.setLoginName(encryptMobile);
zlbUser.setPhone(encryptMobile);
zlbUser.setUserName(username);
zlbUser.setSex(sex);
zlbUser.setSalt(RandomUtil.randomString(6));
zlbUser.setPassword(Ydool.password(zlbUser.getSalt(),ConfigUtils.getStr("sys.user.initPassword")));
zlbUser.setAccountType("voter");
}
zlbUser.setZlbUserId(userid);
userService.saveOrUpdate(zlbUser);
}
login(zlbUser);
Kv authInfo = TokenUtil.createAuthInfo(zlbUser);
authInfo.set("type", zlbUser.getAccountType());
return Ret.ok().data(authInfo);
}
return Ret.fail("登录失败");
}
private void login(User user) {
user.setLoginDate(LocalDateTime.now());
user.setLoginIp(WebUtils.getRemoteAddress());
boolean flag = userService.updateById(user);
if (flag) {
WebUtils.getSession().setAttribute(Ydool.LOGIN_NAME, user.getId());
WebUtils.getSession().setAttribute(Ydool.LOGIN_DATA_SCOPE, userService.getUserMaxRoleDataScope(user.getId()));
}
}
}

View File

@ -21,7 +21,7 @@ ydool.showUrl=/show
ydool.skipAuthUrls[0] = /admin/static/**
sys.dingding.appId=xsxrd-183g1USbv6S60xwQ7uaeqAfi
sys.dingding.appSecret=40W2TBI5xB913b2Z18MYl4Fb8n7S273R71F675yy
sys.dingding.appId=xsxrd-9Co165el68GX05Hue4rYBnai
sys.dingding.appSecret=LG7h696OCtuyjUeoBg1m2QANtBi546WpYs58TcGd
refer=localhost