提交 74bbe35e 编写于 作者: sq3536's avatar sq3536

第三方接入

上级 c3dd4508
......@@ -15,12 +15,8 @@ import com.alibaba.nacos.client.identify.Base64;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.dingtalk.api.request.*;
import com.dingtalk.api.response.*;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
......@@ -102,11 +98,15 @@ public class UserDingtalkRegisterService {
private ISysOpenAccessService sysOpenAccessService;
public SysOpenAccess getOpenAccess(String id)
{
return getOpenAccess(id,true);
}
public SysOpenAccess getOpenAccess(String id,boolean throwEx)
{
final String accessid = StringUtils.isEmpty(id)?"dingtalk":id;
SysOpenAccess sysOpenAccess=sysOpenAccessService.getOne(Wrappers.<SysOpenAccess>lambdaQuery().eq(SysOpenAccess::getOpenType,"dingtalk").
and(wrapper -> wrapper.eq(SysOpenAccess::getAccessKey,accessid).or().eq(SysOpenAccess::getId,accessid)),false);
if(sysOpenAccess==null|| (sysOpenAccess.getDisabled()!=null && sysOpenAccess.getDisabled()==1))
if((sysOpenAccess==null|| (sysOpenAccess.getDisabled()!=null && sysOpenAccess.getDisabled()==1))&&throwEx)
throw new BadRequestAlertException("获取接入配置失败","UserDingtalkRegisterService","");
String accessToken = getAccessToken(sysOpenAccess.getAccessKey(),sysOpenAccess.getSecretKey());
......@@ -190,7 +190,10 @@ public class UserDingtalkRegisterService {
returnObj.put("openid", response.getUserInfo().getOpenid());
returnObj.put("nickname", response.getUserInfo().getNick());
SysUserAuth userAuth = sysUserAuthService.getOne(Wrappers.<SysUserAuth>lambdaQuery().eq(SysUserAuth::getIdentityType,"dingtalk").eq(SysUserAuth::getIdentifier, response.getUserInfo().getOpenid()),false);
returnObj.put("unionid", response.getUserInfo().getUnionid());
SysUserAuth userAuth = sysUserAuthService.getOne(Wrappers.<SysUserAuth>lambdaQuery().eq(SysUserAuth::getIdentityType,"dingtalk")
.and(wrapper -> wrapper.eq(SysUserAuth::getIdentifier, response.getUserInfo().getOpenid()).or().eq(SysUserAuth::getIdentifier, response.getUserInfo().getUnionid())
),false);
IBZUSER user = null;
// 该钉钉用户注册过账号,登录系统
......
package cn.ibizlab.core.uaa.extensions.service;
import cn.ibizlab.core.uaa.domain.SysOpenAccess;
import cn.ibizlab.core.uaa.domain.SysUserAuth;
import cn.ibizlab.core.uaa.service.ISysOpenAccessService;
import cn.ibizlab.core.uaa.service.ISysUserAuthService;
import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.errors.InternalServerErrorException;
import cn.ibizlab.util.helper.HttpUtils;
import cn.ibizlab.util.service.IBZUSERService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -26,50 +30,51 @@ public class UserQQRegisterService {
@Autowired
private ISysUserAuthService sysUserAuthService;
/**
* 注册
*
* @param ibzuser
*/
public void toRegister(IBZUSER ibzuser) {
// 创建ibzuser
boolean flag = ibzuserService.save(ibzuser);
if (!flag) {
throw new BadRequestAlertException("注册失败", "UserQQRegisterService", "");
}
}
/**
* 创建QQ用户授权信息
* @param ibzuser
*/
public void toCreateUserAuth(SysUserAuth ibzuser) {
// 创建用户授权信息
boolean flag = sysUserAuthService.create(ibzuser);
if (!flag) {
throw new BadRequestAlertException("保存用户授权信息失败", "UserQQRegisterService", "");
@Autowired
private ISysOpenAccessService sysOpenAccessService;
public SysOpenAccess getOpenAccess(String id)
{
return getOpenAccess(id,true);
}
public SysOpenAccess getOpenAccess(String id,boolean throwEx)
{
final String accessid = StringUtils.isEmpty(id)?"qq":id;
SysOpenAccess sysOpenAccess=sysOpenAccessService.getOne(Wrappers.<SysOpenAccess>lambdaQuery().eq(SysOpenAccess::getOpenType,"qq").
and(wrapper -> wrapper.eq(SysOpenAccess::getAccessKey,accessid).or().eq(SysOpenAccess::getId,accessid)),false);
if((sysOpenAccess==null|| (sysOpenAccess.getDisabled()!=null && sysOpenAccess.getDisabled()==1))&&throwEx)
throw new BadRequestAlertException("获取接入配置失败","UserQQRegisterService","");
return sysOpenAccess;
}
/**
* 通过code获取QQ用户信息
* qq服务端通过临时授权码code获取授权用户的个人信息
*
* @param code
* @param redirectUri
* @param appId
* @param appSecret
* @return
*/
public JSONObject requestQQUserByCode(String code, String redirectUri, String appId, String appSecret) {
public JSONObject getUserBySnsToken(String id,String requestAuthCode) {
JSONObject returnObj = null;
SysOpenAccess openAccess = getOpenAccess(id);
if (openAccess==null || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
throw new BadRequestAlertException("未找到配置", "UserWechatRegisterService", "");
try {
// 1.根据code获取access_token
String getAccessTokenUrl = "https://graph.qq.com/oauth2.0/token?" +
"grant_type=authorization_code" +
"&client_id=" + appId +
"&client_secret=" + appSecret +
"&code=" + code +
"&redirect_uri=" + redirectUri;
"&client_id=" + openAccess.getAccessKey() +
"&client_secret=" + openAccess.getSecretKey() +
"&code=" + requestAuthCode +
"&redirect_uri=" + openAccess.getRedirectUri();
String responserStr = HttpUtils.get(getAccessTokenUrl, null, null);
JSONObject responseObj = new JSONObject();
if (StringUtils.isEmpty(responserStr)) {
......@@ -89,6 +94,9 @@ public class UserQQRegisterService {
String access_token = responseObj.getString("access_token");
String refresh_token = responseObj.getString("refresh_token");
returnObj.put("access_token",access_token);
returnObj.put("refresh_token",refresh_token);
// 2.使用access_token来获取用户的OpenID
String openid = null;
String getOpenIdUrl = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
......@@ -100,26 +108,33 @@ public class UserQQRegisterService {
JSONObject responseObj2 = JSONObject.parseObject(responseStr2);
if (responseObj2.containsKey("openid")) {
openid = responseObj2.getString("openid");
returnObj.put("openid",openid);
SysUserAuth userAuth = sysUserAuthService.getOne(Wrappers.<SysUserAuth>lambdaQuery().eq(SysUserAuth::getIdentityType,"qq").eq(SysUserAuth::getIdentifier, openid),false);
IBZUSER user = null;
// 该qq用户注册过账号,登录系统
if (userAuth!=null) {
user = ibzuserService.getById(userAuth.getUserid());
if (user == null)
user = ibzuserService.getOne(Wrappers.<IBZUSER>lambdaQuery().eq(IBZUSER::getUserid,openid).or().eq(IBZUSER::getUsername,openid),false);
if(user!=null)
{
returnObj.put("username",user.getLoginname()+(StringUtils.isEmpty(user.getDomains())?"":("|"+user.getDomains())));
}
}
}else {
throw new BadRequestAlertException("QQ服务端获取openid失败!", "UserQQRegisterService", "");
}
}
// 3.使用access_token以及OpenID来访问和修改用户数据
String getQQUserInfoUrl = "https://graph.qq.com/user/get_user_info?" +
"access_token=" + access_token +
"&oauth_consumer_key=" + appId +
"&openid=" + openid;
returnObj = JSONObject.parseObject(HttpUtils.get(getQQUserInfoUrl, null, null));
if (StringUtils.isEmpty(returnObj)) {
throw new BadRequestAlertException("QQ服务端返回结果为空!", "UserQQRegisterService", "");
} else if (returnObj.getInteger("ret")!=0) {
throw new BadRequestAlertException("获取QQ用户信息失败!", "UserQQRegisterService", "");
} else {
returnObj.put("openid", openid);
}
} catch (UnsupportedEncodingException e) {
throw new BadRequestAlertException("连接QQ服务端失败!", "UserQQRegisterService", "");
} catch (Exception e) {
e.printStackTrace();
throw new InternalServerErrorException("获取user失败");
}
return returnObj;
......
package cn.ibizlab.core.uaa.extensions.service;
import cn.ibizlab.core.uaa.domain.SysOpenAccess;
import cn.ibizlab.core.uaa.domain.SysUserAuth;
import cn.ibizlab.core.uaa.service.ISysOpenAccessService;
import cn.ibizlab.core.uaa.service.ISysUserAuthService;
import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.errors.InternalServerErrorException;
import cn.ibizlab.util.helper.HttpUtils;
import cn.ibizlab.util.service.IBZUSERService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiSnsGetuserinfoBycodeRequest;
import com.dingtalk.api.response.OapiSnsGetuserinfoBycodeResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
/**
* 实体[IBZUSER] 微信用户注册接口实现
......@@ -26,92 +35,78 @@ public class UserWechatRegisterService {
@Autowired
private ISysUserAuthService sysUserAuthService;
/**
* 注册
*
* @param ibzuser
*/
public void toRegister(IBZUSER ibzuser) {
// 创建ibzuser
boolean flag = ibzuserService.save(ibzuser);
if (!flag) {
throw new BadRequestAlertException("注册失败", "UserWechatRegisterService", "");
}
}
@Autowired
private ISysOpenAccessService sysOpenAccessService;
/**
* 创建微信用户授权信息
* @param userAuth
*/
public void toCreateUserAuth(SysUserAuth userAuth) {
// 创建用户授权信息
boolean flag = sysUserAuthService.create(userAuth);
if (!flag) {
throw new BadRequestAlertException("保存用户授权信息失败", "UserWechatRegisterService", "");
public SysOpenAccess getOpenAccess(String id)
{
return getOpenAccess(id,true);
}
public SysOpenAccess getOpenAccess(String id,boolean throwEx)
{
final String accessid = StringUtils.isEmpty(id)?"wechat":id;
SysOpenAccess sysOpenAccess=sysOpenAccessService.getOne(Wrappers.<SysOpenAccess>lambdaQuery().eq(SysOpenAccess::getOpenType,"wechat").
and(wrapper -> wrapper.eq(SysOpenAccess::getAccessKey,accessid).or().eq(SysOpenAccess::getId,accessid)),false);
if((sysOpenAccess==null|| (sysOpenAccess.getDisabled()!=null && sysOpenAccess.getDisabled()==1))&&throwEx)
throw new BadRequestAlertException("获取接入配置失败","UserWechatRegisterService","");
return sysOpenAccess;
}
/**
* 通过code获取微信用户信息
* 微信服务端通过临时授权码code获取授权用户的个人信息
*
* @param code
* @param state
* @param appId
* @param appSecret
* @return
*/
public JSONObject requestWechatUserByCode(String code, String state, String appId, String appSecret) {
public JSONObject getUserBySnsToken(String id,String requestAuthCode) {
JSONObject returnObj = null;
SysOpenAccess openAccess = getOpenAccess(id);
if (openAccess==null || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
throw new BadRequestAlertException("未找到配置", "UserWechatRegisterService", "");
try {
// 1.根据code获取access_token、openid、refresh_token
String getAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
"appid=" + appId +
"&secret=" + appSecret +
"&code=" + code +
"appid=" + openAccess.getAccessKey() +
"&secret=" + openAccess.getSecretKey() +
"&code=" + requestAuthCode +
"&grant_type=authorization_code";
JSONObject responseObj = JSONObject.parseObject(HttpUtils.get(getAccessTokenUrl, null, null));
if (!responseObj.containsKey("access_token") && !responseObj.containsKey("openid")) {
returnObj = JSONObject.parseObject(HttpUtils.get(getAccessTokenUrl, null, null));
if (!returnObj.containsKey("access_token") && !returnObj.containsKey("openid")) {
throw new BadRequestAlertException("微信服务端获取access_token失败!", "UserWechatRegisterService", "");
}
String access_token = responseObj.getString("access_token");
String openid = responseObj.getString("openid");
String refresh_token = responseObj.getString("refresh_token");
// 2.检验授权凭证(access_token)是否有效,无效则需要刷新access_token
String checkAccessTokenUrl = "https://api.weixin.qq.com/sns/auth?" +
"access_token=" + access_token +
"&openid=" + openid;
JSONObject responseObj2 = JSONObject.parseObject(HttpUtils.get(checkAccessTokenUrl, null, null));
if (responseObj2.getInteger("errcode") != 0) {
// access_token已失效,使用refresh_token刷新access_token
String refreshAccess_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token?" +
"appid=" + appId +
"&grant_type=refresh_token" +
"&refresh_token=" + refresh_token;
JSONObject responseObj3 = JSONObject.parseObject(HttpUtils.get(refreshAccess_token, null, null));
if (!responseObj3.containsKey("access_token") || !responseObj3.containsKey("openid")) {
throw new BadRequestAlertException("微信服务端刷新access_token失败!", "UserWechatRegisterService", "");
}
// 重新拿到access_token、openid、refresh_token
access_token = responseObj3.getString("access_token");
openid = responseObj3.getString("openid");
refresh_token = responseObj3.getString("refresh_token");
String access_token = returnObj.getString("access_token");
String openid = returnObj.getString("openid");
String refresh_token = returnObj.getString("refresh_token");
String unionid = returnObj.getString("unionid");
SysUserAuth userAuth = sysUserAuthService.getOne(Wrappers.<SysUserAuth>lambdaQuery().eq(SysUserAuth::getIdentityType,"webchat")
.and(wrapper -> wrapper.eq(SysUserAuth::getIdentifier, openid).or().eq(SysUserAuth::getIdentifier, unionid)
),false);
IBZUSER user = null;
// 该wechat用户注册过账号,登录系统
if (userAuth!=null) {
user = ibzuserService.getById(userAuth.getUserid());
if (user == null)
user = ibzuserService.getOne(Wrappers.<IBZUSER>lambdaQuery().eq(IBZUSER::getUserid,openid).or().eq(IBZUSER::getUsername,openid),false);
if(user!=null)
{
returnObj.put("username",user.getLoginname()+(StringUtils.isEmpty(user.getDomains())?"":("|"+user.getDomains())));
}
// 3.access_token有效,拉取用户信息
String getweChatUserInfoUrl = "https://api.weixin.qq.com/sns/userinfo?" +
"access_token=" + access_token +
"&openid=" + openid +
"&lang=zh_CN";
returnObj = JSONObject.parseObject(HttpUtils.get(getweChatUserInfoUrl, null, null));
if (StringUtils.isEmpty(returnObj)) {
throw new BadRequestAlertException("微信服务端返回结果为空!", "UserWechatRegisterService", "");
} else if (!returnObj.containsKey("openid")) {
throw new BadRequestAlertException("获取微信用户信息失败!", "UserWechatRegisterService", "");
}
} catch (UnsupportedEncodingException e) {
throw new BadRequestAlertException("连接微信服务端失败!", "UserWechatRegisterService", "");
} catch (Exception e) {
e.printStackTrace();
throw new InternalServerErrorException("获取user失败");
}
return returnObj;
......
......@@ -50,7 +50,7 @@ public class UserDingtalkRegisterResource {
@GetMapping(value = {"/uaa/getDingtalkAppId","/uaa/open/dingtalk/access_token","/uaa/open/dingtalk/appid"})
public ResponseEntity<JSONObject> getDingtalkAppId(@RequestParam(value = "id",required = false) String id) {
JSONObject obj = new JSONObject();
SysOpenAccess openAccess = userDingtalkRegisterService.getOpenAccess(id);
SysOpenAccess openAccess = userDingtalkRegisterService.getOpenAccess(id,false);
if (openAccess==null || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
return ResponseEntity.ok(obj);
String appId = openAccess.getAccessKey();
......
......@@ -3,12 +3,14 @@ package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.core.uaa.domain.SysOpenAccess;
import cn.ibizlab.core.uaa.domain.SysUserAuth;
import cn.ibizlab.core.uaa.extensions.service.UserQQRegisterService;
import cn.ibizlab.core.uaa.extensions.service.UserRegisterService;
import cn.ibizlab.core.uaa.service.ISysOpenAccessService;
import cn.ibizlab.core.uaa.service.ISysUserAuthService;
import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.security.AuthTokenUtil;
import cn.ibizlab.util.security.AuthenticationInfo;
import cn.ibizlab.util.security.AuthenticationUser;
import cn.ibizlab.util.service.AuthenticationUserService;
import cn.ibizlab.util.service.IBZUSERService;
......@@ -18,10 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
......@@ -33,6 +32,8 @@ public class UserQQRegisterResource {
@Autowired
private UserQQRegisterService userQQRegisterService;
@Autowired
private UserRegisterService userRegisterService;
@Autowired
private IBZUSERService ibzuserService;
@Autowired
private ISysUserAuthService sysUserAuthService;
......@@ -47,67 +48,59 @@ public class UserQQRegisterResource {
/**
* 获取QQ互联平台创建的网站应用appid
*/
@GetMapping(value = "/uaa/getQQAppId")
public ResponseEntity<JSONObject> getQQAppId() {
@GetMapping(value = {"/uaa/getQQAppId","/uaa/open/qq/access_token","/uaa/open/qq/appid"})
public ResponseEntity<JSONObject> getQQAppId(@RequestParam(value = "id",required = false) String id) {
JSONObject obj = new JSONObject();
SysOpenAccess openAccess = openAccessService.getById("qq");
SysOpenAccess openAccess = userQQRegisterService.getOpenAccess(id);
if (openAccess==null || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
return ResponseEntity.ok(obj);
String appId = openAccess.getAccessKey();// qq互联appid
if (!StringUtils.isEmpty(appId)) {
obj.put("appid", appId);
obj.put("corp_id",openAccess.getRegionId());
obj.put("redirect_uri",openAccess.getRedirectUri());
}
return ResponseEntity.ok(obj);
}
/**
* 根据code查QQ用户
* 根据code查qq用户
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/queryQQUserByCode")
public ResponseEntity<JSONObject> queryQQUserByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
public ResponseEntity<JSONObject> queryQQUserByCode(@RequestParam(value = "id",required = false) String id,@RequestParam(value = "code",required = false) String tmpcode,@RequestBody JSONObject param) {
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
code = tmpcode;
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserQQRegisterResource", "");
// 从数据库中获取qq互联信息
SysOpenAccess openAccess = openAccessService.getById("qq");
if (openAccess==null || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
throw new BadRequestAlertException("未找到配置", "UserQQRegisterResource", "");
String appId = openAccess.getAccessKey();// qq互联appid
String appSecret = openAccess.getSecretKey();// qq互联appkey
String redirectUri = openAccess.getRedirectUri();// qq互联应用回调地址
// 通过code获取QQ用户信息
String openid = null;
String nickname = null;
JSONObject returnObj = userQQRegisterService.requestQQUserByCode(code, redirectUri, appId, appSecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
openid = returnObj.getString("openid");
nickname = returnObj.getString("nickname");
object.put("openid", openid);
object.put("nickname", nickname);
return ResponseEntity.ok().body(getUserBySnsCode(id,code));
}
// 根据openid查用户授权信息
SysUserAuth userAuth = sysUserAuthService.getOne(Wrappers.<SysUserAuth>query().eq("identifier", openid));
// 该QQ用户注册过账号,登录系统
if (!StringUtils.isEmpty(userAuth)) {
IBZUSER ibzuser = ibzuserService.getById(userAuth.getUserid());
JSONObject ibzuserObj = new JSONObject();
ibzuserObj.put("loginname", ibzuser.getLoginname());
ibzuserObj.put("password", ibzuser.getPassword());
object.put("ibzuser", ibzuserObj);
@GetMapping(value = "/uaa/open/qq/sns/{code}")
public ResponseEntity<JSONObject> getUserBySnsToken(@PathVariable(value = "code") String code, @RequestParam(value = "id",required = false) String id) {
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserQQRegisterResource", "");
return ResponseEntity.ok().body(getUserBySnsCode(id,code));
}
private JSONObject getUserBySnsCode(String id,String code)
{
JSONObject object = userQQRegisterService.getUserBySnsToken(id,code);
if (!StringUtils.isEmpty(object.getString("username"))) {
String username = object.getString("username");
// 生成登录token信息
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
userDetailsService.resetByUsername(username);
AuthenticationUser user = userDetailsService.loadUserByUsername(username);
final String token = jwtTokenUtil.generateToken(user);
AuthenticationUser user2 = new AuthenticationUser();
CachedBeanCopier.copy(user, user2);
......@@ -116,8 +109,7 @@ public class UserQQRegisterResource {
object.put("token", token);
object.put("user", user2);
}
return ResponseEntity.ok().body(object);
return object;
}
......@@ -127,71 +119,55 @@ public class UserQQRegisterResource {
* @param param
* @return
*/
@PostMapping(value = "/uaa/bindQQtoRegister")
public ResponseEntity<JSONObject> bindQQtoRegister(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
@PostMapping(value = {"/uaa/bindQQtoRegister","/uaa/open/qq/bind"})
public ResponseEntity<AuthenticationInfo> bindQQToRegister(@RequestBody JSONObject param) {
// 空校验
String loginname = param.getString("loginname");
String password = param.getString("password");
String code = param.getString("code");
String state = param.getString("state");
String openid = param.getString("openid");
String nickname = param.getString("nickname");
String personname = param.getString("personname");
String phone = param.getString("phone");
String email = param.getString("email");
String domains = param.getString("domains");
if (StringUtils.isEmpty(loginname))
throw new BadRequestAlertException("用户名为空", "UserQQRegisterResource", "");
if (StringUtils.isEmpty(password))
throw new BadRequestAlertException("密码为空", "UserQQRegisterResource", "");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("QQ授权code为空", "UserQQRegisterResource", "");
if (StringUtils.isEmpty(state))
throw new BadRequestAlertException("QQ授权state为空", "UserQQRegisterResource", "");
if (StringUtils.isEmpty(openid))
throw new BadRequestAlertException("QQ信息openid为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(nickname))
throw new BadRequestAlertException("QQ信息nickname为空", "UserWechatRegisterResource", "");
throw new BadRequestAlertException("QQ信息openid为空", "UserQQRegisterResource", "");
// 检查用户名是否已被注册
List<IBZUSER> ibzusers = ibzuserService.list(Wrappers.<IBZUSER>query().eq("loginname", loginname));
if (ibzusers.size() > 0)
throw new BadRequestAlertException("该用户名已被注册", "UserQQRegisterResource", "");
// QQ用户注册
// 微信用户注册
IBZUSER ibzuser = new IBZUSER();
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
ibzuser.setLoginname(loginname);
ibzuser.setUserid("qq-" + uuid);
ibzuser.setPersonname(nickname);
ibzuser.setPersonname(StringUtils.isEmpty(personname)?nickname:personname);
ibzuser.setNickname(nickname);
userQQRegisterService.toRegister(ibzuser);
// 创建QQ用户授权信息
ibzuser.setPhone(phone);
ibzuser.setEmail(email);
ibzuser.setDomains(domains);
SysUserAuth userAuth = new SysUserAuth();
userAuth.setUserid(ibzuser.getUserid());
userAuth.setIdentifier(openid);
userAuth.setIdentityType("qq");
userQQRegisterService.toCreateUserAuth(userAuth);
// 注册成功,登录系统
if (!StringUtils.isEmpty(ibzuser)) {
JSONObject ibzuserObj = new JSONObject();
ibzuserObj.put("loginname", ibzuser.getLoginname());
ibzuserObj.put("password", ibzuser.getPassword());
object.put("ibzuser", ibzuserObj);
}
userRegisterService.toRegister(ibzuser,userAuth);
// 生成登录token信息
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
userDetailsService.resetByUsername(ibzuser.getLoginname()+(StringUtils.isEmpty(ibzuser.getDomains())?"":("|"+ibzuser.getDomains())));
AuthenticationUser user = userDetailsService.loadUserByUsername(ibzuser.getLoginname()+(StringUtils.isEmpty(ibzuser.getDomains())?"":("|"+ibzuser.getDomains())));
final String token = jwtTokenUtil.generateToken(user);
AuthenticationUser user2 = new AuthenticationUser();
CachedBeanCopier.copy(user, user2);
user2.setAuthorities(null);
user2.setPermissionList(null);
object.put("token", token);
object.put("user", user2);
return ResponseEntity.ok().body(object);
return ResponseEntity.ok().body(new AuthenticationInfo(token,user2));
}
}
......@@ -2,6 +2,7 @@ package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.core.uaa.domain.SysOpenAccess;
import cn.ibizlab.core.uaa.domain.SysUserAuth;
import cn.ibizlab.core.uaa.extensions.service.UserRegisterService;
import cn.ibizlab.core.uaa.extensions.service.UserWechatRegisterService;
import cn.ibizlab.core.uaa.service.ISysOpenAccessService;
import cn.ibizlab.core.uaa.service.ISysUserAuthService;
......@@ -9,6 +10,7 @@ import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.CachedBeanCopier;
import cn.ibizlab.util.security.AuthTokenUtil;
import cn.ibizlab.util.security.AuthenticationInfo;
import cn.ibizlab.util.security.AuthenticationUser;
import cn.ibizlab.util.service.AuthenticationUserService;
import cn.ibizlab.util.service.IBZUSERService;
......@@ -18,10 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
......@@ -33,6 +32,8 @@ public class UserWechatRegisterResource {
@Autowired
private UserWechatRegisterService userWechatRegisterService;
@Autowired
private UserRegisterService userRegisterService;
@Autowired
private IBZUSERService ibzuserService;
@Autowired
private ISysUserAuthService sysUserAuthService;
......@@ -47,21 +48,25 @@ public class UserWechatRegisterResource {
/**
* 获取微信开放平台创建的网站应用appid
*/
@GetMapping(value = "/uaa/getWechatAppId")
public ResponseEntity<JSONObject> getWechatAppId() {
@GetMapping(value = {"/uaa/getWechatAppId","/uaa/open/wechat/access_token","/uaa/open/wechat/appid"})
public ResponseEntity<JSONObject> getWechatAppId(@RequestParam(value = "id",required = false) String id) {
JSONObject obj = new JSONObject();
SysOpenAccess openAccess = openAccessService.getById("webchart");
SysOpenAccess openAccess = userWechatRegisterService.getOpenAccess(id,false);
if (openAccess==null || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
return ResponseEntity.ok(obj);
String appId = openAccess.getAccessKey();// qq互联appid
if (!StringUtils.isEmpty(appId)) {
obj.put("appid", appId);
obj.put("corp_id",openAccess.getRegionId());
obj.put("redirect_uri",openAccess.getRedirectUri());
}
return ResponseEntity.ok(obj);
}
/**
* 根据code查微信用户
*
......@@ -69,44 +74,34 @@ public class UserWechatRegisterResource {
* @return
*/
@PostMapping(value = "/uaa/queryWechatUserByCode")
public ResponseEntity<JSONObject> queryWechatUserByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
public ResponseEntity<JSONObject> queryWechatUserByCode(@RequestParam(value = "id",required = false) String id,@RequestParam(value = "code",required = false) String tmpcode,@RequestBody JSONObject param) {
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
code = tmpcode;
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserWechatRegisterResource", "");
// 从数据库中获取微信授权应用信息
SysOpenAccess openAccess = openAccessService.getById("webchart");
if (openAccess==null || (openAccess.getDisabled()!=null && openAccess.getDisabled()==1))
throw new BadRequestAlertException("未找到配置", "UserWechatRegisterResource", "");
String appId = openAccess.getAccessKey();// 微信开放平台appid
String appSecret = openAccess.getSecretKey();// 微信开放平台appsecret
// 通过code获取微信用户信息
String openid = null;
String nickname = null;
JSONObject returnObj = userWechatRegisterService.requestWechatUserByCode(code, null, appId, appSecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
openid = returnObj.getString("openid");
nickname = returnObj.getString("nickname");
object.put("openid", openid);
object.put("nickname", nickname);
return ResponseEntity.ok().body(getUserBySnsCode(id,code));
}
// 根据openid查用户授权信息
SysUserAuth userAuth = sysUserAuthService.getOne(Wrappers.<SysUserAuth>query().eq("identifier", openid));
// 该微信用户注册过账号,登录系统
if (!StringUtils.isEmpty(userAuth)) {
IBZUSER ibzuser = ibzuserService.getById(userAuth.getUserid());
JSONObject ibzuserObj = new JSONObject();
ibzuserObj.put("loginname", ibzuser.getLoginname());
ibzuserObj.put("password", ibzuser.getPassword());
object.put("ibzuser", ibzuserObj);
@GetMapping(value = "/uaa/open/wechat/sns/{code}")
public ResponseEntity<JSONObject> getUserBySnsToken(@PathVariable(value = "code") String code, @RequestParam(value = "id",required = false) String id) {
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserWechatRegisterResource", "");
return ResponseEntity.ok().body(getUserBySnsCode(id,code));
}
private JSONObject getUserBySnsCode(String id,String code)
{
JSONObject object = userWechatRegisterService.getUserBySnsToken(id,code);
if (!StringUtils.isEmpty(object.getString("username"))) {
String username = object.getString("username");
// 生成登录token信息
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
userDetailsService.resetByUsername(username);
AuthenticationUser user = userDetailsService.loadUserByUsername(username);
final String token = jwtTokenUtil.generateToken(user);
AuthenticationUser user2 = new AuthenticationUser();
CachedBeanCopier.copy(user, user2);
......@@ -115,8 +110,7 @@ public class UserWechatRegisterResource {
object.put("token", token);
object.put("user", user2);
}
return ResponseEntity.ok().body(object);
return object;
}
......@@ -126,71 +120,56 @@ public class UserWechatRegisterResource {
* @param param
* @return
*/
@PostMapping(value = "/uaa/bindWechatToRegister")
public ResponseEntity<JSONObject> bindWechatToRegister(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
@PostMapping(value = {"/uaa/bindWechatToRegister","/uaa/open/wechat/bind"})
public ResponseEntity<AuthenticationInfo> bindWechatToRegister(@RequestBody JSONObject param) {
// 空校验
String loginname = param.getString("loginname");
String password = param.getString("password");
String openid = param.getString("openid");
String nickname = param.getString("nickname");
String code = param.getString("code");
String state = param.getString("state");
String personname = param.getString("personname");
String phone = param.getString("phone");
String email = param.getString("email");
String domains = param.getString("domains");
if (StringUtils.isEmpty(loginname))
throw new BadRequestAlertException("用户名为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(password))
throw new BadRequestAlertException("密码为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(openid))
throw new BadRequestAlertException("微信信息openid为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(nickname))
throw new BadRequestAlertException("微信信息nickname为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("微信授权code为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(state))
throw new BadRequestAlertException("微信授权state为空", "UserWechatRegisterResource", "");
// 检查用户名是否已被注册
List<IBZUSER> ibzusers = ibzuserService.list(Wrappers.<IBZUSER>query().eq("loginname", loginname));
if (ibzusers.size() > 0)
throw new BadRequestAlertException("该用户名已被注册", "UserWechatRegisterResource", "");
// 微信用户注册
IBZUSER ibzuser = new IBZUSER();
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
ibzuser.setLoginname(loginname);
ibzuser.setUserid("wechat-" + uuid);
ibzuser.setPersonname(nickname);
ibzuser.setPersonname(StringUtils.isEmpty(personname)?nickname:personname);
ibzuser.setNickname(nickname);
userWechatRegisterService.toRegister(ibzuser);
// 创建微信用户授权信息
ibzuser.setPhone(phone);
ibzuser.setEmail(email);
ibzuser.setDomains(domains);
SysUserAuth userAuth = new SysUserAuth();
userAuth.setUserid(ibzuser.getUserid());
userAuth.setIdentifier(openid);
userAuth.setIdentityType("wechat");
userWechatRegisterService.toCreateUserAuth(userAuth);
// 注册成功,登录系统
if (!StringUtils.isEmpty(ibzuser)) {
JSONObject ibzuserObj = new JSONObject();
ibzuserObj.put("loginname", ibzuser.getLoginname());
ibzuserObj.put("password", ibzuser.getPassword());
object.put("ibzuser", ibzuserObj);
}
userRegisterService.toRegister(ibzuser,userAuth);
// 生成登录token信息
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
userDetailsService.resetByUsername(ibzuser.getLoginname()+(StringUtils.isEmpty(ibzuser.getDomains())?"":("|"+ibzuser.getDomains())));
AuthenticationUser user = userDetailsService.loadUserByUsername(ibzuser.getLoginname()+(StringUtils.isEmpty(ibzuser.getDomains())?"":("|"+ibzuser.getDomains())));
final String token = jwtTokenUtil.generateToken(user);
AuthenticationUser user2 = new AuthenticationUser();
CachedBeanCopier.copy(user, user2);
user2.setAuthorities(null);
user2.setPermissionList(null);
object.put("token", token);
object.put("user", user2);
return ResponseEntity.ok().body(object);
return ResponseEntity.ok().body(new AuthenticationInfo(token,user2));
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册