提交 8ffdfae1 编写于 作者: laizhilong's avatar laizhilong

微信授权登录、钉钉授权登录

上级 fefb0d0c
......@@ -17,7 +17,6 @@
if (code && state) {
// 通过授权code请求后台
// alert(window.location.hostname);
var opt = {"code": code, "state": state};
$.ajax({
type: "post",
......@@ -41,9 +40,8 @@
// 跳转首页
window.location.href = "../index";
} else {
// 跳转微信绑定
alert("跳转到微信")
window.location.href = "../#/weixinLoginRedirect?code=" + code + "&state=" + state;
// 跳转钉钉绑定
window.location.href = "../#/dingdingLoginRedirect?code=" + code + "&state=" + state;
}
}
},
......@@ -54,7 +52,7 @@
}
});
} else {
alert("微信授权登录失败!");
alert("钉钉授权登录失败!");
// 回到登录页
window.location.href = "../";
}
......
......@@ -17,7 +17,6 @@
if (code && state) {
// 通过授权code请求后台
// alert(window.location.hostname);
var opt = {"code": code, "state": state};
$.ajax({
type: "post",
......
.login {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
min-height: 100%;
background: #108cee;
> img {
width: 100vw;
height: 100vh;
min-width: 1280px;
min-height: 720px;
}
&-con {
position: absolute;
left: 0;
right: 0;
top: 150px;
margin: auto;
width: 450px;
.ivu-card-head {
padding: 30px 6px;
border-bottom: 0px;
> p {
line-height: 24px;
height: 24px;
margin-bottom: -2px;
font-size: 24px;
color: #666666;
font-weight: 700;
}
}
&-header {
font-size: 16px;
font-weight: 300;
text-align: center;
padding: 30px 0;
}
.form-con {
padding: 0px 20px 0px 20px;
> i-button {
width: 170px;
height: 40px;
}
}
}
}
.log_footer {
display: block;
padding: 0 16px;
margin: 48px 0 24px;
text-align: center;
color: #212529;
}
.log_footer a {
color: white;
text-decoration: none;
}
.goLogin {
float: right;
font-size: 16px;
margin-right: 30px;
text-decoration: underline;
}
.confirm_register {
height: 40px;
font-size: 18px;
font-family: MicrosoftYaHei;
}
.disabled {
background-color: #ddd;
border-color: #ddd;
color: #57a3f3;
cursor: not-allowed; // 鼠标变化
}
\ No newline at end of file
<template>
<div class='login'>
<img src="/assets/img/background.png" draggable="false"/>
<div class='login-con'>
<card :bordered="false">
<p slot='title' style="text-align: center">
账号绑定
<!--<a @click="goLogin" class="goLogin">返回登录</a>-->
</p>
<div class='form-con'>
<i-form ref='loginForm' :rules="rules" :model="form">
<form-item prop='loginname'>
<i-input
size='large'
prefix='ios-contact'
v-model.trim="form.loginname"
placeholder="用户名"
@keyup.enter.native="handleRegister">
</i-input>
</form-item>
<form-item prop='password'>
<i-input
size='large'
prefix='ios-key'
v-model.trim="form.password"
type='password'
placeholder="密码"
@keyup.enter.native="handleRegister">
</i-input>
</form-item>
<form-item>
<i-button
@click="handleRegister"
type='success'
class="confirm_register"
:class="{disabled: !this.canClick}"
long>{{this.BtnContent}}
</i-button>
</form-item>
</i-form>
</div>
</card>
<div class="log_footer">
<div class="copyright">
<a href="https://www.ibizlab.cn/" target="_blank">{{appTitle}} is based on ibizlab .</a>
</div>
</div>
</div>
</div>
</template>
<script lang="ts">
import {Vue, Component, Watch} from 'vue-property-decorator';
import {Environment} from '@/environments/environment';
@Component({
components: {}
})
export default class dingdingLoginRedirect extends Vue {
/**
* 表单对象
*
* @type {*}
* @memberof Register
*/
public form: any = {loginname: '', password: ''};
/**
* 按钮可点击
*/
public canClick: any = true;
/**
* 按钮内容
*/
public BtnContent: any = "注册并绑定钉钉";
/**
* 钉钉授权成功返回的code和state
*/
public code: any;
public state: any;
/**
* 钉钉用户身份的唯一标识
*/
public openid: any;
/**
* 钉钉用户名称
*/
public nickname: any;
/**
* 应用名称
*
* @type {string}
* @memberof Register
*/
public appTitle: string = Environment.AppTitle;
/**
* 值规则
*
* @type {*}
* @memberof Register
*/
public rules = {};
/**
* 设置值规则
*
* @memberof Register
*/
public setRules() {
this.rules = {
loginname: [
{required: true, message: this.$t('components.login.loginname.message'), trigger: 'change'},
],
password: [
{required: true, message: this.$t('components.login.password.message'), trigger: 'change'},
]
}
};
/**
* 生命周期Create
*
* @memberof Register
*/
public created() {
this.setRules();
}
/**
* 挂载
*/
public mounted() {
// 从url获取授权code和state
this.code = this.$route.query.code;
if (!this.code) {
this.code = this.getUrlParam('code');
}
this.state = this.$route.query.state;
if (!this.state) {
this.state = this.getUrlParam('state');
}
// alert("code:" + this.code + "\nstate:" + this.state);
// 获取失败,回到登录页
if (!this.code || !this.state) {
this.$message.error("钉钉授权,获取code失败");
this.goLogin();
}
else {
// 根据code获取钉钉用户信息
this.getDingtalkUserInfoByCode(this.code);
}
}
/**
* 监听语言变化
*
* @memberof Login
*/
@Watch('$i18n.locale')
onLocaleChange(newval: any, val: any) {
this.setRules();
}
/**
* 跳转登录页面
*
* @memberof Register
*/
public goLogin(): void {
const _this = this;
_this.$router.push('/login');
}
/**
* 获取url参数
*/
public getUrlParam(name: any) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]);
return null; //返回参数值
}
/**
* 根据code获取钉钉用户信息
*/
private getDingtalkUserInfoByCode(code: any) {
var param: any = {};
param.code = code;
const post: Promise<any> = this.$http.post('/uaa/getDingtalkUserInfoByCode', param, true);
post.then((response: any) => {
if (response && response.status === 200) {
const data = response.data;
if (data && data.openid) {
this.openid = data.openid;
this.nickname = data.nickname;
} else {
this.$Message.error({
content: "获取钉钉用户信息失败,请重新授权",
duration: 3,
closable: true
});
}
}
}).catch((e: any) => {
const data = e.data;
if (data && data.message) {
this.$Message.error({
content: data.message,
duration: 3,
closable: true
});
} else {
this.$Message.error({
content: "获取钉钉用户信息失败,请重新授权",
duration: 3,
closable: true
});
}
});
}
/**
* 注册处理
*/
public handleRegister(): void {
if (this.canClick != true) {
return;
}
const form: any = this.$refs.loginForm;
let validatestate: boolean = true;
form.validate((valid: boolean) => {
validatestate = valid ? true : false;
});
if (!validatestate) {
return;
}
// 请求传参
var param: any = this.form;
if (this.code && this.state) {
param.openid = this.openid;
param.nickname = this.nickname;
} else {
this.$message.error("钉钉授权,获取code失败");
return;
}
const post: Promise<any> = this.$http.post('/uaa/bindDingtalkToRegister', param, true);
post.then((response: any) => {
if (response && response.status === 200) {
const data = response.data;
if (data) {
this.$Message.success({
content: "注册成功,正在登录"
});
if (data.token) {
localStorage.setItem('token', data.token);
}
if (data.user) {
localStorage.setItem('user', JSON.stringify(data.user));
}
if (data.ibzuser) {
let ibzuser: any = JSON.stringify(data.ibzuser);
// 设置cookie,保存账号密码7天
this.setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页
const url: any = '*';
this.$router.push({path: url});
}
}
}
}).catch((e: any) => {
const data = e.data;
if (data && data.message) {
this.$Message.error({
content: "注册失败," + data.message,
duration: 3,
closable: true
});
} else {
this.$Message.error({
content: "注册失败",
duration: 3,
closable: true
});
}
});
}
/**
* 设置cookie,保存账号密码
* @param loginname
* @param password
*/
public setCookie(loginname: any, password: any, exdays: any) {
// 获取时间
let exdate = new Date();
// 保存的天数
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
// 字符串拼接cookie
window.document.cookie = "loginname" + "=" + loginname + ";path=/;expires=" + exdate.toUTCString();
window.document.cookie = "password" + "=" + password + ";path=/;expires=" + exdate.toUTCString();
}
}
</script>
<style lang='less'>
@import 'dingdingLoginRedirect.less';
</style>
\ No newline at end of file
......@@ -439,27 +439,6 @@
}
/**
* 打开一个新窗口
* @param url 链接地址
* @param title 窗口标题
* @param w 窗口宽度
* @param h 窗口高度
*/
public openWindow(url: any, title: any, w: any, h: any): void {
const dualScreenLeft = window.screenLeft;
const dualScreenTop = window.screenTop;
const width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
const height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
const left = ((width / 2) - (w / 2)) + dualScreenLeft;
const top = ((height / 2) - (h / 2)) + dualScreenTop;
const newWindow = window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
if (window.focus && newWindow) {
newWindow.focus();
}
}
}
</script>
......
.login {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
min-height: 100%;
background: #108cee;
> img {
width: 100vw;
height: 100vh;
min-width: 1280px;
min-height: 720px;
}
&-con {
position: absolute;
left: 0;
right: 0;
top: 150px;
margin: auto;
width: 450px;
.ivu-card-head {
padding: 30px 6px;
border-bottom: 0px;
> p {
line-height: 24px;
height: 24px;
margin-bottom: -2px;
font-size: 24px;
color: #666666;
font-weight: 700;
}
}
&-header {
font-size: 16px;
font-weight: 300;
text-align: center;
padding: 30px 0;
}
.form-con {
padding: 0px 20px 0px 20px;
> i-button {
width: 170px;
height: 40px;
}
}
}
}
.log_footer {
display: block;
padding: 0 16px;
margin: 48px 0 24px;
text-align: center;
color: #212529;
}
.log_footer a {
color: white;
text-decoration: none;
}
.goLogin {
float: right;
font-size: 16px;
margin-right: 30px;
text-decoration: underline;
}
.confirm_register {
height: 40px;
font-size: 18px;
font-family: MicrosoftYaHei;
}
.disabled {
background-color: #ddd;
border-color: #ddd;
color: #57a3f3;
cursor: not-allowed; // 鼠标变化
}
\ No newline at end of file
<template>
<div class='login'>
<img src="/assets/img/background.png" draggable="false"/>
<div class='login-con'>
<card :bordered="false">
<p slot='title' style="text-align: center">
账号绑定
<!--<a @click="goLogin" class="goLogin">返回登录</a>-->
</p>
<div class='form-con'>
<i-form ref='loginForm' :rules="rules" :model="form">
<form-item prop='loginname'>
<i-input
size='large'
prefix='ios-contact'
v-model.trim="form.loginname"
placeholder="用户名"
@keyup.enter.native="handleRegister">
</i-input>
</form-item>
<form-item prop='password'>
<i-input
size='large'
prefix='ios-key'
v-model.trim="form.password"
type='password'
placeholder="密码"
@keyup.enter.native="handleRegister">
</i-input>
</form-item>
<form-item>
<i-button
@click="handleRegister"
type='success'
class="confirm_register"
:class="{disabled: !this.canClick}"
long>{{this.BtnContent}}
</i-button>
</form-item>
</i-form>
</div>
</card>
<div class="log_footer">
<div class="copyright">
<a href="https://www.ibizlab.cn/" target="_blank">{{appTitle}} is based on ibizlab .</a>
</div>
</div>
</div>
</div>
</template>
<script lang="ts">
import {Vue, Component, Watch} from 'vue-property-decorator';
import {Environment} from '@/environments/environment';
@Component({
components: {}
})
export default class weixinLoginRedirect extends Vue {
/**
* 表单对象
*
* @type {*}
* @memberof Register
*/
public form: any = {loginname: '', password: ''};
/**
* 按钮可点击
*/
public canClick: any = true;
/**
* 按钮内容
*/
public BtnContent: any = "注册并绑定微信";
/**
* 微信公众号授权成功返回的code和state
*/
public code: any;
public state: any;
/**
* 钉钉用户身份的唯一标识
*/
public openid: any;
/**
* 微信用户名称
*/
public nickname: any;
/**
* 应用名称
*
* @type {string}
* @memberof Register
*/
public appTitle: string = Environment.AppTitle;
/**
* 值规则
*
* @type {*}
* @memberof Register
*/
public rules = {};
/**
* 设置值规则
*
* @memberof Register
*/
public setRules() {
this.rules = {
loginname: [
{required: true, message: this.$t('components.login.loginname.message'), trigger: 'change'},
],
password: [
{required: true, message: this.$t('components.login.password.message'), trigger: 'change'},
]
}
};
/**
* 生命周期Create
*
* @memberof Register
*/
public created() {
this.setRules();
}
/**
* 挂载
*/
public mounted() {
// 从url获取授权code和state
this.code = this.$route.query.code;
if (!this.code) {
this.code = this.getUrlParam('code');
}
this.state = this.$route.query.state;
if (!this.state) {
this.state = this.getUrlParam('state');
}
// alert("code:" + this.wechatCode + "\nstate:" + this.wechatState);
// 获取失败,回到登录页
if (!this.code || !this.state) {
this.$message.error("微信授权,获取code失败");
this.goLogin();
}
else {
// 根据code获取微信用户信息
this.getWechatUserInfoByCode(this.code);
}
}
/**
* 监听语言变化
*
* @memberof Login
*/
@Watch('$i18n.locale')
onLocaleChange(newval: any, val: any) {
this.setRules();
}
/**
* 跳转登录页面
*
* @memberof Register
*/
public goLogin(): void {
const _this = this;
_this.$router.push('/login');
}
/**
* 获取url参数
*/
public getUrlParam(name: any) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r != null) return unescape(r[2]);
return null; //返回参数值
}
/**
* 根据code获取微信用户信息
*/
private getWechatUserInfoByCode(code: any) {
var param: any = {};
param.code = code;
const post: Promise<any> = this.$http.post('/uaa/getWechatUserInfoByCode', param, true);
post.then((response: any) => {
if (response && response.status === 200) {
const data = response.data;
if (data && data.openid) {
this.openid = data.openid;
this.nickname = data.nickname;
} else {
this.$Message.error({
content: "获取微信用户信息失败,请重新授权",
duration: 3,
closable: true
});
}
}
}).catch((e: any) => {
const data = e.data;
if (data && data.message) {
this.$Message.error({
content: data.message,
duration: 3,
closable: true
});
} else {
this.$Message.error({
content: "获取微信用户信息失败,请重新授权",
duration: 3,
closable: true
});
}
});
}
/**
* 注册处理
*/
public handleRegister(): void {
if (this.canClick != true) {
return;
}
const form: any = this.$refs.loginForm;
let validatestate: boolean = true;
form.validate((valid: boolean) => {
validatestate = valid ? true : false;
});
if (!validatestate) {
return;
}
// 请求传参
var param: any = this.form;
if (this.code && this.state) {
param.code = this.code;
param.state = this.state;
param.openid = this.openid;
param.nickname = this.nickname;
} else {
this.$message.error("微信授权,获取code失败");
return;
}
const post: Promise<any> = this.$http.post('/uaa/bindWechatToRegister', param, true);
post.then((response: any) => {
if (response && response.status === 200) {
const data = response.data;
if (data) {
this.$Message.success({
content: "注册成功,正在登录"
});
if (data.token) {
localStorage.setItem('token', data.token);
}
if (data.user) {
localStorage.setItem('user', JSON.stringify(data.user));
}
if (data.ibzuser) {
let ibzuser: any = JSON.stringify(data.ibzuser);
// 设置cookie,保存账号密码7天
this.setCookie(ibzuser.loginname, ibzuser.password, 7);
// 跳转首页
const url: any = '*';
this.$router.push({path: url});
}
}
}
}).catch((e: any) => {
const data = e.data;
if (data && data.message) {
this.$Message.error({
content: "注册失败," + data.message,
duration: 3,
closable: true
});
} else {
this.$Message.error({
content: "注册失败",
duration: 3,
closable: true
});
}
});
}
/**
* 设置cookie,保存账号密码
* @param loginname
* @param password
*/
public setCookie(loginname: any, password: any, exdays: any) {
// 获取时间
let exdate = new Date();
// 保存的天数
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays);
// 字符串拼接cookie
window.document.cookie = "loginname" + "=" + loginname + ";path=/;expires=" + exdate.toUTCString();
window.document.cookie = "password" + "=" + password + ";path=/;expires=" + exdate.toUTCString();
}
}
</script>
<style lang='less'>
@import './weixinLoginRedirect.less';
</style>
\ No newline at end of file
......@@ -12,6 +12,10 @@ export const globalRoutes:Array<any> = [
path: '/weixinLoginRedirect',
component: ()=> import('@components/login/weixinLoginRedirect.vue'),
},
{
path: '/dingdingLoginRedirect',
component: ()=> import('@components/login/dingdingLoginRedirect.vue'),
},
];
//用户自定义首页路由
export const indexRoutes:Array<any> = [
......
......@@ -106,18 +106,18 @@ public class DevBootSecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/syspssystems/save").permitAll()
.antMatchers("/uaa/login").permitAll()
.antMatchers("/uaa/register").permitAll()
.antMatchers("/uaa/thirdPartRegister").permitAll()
.antMatchers("/uaa/responseTokenToWeiXin").permitAll()
.antMatchers("/uaa/getWechatAppId").permitAll()
.antMatchers("/uaa/queryWechatUserByCode").permitAll()
.antMatchers("/uaa/getWechatUserInfoByCode").permitAll()
.antMatchers("/uaa/bindWechatToRegister").permitAll()
.antMatchers("/uaa/getDingtalkAppId").permitAll()
.antMatchers("/uaa/queryDingtalkUserByCode").permitAll()
.antMatchers("/uaa/getDingtalkUserInfoByCode").permitAll()
.antMatchers("/uaa/bindDingtalkToRegister").permitAll()
.antMatchers("/uaa/queryQQUserByCode").permitAll()
.antMatchers("/uaa/getQQAppId").permitAll()
.anyRequest().authenticated()
// 防止iframe 造成跨域
.and().headers().frameOptions().disable();
......
package cn.ibizlab.core.uaa.extensions.service;
import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.service.IBZUSERService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.identify.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* 实体[IBZUSER] 钉钉用户注册接口实现
*/
@Service
@Slf4j
public class UserDingtalkRegisterService {
@Autowired
private IBZUSERService ibzuserService;
/**
* 注册
*
* @param ibzuser
* @return
*/
public IBZUSER toRegister(IBZUSER ibzuser) {
// 创建ibzuser
boolean flag = ibzuserService.save(ibzuser);
if (!flag) {
return null;
}
return ibzuser;
}
/**
* 钉钉服务端通过临时授权码code获取授权用户的个人信息
*
* @param code
* @param currentTimeMillis
* @param dingTalkAppId
* @param dingTalkAppSecret
* @return
*/
public JSONObject requestDingtalkUserByCode(String code, long currentTimeMillis, String dingTalkAppId, String dingTalkAppSecret) {
JSONObject returnObj = null;
try {
// 根据timestamp, appSecret计算签名值
String stringToSign = String.valueOf(currentTimeMillis);
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(dingTalkAppSecret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signatureBytes = mac.doFinal(stringToSign.getBytes("UTF-8"));
String signature = new String(Base64.encodeBase64(signatureBytes));
String urlEncodeSignature = URLEncoder.encode(signature, "UTF-8");
// 通过临时授权码Code获取用户信息,临时授权码只能使用一次
String url = "https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=" + dingTalkAppId
+ "&timestamp=" + String.valueOf(currentTimeMillis)
+ "&signature=" + urlEncodeSignature;
// 创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
JSONObject param = new JSONObject();
param.put("tmp_auth_code", code);
StringEntity jsonBody = new StringEntity(param.toString(), "UTF-8");
// 设置参数到请求对象中
httpPost.setEntity(jsonBody);
// 设置header信息
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
// 获取结果实体
HttpEntity entity = response.getEntity();
JSONObject entityJson = null;
if (entity != null) {
//按指定编码转换结果实体为String类型
entityJson = JSONObject.parseObject(EntityUtils.toString(entity, "UTF-8"));
}
// 是否获取钉钉用户信息成功
if (entityJson.containsKey("user_info")) {
returnObj = entityJson.getJSONObject("user_info");
} else {
throw new BadRequestAlertException("code获取钉钉用户信息失败", "UserDingtalkRegisterService", "");
}
} catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
throw new BadRequestAlertException("钉钉生成安全签名失败", "UserDingtalkRegisterService", "");
}
return returnObj;
}
}
\ No newline at end of file
package cn.ibizlab.core.uaa.extensions.service;
import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.HttpUtils;
import cn.ibizlab.util.service.IBZUSERService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.identify.Base64;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* 实体[IBZUSER] 用户注册接口实现
......@@ -52,169 +32,4 @@ public class UserRegisterService {
return ibzuser;
}
/**
* 通过code获取微信用户信息
*
* @param code
* @param state
* @param wechatAppId
* @param wechatappsecret
* @return
*/
public JSONObject requestWechatUserByCode(String code, String state, String wechatAppId, String wechatappsecret) {
JSONObject returnObj = null;
try {
// 1.根据code获取access_token
String getAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatAppId + "&secret=" + wechatappsecret + "&code=" + code + "&grant_type=authorization_code";
JSONObject responseObj = JSONObject.parseObject(HttpUtils.get(getAccessTokenUrl, null, null));
if (!responseObj.containsKey("access_token") && !responseObj.containsKey("openid")) {
throw new BadRequestAlertException("获取access_token失败!", "UserRegisterService", "");
}
String access_token = responseObj.getString("access_token");
String openid = responseObj.getString("openid");
String refresh_token = responseObj.getString("refresh_token");
// 2.检验授权凭证(access_token)是否有效
String checkAccessTokenUrl = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + responseObj.get("openid");
JSONObject responseObj2 = JSONObject.parseObject(HttpUtils.get(checkAccessTokenUrl, null, null));
if (responseObj2.getInteger("errcode") != 0) {
// access_token已失效,使用refresh_token进行刷新
String refreshAccess_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + wechatAppId + "&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失败!", "UserRegisterService", "");
}
openid = responseObj3.getString("openid");
access_token = responseObj3.getString("access_token");
}
// 3.access_token有效,拉取用户信息(需scope为 snsapi_userinfo)
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) || returnObj.containsKey("errcode")) {
throw new BadRequestAlertException("拉取微信用户信息失败!", "UserRegisterService", "");
}
} catch (UnsupportedEncodingException e) {
throw new BadRequestAlertException("获取微信授权用户相关信息失败!", "UserRegisterService", "");
}
return returnObj;
}
/**
* 通过code获取qq用户信息
*
* @param code
* @param qqRedirectUri
* @param qqAppId
* @param qqAppKey
* @return
*/
public JSONObject requestQQUserByCode(String code, String qqRedirectUri, String qqAppId, String qqAppKey) {
JSONObject returnObj = null;
try {
// 1.根据code获取access_token
String getAccessTokenUrl = "https://graph.qq.com/oauth2.0/token?client_id=" + qqAppId + "&client_secret=" + qqAppKey + "&code=" + code + "&redirect_uri=" + qqRedirectUri + "&grant_type=authorization_code";
JSONObject responseObj = JSONObject.parseObject(HttpUtils.get(getAccessTokenUrl, null, null));
if (!responseObj.containsKey("access_token")) {
throw new BadRequestAlertException("获取access_token失败!", "UserRegisterService", "");
}
String access_token = responseObj.getString("access_token");
String refresh_token = responseObj.getString("refresh_token");
// 2.权限自动续期,获取access_token
String refreshAccessTokenUrl = "https://graph.qq.com/oauth2.0/token?client_id=" + qqAppId + "&client_secret=" + qqAppKey + "&refresh_token=" + refresh_token + "&grant_type=refresh_token";
JSONObject responseObj2 = JSONObject.parseObject(HttpUtils.get(refreshAccessTokenUrl, null, null));
if (responseObj2.containsKey("code")) {
throw new BadRequestAlertException("续期access_token失败!", "UserRegisterService", "");
} else {
access_token = responseObj2.getString("access_token");
refresh_token = responseObj2.getString("refresh_token");
}
// 3.access_token有效,获取openid
String getOpenidUrl = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
JSONObject responseObj3 = JSONObject.parseObject(HttpUtils.get(getOpenidUrl, null, null));
if (responseObj3.containsKey("code")) {
throw new BadRequestAlertException("拉取qq用户信息失败!", "UserRegisterService", "");
}
String openid = responseObj3.getString("openid");
// 4.使用access Token以及openid来访问和修改用户数据
String getQQUserInfoUrl = "https://graph.qq.com/user/get_user_info?access_token=" + access_token + "&oauth_consumer_key=" + qqAppId + "&openid=" + openid;
returnObj = JSONObject.parseObject(HttpUtils.get(getQQUserInfoUrl, null, null));
if (returnObj.containsKey("ret") && returnObj.getInteger("ret")!=0) {
throw new BadRequestAlertException("拉取qq用户信息失败!", "UserRegisterService", "");
}
returnObj.put("openid", openid);
} catch (UnsupportedEncodingException e) {
throw new BadRequestAlertException("获取qq授权用户相关信息失败!", "UserRegisterService", "");
}
return returnObj;
}
/**
* 钉钉服务端通过临时授权码code获取授权用户的个人信息
* @param code
* @param currentTimeMillis
* @param dingTalkAppId
* @param dingTalkAppSecret
* @return
*/
public JSONObject requestDingtalkUserByCode(String code, long currentTimeMillis, String dingTalkAppId, String dingTalkAppSecret) {
JSONObject returnObj = null;
try {
// 1.根据timestamp, appSecret计算签名值
String stringToSign = String.valueOf(currentTimeMillis);
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(dingTalkAppSecret.getBytes("UTF-8"), "HmacSHA256"));
byte[] signatureBytes = mac.doFinal(stringToSign.getBytes("UTF-8"));
String signature = new String(Base64.encodeBase64(signatureBytes));
String urlEncodeSignature = URLEncoder.encode(signature, "UTF-8");
// 2.通过临时授权码Code获取用户信息,临时授权码只能使用一次
String url = "https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=" + dingTalkAppId
+ "&timestamp=" + String.valueOf(currentTimeMillis)
+ "&signature=" + urlEncodeSignature;
// 创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
JSONObject param = new JSONObject();
param.put("tmp_auth_code",code);
StringEntity jsonBody = new StringEntity(param.toString(),"UTF-8");
// 设置参数到请求对象中
httpPost.setEntity(jsonBody);
// 设置header信息
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
//获取结果实体
HttpEntity entity = response.getEntity();
JSONObject entityJson = null;
if (entity != null) {
//按指定编码转换结果实体为String类型
entityJson = JSONObject.parseObject(EntityUtils.toString(entity, "UTF-8"));
}
if (entityJson.containsKey("user_info")) {
returnObj = entityJson.getJSONObject("user_info");
}else {
throw new BadRequestAlertException("code获取钉钉用户信息失败","UserRegisterService","");
}
} catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
throw new BadRequestAlertException("钉钉生成安全签名失败","UserRegisterService","");
}
return returnObj;
}
}
\ No newline at end of file
package cn.ibizlab.core.uaa.extensions.service;
import cn.ibizlab.util.domain.IBZUSER;
import cn.ibizlab.util.errors.BadRequestAlertException;
import cn.ibizlab.util.helper.HttpUtils;
import cn.ibizlab.util.service.IBZUSERService;
import com.alibaba.fastjson.JSONObject;
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;
/**
* 实体[IBZUSER] 微信用户注册接口实现
*/
@Service
@Slf4j
public class UserWechatRegisterService {
@Autowired
private IBZUSERService ibzuserService;
/**
* 注册
*
* @param ibzuser
* @return
*/
public IBZUSER toRegister(IBZUSER ibzuser) {
// 创建ibzuser
boolean flag = ibzuserService.save(ibzuser);
if (!flag) {
return null;
}
return ibzuser;
}
/**
* 通过code获取微信用户信息
*
* @param code
* @param state
* @param wechatAppId
* @param wechatappsecret
* @return
*/
public JSONObject requestWechatUserByCode(String code, String state, String wechatAppId, String wechatappsecret) {
JSONObject returnObj = null;
try {
// 1.根据code获取access_token
String getAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wechatAppId + "&secret=" + wechatappsecret + "&code=" + code + "&grant_type=authorization_code";
JSONObject responseObj = JSONObject.parseObject(HttpUtils.get(getAccessTokenUrl, null, null));
if (!responseObj.containsKey("access_token") && !responseObj.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)是否有效
String checkAccessTokenUrl = "https://api.weixin.qq.com/sns/auth?access_token=" + access_token + "&openid=" + responseObj.get("openid");
JSONObject responseObj2 = JSONObject.parseObject(HttpUtils.get(checkAccessTokenUrl, null, null));
if (responseObj2.getInteger("errcode") != 0) {
// access_token已失效,使用refresh_token进行刷新
String refreshAccess_token = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=" + wechatAppId + "&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", "");
}
openid = responseObj3.getString("openid");
access_token = responseObj3.getString("access_token");
}
// 3.access_token有效,拉取用户信息(需scope为 snsapi_userinfo)
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) || returnObj.containsKey("errcode")) {
throw new BadRequestAlertException("拉取微信用户信息失败!", "UserWechatRegisterService", "");
}
} catch (UnsupportedEncodingException e) {
throw new BadRequestAlertException("获取微信授权用户相关信息失败!", "UserWechatRegisterService", "");
}
return returnObj;
}
}
\ No newline at end of file
package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.core.uaa.extensions.service.UserDingtalkRegisterService;
import cn.ibizlab.core.uaa.service.ISysPSSystemService;
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.AuthenticationUser;
import cn.ibizlab.util.service.AuthenticationUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RestController
public class UserDingtalkRegisterResource {
@Autowired
private UserDingtalkRegisterService userDingtalkRegisterService;
@Autowired
private ISysPSSystemService iSysPSSystemService;
@Autowired
private AuthTokenUtil jwtTokenUtil;
@Autowired
@Qualifier("UAAUserService")
private AuthenticationUserService userDetailsService;
@Value("${ibiz.auth.dingtalk.appid:dingoatysxi5rpbluqgzpk}")
private String DingtalkAppid;
@Value("${ibiz.auth.dingtalk.appid:z2SuCSciWQJ6VSFX8jvHzTF0G_rEmHpjHrZct0rGnWrLyl8cZiRny72244EvF4SO}")
private String DingtalkAppSecret;
@Value("${ibiz.auth.dingtalk.openid:}")
private String DingtalkOpenid;
@Value("${ibiz.auth.dingtalk.nickname:}")
private String DingtalkNickname;
/**
* 获取钉钉开放平台创建的网站应用appid
*/
@GetMapping(value = "/uaa/getDingtalkAppId")
public ResponseEntity<JSONObject> getDingtalkAppId() {
JSONObject obj = new JSONObject();
String appid = DingtalkAppid;
if (!StringUtils.isEmpty(appid)) {
obj.put("appid", appid);
}
return ResponseEntity.ok(obj);
}
/**
* 根据code查钉钉用户
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/queryDingtalkUserByCode")
public ResponseEntity<JSONObject> queryDingtalkUserByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserDingtalkRegisterResource", "");
// 通过code获取钉钉用户信息
String openid = null;
String nickname = null;
long currentTimeMillis = System.currentTimeMillis();
JSONObject returnObj = userDingtalkRegisterService.requestDingtalkUserByCode(code, currentTimeMillis, DingtalkAppid, DingtalkAppSecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
openid = returnObj.getString("openid");
nickname = returnObj.getString("nick");
// 将用户信息存到缓存,供当前人后续请求使用
DingtalkOpenid = openid;
DingtalkNickname = nickname;
}
//根据openid查用户
String sql = "select * from ibzuser where avatar=#{et.avatar}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("avatar", openid);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0) {
IBZUSER ibzuser = select.get(0).toJavaObject(IBZUSER.class);
object.put("ibzuser", ibzuser);
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
/**
* 根据code获取钉钉用户信息
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/getDingtalkUserInfoByCode")
public ResponseEntity<JSONObject> getDingtalkUserInfoByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserDingtalkRegisterResource", "");
// 通过code获取微信用户信息
String openid = DingtalkOpenid;
String nickname = DingtalkNickname;
object.put("openid", openid);
object.put("nickname", nickname);
return ResponseEntity.ok().body(object);
}
/**
* 绑定钉钉并注册
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/bindDingtalkToRegister")
public ResponseEntity<JSONObject> thirdPartRegister(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String loginname = param.getString("loginname");
String password = param.getString("password");
String openid = param.getString("openid");
if (StringUtils.isEmpty(openid)) {
openid = DingtalkOpenid;
}
String nickname = param.getString("nickname");
if (StringUtils.isEmpty(nickname)) {
nickname = DingtalkNickname;
}
if (StringUtils.isEmpty(loginname))
throw new BadRequestAlertException("用户名为空", "UserDingtalkRegisterResource", "");
if (StringUtils.isEmpty(password))
throw new BadRequestAlertException("密码为空", "UserDingtalkRegisterResource", "");
// 检查用户名是否已被注册
String sql = "select loginname from ibzuser where loginname=#{et.loginname}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("loginname", loginname);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0)
throw new BadRequestAlertException("该用户名已被注册", "UserDingtalkRegisterResource", "");
IBZUSER ibzuser = new IBZUSER();
if (StringUtils.isEmpty(openid)) {
// 钉钉授权code已失效,重新授权
throw new BadRequestAlertException("钉钉授权已失效,请重新授权","UserDingtalkRegisterResource","");
} else {
// 已经有钉钉用户信息直接注册
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
ibzuser.setLoginname(loginname);
ibzuser.setUserid("dingtalk-" + uuid);
ibzuser.setPersonname(nickname);
ibzuser.setAvatar(openid);
userDingtalkRegisterService.toRegister(ibzuser);
}
// 注册成功,登录系统
if (!StringUtils.isEmpty(ibzuser)) {
object.put("ibzuser", ibzuser);
}
// 生成登录token信息
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
}
package cn.ibizlab.api.rest.extensions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserQQRegisterResource {
@Value("${ibiz.auth.qq.appid:101881963}")
private String qqAppId;// qq互联appid
@Value("${ibiz.auth.qq.appkey:}")
private String qqAppKey;// qq互联appkey
@Value("${ibiz.auth.qq.redirect_uri:}")
private String qqRedirectUri;// qq互联应用回调地址
}
......@@ -4,26 +4,18 @@ import cn.ibizlab.core.uaa.extensions.service.UserRegisterService;
import cn.ibizlab.core.uaa.service.ISysPSSystemService;
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.AuthenticationUser;
import cn.ibizlab.util.service.AuthenticationUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RestController
......@@ -33,36 +25,6 @@ public class UserRegisterResource {
private UserRegisterService userRegisterService;
@Autowired
private ISysPSSystemService iSysPSSystemService;
@Autowired
private AuthTokenUtil jwtTokenUtil;
@Autowired
@Qualifier("UAAUserService")
private AuthenticationUserService userDetailsService;
@Value("${ibiz.auth.qq.appid:101881963}")
private String qqAppId;// qq互联appid
@Value("${ibiz.auth.qq.appkey:}")
private String qqAppKey;// qq互联appkey
@Value("${ibiz.auth.qq.redirect_uri:}")
private String qqRedirectUri;// qq互联应用回调地址
@Value("${ibiz.auth.wechat.appID:wx4a7473ac9c617f76}")
private String wechatAppId; // 微信开放平台appid
@Value("${ibiz.auth.wechat.appsecret:c2b308b9245dffa316b049840fd81a79}")
private String wechatappsecret; // 微信开放平台appsecret
@Value("${ibiz.auth.wechat.openid:}")
private String Avatar;
@Value("${ibiz.auth.wechat.nickname:}")
private String Nickname;
@Value("${ibiz.auth.dingding.appid:dingoatysxi5rpbluqgzpk}")
private String dingTalkAppId;
@Value("${ibiz.auth.dingding.appid:z2SuCSciWQJ6VSFX8jvHzTF0G_rEmHpjHrZct0rGnWrLyl8cZiRny72244EvF4SO}")
private String dingTalkAppSecret;
public static final String TOKEN = "weixin"; // 这里的token要和微信公众测试号网页填写的token一致
/**
* 普通注册
......@@ -109,379 +71,7 @@ public class UserRegisterResource {
return ResponseEntity.ok().body(object);
}
/**
* 第三方注册:qq、微信
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/thirdPartRegister")
public ResponseEntity<JSONObject> thirdPartRegister(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String loginname = param.getString("loginname");
String password = param.getString("password");
String registerType = param.getString("registerType");
String code = param.getString("wechatCode");
System.out.println("code:" + code);
String state = param.getString("wechatState");
System.out.println("state:" + state);
String avatar = param.getString("avatar");
if (StringUtils.isEmpty(avatar)) {
avatar = Avatar;
}
System.out.println("avatar:" + avatar);
String nickname = param.getString("nickname");
if (StringUtils.isEmpty(nickname)) {
nickname = Nickname;
}
System.out.println("nickname:" + nickname);
if (StringUtils.isEmpty(loginname))
throw new BadRequestAlertException("用户名为空", "UserRegisterResource", "");
if (StringUtils.isEmpty(password))
throw new BadRequestAlertException("密码为空", "UserRegisterResource", "");
if (StringUtils.isEmpty(registerType))
throw new BadRequestAlertException("注册方式为空", "UserRegisterResource", "");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("微信授权code为空", "UserRegisterResource", "");
if (StringUtils.isEmpty(state))
throw new BadRequestAlertException("微信授权state为空", "UserRegisterResource", "");
// 检查用户名是否已被注册
String sql = "select loginname from ibzuser where loginname=#{et.loginname}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("loginname", loginname);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0)
throw new BadRequestAlertException("该用户名已被注册", "UserRegisterResource", "");
IBZUSER ibzuser = new IBZUSER();
if (StringUtils.isEmpty(avatar)) {
// 通过code获取微信用户信息
JSONObject returnObj = userRegisterService.requestWechatUserByCode(code, state ,wechatAppId , wechatappsecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
// 微信注册
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
ibzuser.setLoginname(loginname);
ibzuser.setUserid("wechat-" + uuid);
ibzuser.setPersonname(returnObj.getString("nickname"));
ibzuser.setAvatar(returnObj.getString("openid"));
userRegisterService.toRegister(ibzuser);
object.put("ibzuser", ibzuser);
}
} else {
// 已经有微信用户信息直接注册
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
ibzuser.setLoginname(loginname);
ibzuser.setUserid("wechat-" + uuid);
ibzuser.setPersonname(nickname);
ibzuser.setAvatar(avatar);
userRegisterService.toRegister(ibzuser);
object.put("ibzuser", ibzuser);
}
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
/**
* 获取qq互联创建的网站应用appid
*/
@GetMapping(value = "/uaa/getQQAppId")
public ResponseEntity<JSONObject> getTecentAppId() {
JSONObject obj = new JSONObject();
String appid = qqAppId;
if (!StringUtils.isEmpty(appid)) {
obj.put("appid", appid);
}
return ResponseEntity.ok(obj);
}
/**
* 获取微信开放平台创建的网站应用appid
*/
@GetMapping(value = "/uaa/getWechatAppId")
public ResponseEntity<JSONObject> getWechatAppId() {
JSONObject obj = new JSONObject();
String appid = wechatAppId;
if (!StringUtils.isEmpty(appid)) {
obj.put("appid", appid);
}
return ResponseEntity.ok(obj);
}
/**
* 获取钉钉开放平台创建的网站应用appid
*/
@GetMapping(value = "/uaa/getDingtalkAppId")
public ResponseEntity<JSONObject> getDingtalkAppId() {
JSONObject obj = new JSONObject();
String appid = dingTalkAppId;
if (!StringUtils.isEmpty(appid)) {
obj.put("appid", appid);
}
return ResponseEntity.ok(obj);
}
/**
* 根据code查QQ用户
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/queryQQUserByCode")
public ResponseEntity<JSONObject> queryQQUserByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserRegisterResource", "");
// 通过code获取qq用户信息
String openid = "";
JSONObject returnObj = userRegisterService.requestQQUserByCode(code, qqRedirectUri, qqAppId, qqAppKey);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
openid = returnObj.getString("openid");
String nickname = returnObj.getString("nickname");
// 将用户信息存到缓存,供当前人后续请求使用
Avatar = openid;
Nickname = nickname;
// 返回用户信息
object.put("avatar", openid);
object.put("nickname", nickname);
}
//根据openid查用户
String sql = "select * from ibzuser where avatar=#{et.avatar}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("avatar", openid);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0) {
IBZUSER ibzuser = select.get(0).toJavaObject(IBZUSER.class);
object.put("ibzuser", ibzuser);
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
/**
* 根据code查微信用户
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/queryWechatUserByCode")
public ResponseEntity<JSONObject> queryWechatUserByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserRegisterResource", "");
// 通过code获取微信用户信息
String openid = "";
JSONObject returnObj = userRegisterService.requestWechatUserByCode(code, null, wechatAppId, wechatappsecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
openid = returnObj.getString("openid");
String nickname = returnObj.getString("nickname");
// 将用户信息存到缓存,供当前人后续请求使用
Avatar = openid;
Nickname = nickname;
// 返回用户信息
object.put("avatar", openid);
object.put("nickname", nickname);
}
//根据openid查用户
String sql = "select * from ibzuser where avatar=#{et.avatar}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("avatar", openid);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0) {
IBZUSER ibzuser = select.get(0).toJavaObject(IBZUSER.class);
object.put("ibzuser", ibzuser);
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
/**
* 根据code查钉钉用户
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/queryDingtalkUserByCode")
public ResponseEntity<JSONObject> queryDingtalkUserByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserRegisterResource", "");
// 通过code获取钉钉用户信息
String openid = "";
long currentTimeMillis = System.currentTimeMillis();
JSONObject returnObj = userRegisterService.requestDingtalkUserByCode(code, currentTimeMillis, dingTalkAppId, dingTalkAppSecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
openid = returnObj.getString("openid");
String nickname = returnObj.getString("nick");
// 将用户信息存到缓存,供当前人后续请求使用
Avatar = openid;
Nickname = nickname;
// 返回用户信息
object.put("avatar", openid);
object.put("nickname", nickname);
}
//根据openid查用户
String sql = "select * from ibzuser where avatar=#{et.avatar}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("avatar", openid);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0) {
IBZUSER ibzuser = select.get(0).toJavaObject(IBZUSER.class);
object.put("ibzuser", ibzuser);
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
/**
* 根据code获取微信用户信息
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/getWechatUserInfoByCode")
public ResponseEntity<JSONObject> getWechatUserInfoByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserRegisterResource", "");
// 通过code获取微信用户信息
String openid = Avatar;
String nickname = Nickname;
object.put("avatar", openid);
object.put("nickname", nickname);
return ResponseEntity.ok().body(object);
}
/**
* 根据openId查用户
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/queryUserByOpenId")
public ResponseEntity<JSONObject> queryUserByOpenId(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String openId = param.getString("openId");
if (StringUtils.isEmpty(openId))
throw new BadRequestAlertException("openId为空", "UserRegisterResource", "");
// 根据openid查用户
String sql = "select * from ibzuser where avatar=#{et.avatar}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("avatar", openId);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0) {
// 将查询到的ibzuser对象返回
IBZUSER ibzuser = select.get(0).toJavaObject(IBZUSER.class);
object.put("ibzuser", ibzuser);
}
return ResponseEntity.ok().body(object);
}
/**
* 响应微信公众测试号发送的Token验证
*
* @param signature  微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
* @param timestamp  时间戳
* @param nonce  随机数
* @param echostr  随机字符串
* @param response 响应对象
* @throws IOException
* @throws NoSuchAlgorithmException
*/
@GetMapping(value = "/uaa/responseTokenToWeiXin")
public void responseTokenToWeiXin(String signature, String timestamp, String nonce, String echostr, HttpServletResponse response) throws IOException, NoSuchAlgorithmException {
// 将token、timestamp、nonce三个参数进行字典序排序
System.out.println("signature:" + signature);
System.out.println("timestamp:" + timestamp);
System.out.println("nonce:" + nonce);
System.out.println("echostr:" + echostr);
System.out.println("TOKEN:" + TOKEN);
String[] params = new String[]{TOKEN, timestamp, nonce};
Arrays.sort(params);
// 将三个参数字符串拼接成一个字符串进行sha1加密
String clearText = params[0] + params[1] + params[2];
String algorithm = "SHA-1";
String sign = new String(org.apache.commons.codec.binary.Hex.encodeHex(MessageDigest.getInstance(algorithm).digest((clearText).getBytes()), true));
// 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
if (signature.equals(sign)) {
// 确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功
response.getWriter().print(echostr);
}
}
}
package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.core.uaa.extensions.service.UserWechatRegisterService;
import cn.ibizlab.core.uaa.service.ISysPSSystemService;
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.AuthenticationUser;
import cn.ibizlab.util.service.AuthenticationUserService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
@RestController
public class UserWechatRegisterResource {
@Autowired
private UserWechatRegisterService userWechatRegisterService;
@Autowired
private ISysPSSystemService iSysPSSystemService;
@Autowired
private AuthTokenUtil jwtTokenUtil;
@Autowired
@Qualifier("UAAUserService")
private AuthenticationUserService userDetailsService;
@Value("${ibiz.auth.wechat.appID:wx4a7473ac9c617f76}")
private String WechatAppid; // 微信开放平台appid
@Value("${ibiz.auth.wechat.appsecret:c2b308b9245dffa316b049840fd81a79}")
private String WechatAppsecret; // 微信开放平台appsecret
@Value("${ibiz.auth.wechat.openid:}")
private String WechatOpenid;
@Value("${ibiz.auth.wechat.nickname:}")
private String WechatNickname;
public static final String TOKEN = "weixin"; // 这里的token要和微信公众测试号网页填写的token一致
/**
* 响应微信公众测试号发送的Token验证
*
* @param signature  微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数
* @param timestamp  时间戳
* @param nonce  随机数
* @param echostr  随机字符串
* @param response 响应对象
* @throws IOException
* @throws NoSuchAlgorithmException
*/
@GetMapping(value = "/uaa/responseTokenToWeiXin")
public void responseTokenToWeiXin(String signature, String timestamp, String nonce, String echostr, HttpServletResponse response) throws IOException, NoSuchAlgorithmException {
// 将token、timestamp、nonce三个参数进行字典序排序
System.out.println("signature:" + signature);
System.out.println("timestamp:" + timestamp);
System.out.println("nonce:" + nonce);
System.out.println("echostr:" + echostr);
System.out.println("TOKEN:" + TOKEN);
String[] params = new String[]{TOKEN, timestamp, nonce};
Arrays.sort(params);
// 将三个参数字符串拼接成一个字符串进行sha1加密
String clearText = params[0] + params[1] + params[2];
String algorithm = "SHA-1";
String sign = new String(org.apache.commons.codec.binary.Hex.encodeHex(MessageDigest.getInstance(algorithm).digest((clearText).getBytes()), true));
// 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
if (signature.equals(sign)) {
// 确认此次GET请求来自微信服务器,原样返回echostr参数内容,则接入生效,成为开发者成功
response.getWriter().print(echostr);
}
}
/**
* 获取微信开放平台创建的网站应用appid
*/
@GetMapping(value = "/uaa/getWechatAppId")
public ResponseEntity<JSONObject> getWechatAppId() {
JSONObject obj = new JSONObject();
String appid = WechatAppid;
if (!StringUtils.isEmpty(appid)) {
obj.put("appid", appid);
}
return ResponseEntity.ok(obj);
}
/**
* 根据code查微信用户
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/queryWechatUserByCode")
public ResponseEntity<JSONObject> queryWechatUserByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserRegisterResource", "");
// 通过code获取微信用户信息
String openid = null;
String nickname = null;
JSONObject returnObj = userWechatRegisterService.requestWechatUserByCode(code, null, WechatAppid, WechatAppsecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
openid = returnObj.getString("openid");
nickname = returnObj.getString("nickname");
// 将用户信息存到缓存,供当前人后续请求使用
WechatOpenid = openid;
WechatNickname = nickname;
}
//根据openid查用户
String sql = "select * from ibzuser where avatar=#{et.avatar}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("avatar", openid);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0) {
IBZUSER ibzuser = select.get(0).toJavaObject(IBZUSER.class);
object.put("ibzuser", ibzuser);
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
/**
* 根据code获取微信用户信息
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/getWechatUserInfoByCode")
public ResponseEntity<JSONObject> getWechatUserInfoByCode(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
String code = param.getString("code");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("code为空", "UserRegisterResource", "");
// 通过code获取微信用户信息
String openid = WechatOpenid;
String nickname = WechatNickname;
object.put("openid", openid);
object.put("nickname", nickname);
return ResponseEntity.ok().body(object);
}
/**
* 绑定微信并注册
*
* @param param
* @return
*/
@PostMapping(value = "/uaa/bindWechatToRegister")
public ResponseEntity<JSONObject> thirdPartRegister(@RequestBody JSONObject param) {
JSONObject object = new JSONObject();
// 空校验
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");
if (StringUtils.isEmpty(openid)) {
openid = WechatOpenid;
}
String nickname = param.getString("nickname");
if (StringUtils.isEmpty(nickname)) {
nickname = WechatNickname;
}
if (StringUtils.isEmpty(loginname))
throw new BadRequestAlertException("用户名为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(password))
throw new BadRequestAlertException("密码为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(code))
throw new BadRequestAlertException("微信授权code为空", "UserWechatRegisterResource", "");
if (StringUtils.isEmpty(state))
throw new BadRequestAlertException("微信授权state为空", "UserWechatRegisterResource", "");
// 检查用户名是否已被注册
String sql = "select loginname from ibzuser where loginname=#{et.loginname}";
Map<String, Object> sqlParam = new HashMap<>();
sqlParam.put("loginname", loginname);
List<JSONObject> select = iSysPSSystemService.select(sql, sqlParam);
if (select.size() > 0)
throw new BadRequestAlertException("该用户名已被注册", "UserWechatRegisterResource", "");
IBZUSER ibzuser = new IBZUSER();
if (StringUtils.isEmpty(openid)) {
// 通过code获取微信用户信息
JSONObject returnObj = userWechatRegisterService.requestWechatUserByCode(code, state ,WechatAppid , WechatAppsecret);
if (!StringUtils.isEmpty(returnObj) && !returnObj.containsKey("errcode")) {
// 微信注册
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
ibzuser.setLoginname(loginname);
ibzuser.setUserid("wechat-" + uuid);
ibzuser.setPersonname(returnObj.getString("nickname"));
ibzuser.setAvatar(returnObj.getString("openid"));
userWechatRegisterService.toRegister(ibzuser);
object.put("ibzuser", ibzuser);
}
} else {
// 已经有微信用户信息直接注册
String uuid = UUID.randomUUID().toString();
ibzuser.setPassword(password);
ibzuser.setLoginname(loginname);
ibzuser.setUserid("wechat-" + uuid);
ibzuser.setPersonname(nickname);
ibzuser.setAvatar(openid);
userWechatRegisterService.toRegister(ibzuser);
object.put("ibzuser", ibzuser);
}
userDetailsService.resetByUsername(ibzuser.getLoginname());
AuthenticationUser user = userDetailsService.loadUserByLogin(ibzuser.getLoginname(), ibzuser.getPassword());
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);
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册