单点登录
This commit is contained in:
+3
-51
@@ -56,8 +56,6 @@ public class LoginController {
|
|||||||
private RedisUtil redisUtil;
|
private RedisUtil redisUtil;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ISysDepartService sysDepartService;
|
private ISysDepartService sysDepartService;
|
||||||
@Autowired
|
|
||||||
private ISysDictService sysDictService;
|
|
||||||
@Resource
|
@Resource
|
||||||
private BaseCommonService baseCommonService;
|
private BaseCommonService baseCommonService;
|
||||||
@Resource
|
@Resource
|
||||||
@@ -160,7 +158,7 @@ public class LoginController {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
//用户登录信息
|
//用户登录信息
|
||||||
userInfo(sysUser, result);
|
sysUserService.userInfo(sysUser, result);
|
||||||
//update-begin--Author:wangshuai Date:20200714 for:登录日志没有记录人员
|
//update-begin--Author:wangshuai Date:20200714 for:登录日志没有记录人员
|
||||||
LoginUser loginUser = new LoginUser();
|
LoginUser loginUser = new LoginUser();
|
||||||
BeanUtils.copyProperties(sysUser, loginUser);
|
BeanUtils.copyProperties(sysUser, loginUser);
|
||||||
@@ -413,7 +411,7 @@ public class LoginController {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
//用户信息
|
//用户信息
|
||||||
userInfo(sysUser, result);
|
sysUserService.userInfo(sysUser, result);
|
||||||
//添加日志
|
//添加日志
|
||||||
baseCommonService.addLog("账号: " + sysUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
baseCommonService.addLog("账号: " + sysUser.getUsername() + ",登录成功!", CommonConstant.LOG_TYPE_1, null);
|
||||||
|
|
||||||
@@ -421,50 +419,7 @@ public class LoginController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户信息
|
|
||||||
*
|
|
||||||
* @param sysUser
|
|
||||||
* @param result
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
|
|
||||||
String syspassword = sysUser.getPassword();
|
|
||||||
String username = sysUser.getUsername();
|
|
||||||
// 生成token
|
|
||||||
String token = JwtUtil.sign(username, syspassword);
|
|
||||||
// 设置token缓存有效时间
|
|
||||||
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
|
|
||||||
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
|
|
||||||
|
|
||||||
// 获取用户部门信息
|
|
||||||
JSONObject obj = new JSONObject();
|
|
||||||
List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
|
|
||||||
obj.put("departs", departs);
|
|
||||||
if (departs == null || departs.size() == 0) {
|
|
||||||
obj.put("multi_depart", 0);
|
|
||||||
} else if (departs.size() == 1) {
|
|
||||||
sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
|
|
||||||
obj.put("multi_depart", 1);
|
|
||||||
} else {
|
|
||||||
//查询当前是否有登录部门
|
|
||||||
// update-begin--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
|
|
||||||
SysUser sysUserById = sysUserService.getById(sysUser.getId());
|
|
||||||
if (oConvertUtils.isEmpty(sysUserById.getOrgCode())) {
|
|
||||||
sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
|
|
||||||
}
|
|
||||||
// update-end--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
|
|
||||||
obj.put("multi_depart", 2);
|
|
||||||
}
|
|
||||||
sysUser.setPhone(PasswordUtil.decrypt(sysUser.getPhone()));
|
|
||||||
sysUser.setEmail(PasswordUtil.decrypt(sysUser.getEmail()));
|
|
||||||
obj.put("token", token);
|
|
||||||
obj.put("userInfo", sysUser);
|
|
||||||
obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
|
|
||||||
result.setResult(obj);
|
|
||||||
result.success("登录成功");
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取加密字符串
|
* 获取加密字符串
|
||||||
@@ -647,10 +602,7 @@ public class LoginController {
|
|||||||
@ApiOperation(value = "单点登录")
|
@ApiOperation(value = "单点登录")
|
||||||
@PostMapping(value = "/singleSignOn")
|
@PostMapping(value = "/singleSignOn")
|
||||||
public Result<?> singleSignOn(@RequestParam(name="code") String code) {
|
public Result<?> singleSignOn(@RequestParam(name="code") String code) {
|
||||||
Result<JSONObject> result = new Result<>();
|
return sysUserService.singleSignOn(code);
|
||||||
|
|
||||||
sysUserService.singleSignOn(code);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-1
@@ -4,6 +4,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
@@ -265,6 +266,14 @@ public interface ISysUserService extends IService<SysUser> {
|
|||||||
PayContactsManagement getPayContactsManagementByPhone(String phone);
|
PayContactsManagement getPayContactsManagementByPhone(String phone);
|
||||||
|
|
||||||
//单点登录
|
//单点登录
|
||||||
void singleSignOn(String code);
|
Result<JSONObject> singleSignOn(String code);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户信息
|
||||||
|
*
|
||||||
|
* @param sysUser
|
||||||
|
* @param result
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result);
|
||||||
}
|
}
|
||||||
|
|||||||
+128
-8
@@ -1,15 +1,19 @@
|
|||||||
package com.jero.modules.system.service.impl;
|
package com.jero.modules.system.service.impl;
|
||||||
|
|
||||||
import cn.hutool.http.HttpUtil;
|
import cn.hutool.http.HttpUtil;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.jero.common.exception.JeroBootException;
|
import com.jero.common.exception.JeroBootException;
|
||||||
|
import com.jero.common.system.util.JwtUtil;
|
||||||
import com.jero.common.util.*;
|
import com.jero.common.util.*;
|
||||||
import com.jero.modules.pay.company.entity.PayContactsManagement;
|
import com.jero.modules.pay.company.entity.PayContactsManagement;
|
||||||
|
import com.jero.modules.system.model.SysLoginModel;
|
||||||
import com.jero.modules.system.model.SysLoginVO;
|
import com.jero.modules.system.model.SysLoginVO;
|
||||||
|
import com.jero.modules.system.service.*;
|
||||||
import com.jero.modules.system.vo.SysUserVo;
|
import com.jero.modules.system.vo.SysUserVo;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import com.jero.common.api.vo.Result;
|
import com.jero.common.api.vo.Result;
|
||||||
@@ -22,7 +26,6 @@ import com.jero.common.system.vo.SysUserCacheInfo;
|
|||||||
import com.jero.modules.system.entity.*;
|
import com.jero.modules.system.entity.*;
|
||||||
import com.jero.modules.system.mapper.*;
|
import com.jero.modules.system.mapper.*;
|
||||||
import com.jero.modules.system.model.SysUserSysDepartModel;
|
import com.jero.modules.system.model.SysUserSysDepartModel;
|
||||||
import com.jero.modules.system.service.ISysUserService;
|
|
||||||
import com.jero.modules.system.vo.SysUserDepVo;
|
import com.jero.modules.system.vo.SysUserDepVo;
|
||||||
import org.apache.commons.lang.StringUtils;
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
@@ -33,11 +36,14 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static com.jero.modules.system.controller.LoginController.RETRY_LOGIN_PREFIX;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* 用户表 服务实现类
|
* 用户表 服务实现类
|
||||||
@@ -72,6 +78,14 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||||||
private BaseCommonService baseCommonService;
|
private BaseCommonService baseCommonService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private RedisUtil redisUtil;
|
private RedisUtil redisUtil;
|
||||||
|
@Autowired
|
||||||
|
private ISysDictService sysDictService;
|
||||||
|
@Autowired
|
||||||
|
private ISysDepartService sysDepartService;
|
||||||
|
@Resource
|
||||||
|
private ISysRoleService sysRoleService;
|
||||||
|
@Resource
|
||||||
|
private ISysUserRoleService sysUserRoleService;
|
||||||
@Value("${defaultPassword}")
|
@Value("${defaultPassword}")
|
||||||
private String defaultPassword;
|
private String defaultPassword;
|
||||||
@Value("${singleSignON.client_id}")
|
@Value("${singleSignON.client_id}")
|
||||||
@@ -628,13 +642,119 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||||||
}
|
}
|
||||||
|
|
||||||
//单点登录
|
//单点登录
|
||||||
public void singleSignOn(String code){
|
public Result<JSONObject> singleSignOn(String code){
|
||||||
HashMap<String, Object> paramMap = new HashMap<>();
|
//根据授权码获取access_token
|
||||||
paramMap.put("client_id", client_id);
|
HashMap<String, Object> tokenParam = new HashMap<>();
|
||||||
paramMap.put("client_secret", client_secret);
|
tokenParam.put("client_id", client_id);
|
||||||
paramMap.put("code", 1);
|
tokenParam.put("client_secret", client_secret);
|
||||||
paramMap.put("grant_type", "authorization_code");
|
tokenParam.put("code", 1);
|
||||||
|
tokenParam.put("grant_type", "authorization_code");
|
||||||
|
|
||||||
String token= HttpUtil.post("https://idmtest.catarc.ac.cn/bam-protocol-service/oauth2/getToken", paramMap);
|
String tokenRes= HttpUtil.post("https://idmtest.catarc.ac.cn/bam-protocol-service/oauth2/getToken", tokenParam);
|
||||||
|
JSONObject tokenJson = JSONObject.parseObject(tokenRes);
|
||||||
|
String access_token = tokenJson.getString("access_token");
|
||||||
|
|
||||||
|
//根据access_token获取用户信息
|
||||||
|
HashMap<String, Object> userInfoParam = new HashMap<>();
|
||||||
|
userInfoParam.put("access_token", access_token);
|
||||||
|
userInfoParam.put("client_id", client_id);
|
||||||
|
|
||||||
|
String userInfoRes= HttpUtil.get("https://idmtest.catarc.ac.cn/bam-protocol-service/oauth2/getUserInfo", userInfoParam);
|
||||||
|
|
||||||
|
//根据工号登录
|
||||||
|
return loginByUserNo("01433");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据工号登录
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Result<JSONObject> loginByUserNo(String workNo) {
|
||||||
|
Result<JSONObject> result = new Result<>();
|
||||||
|
|
||||||
|
//1. 校验用户是否有效
|
||||||
|
//update-begin-author:wangshuai date:20200601 for: 登录代码验证用户是否注销bug,if条件永远为false
|
||||||
|
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
|
||||||
|
queryWrapper.eq(SysUser::getWorkNo, workNo);
|
||||||
|
SysUser sysUser = getOne(queryWrapper);
|
||||||
|
//update-end-author:wangshuai date:20200601 for: 登录代码验证用户是否注销bug,if条件永远为false
|
||||||
|
result= (Result<JSONObject>) checkUserIsEffective(sysUser);
|
||||||
|
if (!result.isSuccess()) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//用户登录信息
|
||||||
|
userInfo(sysUser, result);
|
||||||
|
//update-begin--Author:wangshuai Date:20200714 for:登录日志没有记录人员
|
||||||
|
LoginUser loginUser = new LoginUser();
|
||||||
|
BeanUtils.copyProperties(sysUser, loginUser);
|
||||||
|
// 新增用户角色code
|
||||||
|
LambdaQueryWrapper<SysUserRole> querySysUserRole = new LambdaQueryWrapper();
|
||||||
|
querySysUserRole.eq(SysUserRole::getUserId,sysUser.getId());
|
||||||
|
List<SysUserRole> listSysUserRole = sysUserRoleService.list(querySysUserRole);
|
||||||
|
if(!org.apache.commons.collections.CollectionUtils.isEmpty(listSysUserRole)){
|
||||||
|
List<String> listStr = listSysUserRole.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
|
||||||
|
LambdaQueryWrapper<SysRole> querySysRole = new LambdaQueryWrapper();
|
||||||
|
querySysRole.in(SysRole::getId,listStr);
|
||||||
|
List<SysRole> listSysRole = sysRoleService.list(querySysRole);
|
||||||
|
if(!org.apache.commons.collections.CollectionUtils.isEmpty(listSysRole)){
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
listSysRole.forEach(p->{
|
||||||
|
sb.append(p.getRoleCode());
|
||||||
|
if(listSysRole.indexOf(p) != (listSysRole.size() - 1)){
|
||||||
|
sb.append(",");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
loginUser.setRoleCode(sb.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
baseCommonService.addLog("账号: " + sysUser.getUsername() + ",第三方登录成功!", CommonConstant.LOG_TYPE_1, null, loginUser);
|
||||||
|
//update-end--Author:wangshuai Date:20200714 for:登录日志没有记录人员
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户信息
|
||||||
|
*
|
||||||
|
* @param sysUser
|
||||||
|
* @param result
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Result<JSONObject> userInfo(SysUser sysUser, Result<JSONObject> result) {
|
||||||
|
String syspassword = sysUser.getPassword();
|
||||||
|
String username = sysUser.getUsername();
|
||||||
|
// 生成token
|
||||||
|
String token = JwtUtil.sign(username, syspassword);
|
||||||
|
// 设置token缓存有效时间
|
||||||
|
redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
|
||||||
|
redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
|
||||||
|
|
||||||
|
// 获取用户部门信息
|
||||||
|
JSONObject obj = new JSONObject();
|
||||||
|
List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
|
||||||
|
obj.put("departs", departs);
|
||||||
|
if (departs == null || departs.size() == 0) {
|
||||||
|
obj.put("multi_depart", 0);
|
||||||
|
} else if (departs.size() == 1) {
|
||||||
|
updateUserDepart(username, departs.get(0).getOrgCode());
|
||||||
|
obj.put("multi_depart", 1);
|
||||||
|
} else {
|
||||||
|
//查询当前是否有登录部门
|
||||||
|
// update-begin--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
|
||||||
|
SysUser sysUserById = getById(sysUser.getId());
|
||||||
|
if (oConvertUtils.isEmpty(sysUserById.getOrgCode())) {
|
||||||
|
updateUserDepart(username, departs.get(0).getOrgCode());
|
||||||
|
}
|
||||||
|
// update-end--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
|
||||||
|
obj.put("multi_depart", 2);
|
||||||
|
}
|
||||||
|
sysUser.setPhone(PasswordUtil.decrypt(sysUser.getPhone()));
|
||||||
|
sysUser.setEmail(PasswordUtil.decrypt(sysUser.getEmail()));
|
||||||
|
obj.put("token", token);
|
||||||
|
obj.put("userInfo", sysUser);
|
||||||
|
obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
|
||||||
|
result.setResult(obj);
|
||||||
|
result.success("登录成功");
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user