|
@@ -5,6 +5,7 @@ import cn.hutool.crypto.SecureUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -12,6 +13,8 @@ import org.apache.shiro.SecurityUtils;
|
|
|
import org.apache.shiro.session.Session;
|
|
|
import org.apache.shiro.session.mgt.eis.SessionDAO;
|
|
|
import org.apache.shiro.subject.PrincipalCollection;
|
|
|
+import org.codehaus.jackson.map.ObjectMapper;
|
|
|
+import org.codehaus.jackson.type.TypeReference;
|
|
|
import org.crazycake.shiro.RedisSessionDAO;
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
import org.jeecg.common.constant.CacheConstant;
|
|
@@ -23,6 +26,8 @@ import org.jeecg.common.system.util.JwtUtil;
|
|
|
import org.jeecg.common.system.vo.LoginUser;
|
|
|
import org.jeecg.common.util.*;
|
|
|
import org.jeecg.common.util.encryption.EncryptedString;
|
|
|
+import org.jeecg.modules.message.websocket.Chat;
|
|
|
+import org.jeecg.modules.message.websocket.WebSocket;
|
|
|
import org.jeecg.modules.system.entity.SysDepart;
|
|
|
import org.jeecg.modules.system.entity.SysUser;
|
|
|
import org.jeecg.modules.system.model.SysLoginModel;
|
|
@@ -30,8 +35,12 @@ import org.jeecg.modules.system.service.ISysDepartService;
|
|
|
import org.jeecg.modules.system.service.ISysDictService;
|
|
|
import org.jeecg.modules.system.service.ISysLogService;
|
|
|
import org.jeecg.modules.system.service.ISysUserService;
|
|
|
+import org.jeecg.modules.system.util.AES;
|
|
|
import org.jeecg.modules.system.util.RandImageUtil;
|
|
|
+import org.jeecg.modules.system.util.RestTemplateUtil;
|
|
|
import org.jeecg.modules.system.util.TokenInfo;
|
|
|
+import org.jeecg.modules.system.vo.WeixinUser;
|
|
|
+import org.jeecg.modules.system.vo.WeixinXCX;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
@@ -51,6 +60,8 @@ import java.util.*;
|
|
|
@Api(tags="用户登录")
|
|
|
@Slf4j
|
|
|
public class LoginController {
|
|
|
+ @Autowired
|
|
|
+ private RestTemplateUtil restTemplateUtil;
|
|
|
@Autowired
|
|
|
private ISysUserService sysUserService;
|
|
|
@Autowired
|
|
@@ -69,6 +80,8 @@ public class LoginController {
|
|
|
@Autowired
|
|
|
private SessionDAO sessionDAO;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private WebSocket webSocket;
|
|
|
|
|
|
|
|
|
|
|
@@ -153,26 +166,9 @@ public class LoginController {
|
|
|
//清空用户登录Token缓存
|
|
|
redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
|
|
|
//清空用户登录Shiro权限缓存
|
|
|
- //redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + sysUser.getId());
|
|
|
+ redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + sysUser.getId());
|
|
|
//清空用户的缓存信息(包括部门信息),例如sys:cache:user::<username>
|
|
|
- //redisUtil.del(String.format("%s::%s", CacheConstant.SYS_USERS_CACHE, sysUser.getUsername()));
|
|
|
- //清空session缓存
|
|
|
- List<TokenInfo> taskList = (ArrayList<TokenInfo>) redisUtil.get(sysUser.getId());
|
|
|
- if (taskList.size()>1){
|
|
|
- for (TokenInfo t:taskList){
|
|
|
- if (t.getTokenKey().equals(CommonConstant.PREFIX_USER_TOKEN +token)){
|
|
|
- taskList.remove(t);
|
|
|
- System.out.println("销毁session"+t.getSessionId());
|
|
|
- }
|
|
|
- }
|
|
|
- redisUtil.set(sysUser.getId(),taskList);
|
|
|
- }else{
|
|
|
- redisUtil.del(sysUser.getId());
|
|
|
- redisUtil.del(String.format("%s::%s", CacheConstant.SYS_USERS_CACHE, sysUser.getUsername()));
|
|
|
- redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + sysUser.getId());
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
+ redisUtil.del(String.format("%s::%s", CacheConstant.SYS_USERS_CACHE, sysUser.getUsername()));
|
|
|
//调用shiro的logout
|
|
|
SecurityUtils.getSubject().logout();
|
|
|
|
|
@@ -464,7 +460,7 @@ public class LoginController {
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@RequestMapping(value = "/mLogin", method = RequestMethod.POST)
|
|
|
- public Result<JSONObject> mLogin(@RequestBody SysLoginModel sysLoginModel, HttpSession session) throws Exception {
|
|
|
+ public Result<JSONObject> mLogin(@RequestBody SysLoginModel sysLoginModel, HttpSession session,HttpServletRequest request) throws Exception {
|
|
|
Result<JSONObject> result = new Result<JSONObject>();
|
|
|
String username = sysLoginModel.getUsername();
|
|
|
String password = sysLoginModel.getPassword();
|
|
@@ -516,32 +512,6 @@ public class LoginController {
|
|
|
List<TokenInfo>list=new ArrayList<>();
|
|
|
TokenInfo tokenInfo=new TokenInfo();
|
|
|
baseCommonService.addLog("用户名: " + username + ",登录成功[移动端]!", CommonConstant.LOG_TYPE_1, null);
|
|
|
- Collection<Session> activeSessions = sessionDAO.getActiveSessions();
|
|
|
- if (!redisUtil.hasKey(sysUser.getId())){
|
|
|
- tokenInfo.setTokenKey(CommonConstant.PREFIX_USER_TOKEN + token);
|
|
|
- tokenInfo.setSessionId(session.getId());
|
|
|
-
|
|
|
- list.add(tokenInfo);
|
|
|
- redisUtil.set(sysUser.getId(),list);
|
|
|
-
|
|
|
-
|
|
|
- }else {
|
|
|
- List<TokenInfo> taskList = (ArrayList<TokenInfo>) redisUtil.get(sysUser.getId());
|
|
|
- if (taskList.size() ==2) {
|
|
|
- TokenInfo tokenInfo1 = taskList.get(0);
|
|
|
- taskList.remove(0);
|
|
|
- redisUtil.del(tokenInfo1.getTokenKey());
|
|
|
- tokenInfo.setTokenKey(CommonConstant.PREFIX_USER_TOKEN + token);
|
|
|
- tokenInfo.setSessionId(session.getId());
|
|
|
- taskList.add(tokenInfo);
|
|
|
- redisUtil.set(sysUser.getId(), taskList);
|
|
|
- } else {
|
|
|
- tokenInfo.setTokenKey(CommonConstant.PREFIX_USER_TOKEN + token);
|
|
|
- tokenInfo.setSessionId(session.getId());
|
|
|
- taskList.add(tokenInfo);
|
|
|
- redisUtil.set(sysUser.getId(), taskList);
|
|
|
- }
|
|
|
- }
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -565,5 +535,111 @@ public class LoginController {
|
|
|
}
|
|
|
return Result.ok();
|
|
|
}
|
|
|
+ @RequestMapping(value = "/chatConter")
|
|
|
+ public List<Chat> chatConter(String chatId){
|
|
|
+ String sendUserno = chatId.split("[|]")[1];
|
|
|
+ String userid=chatId.split("[|]")[0];
|
|
|
+ List<Chat> taskList =null;
|
|
|
+ if (redisUtil.hasKey(sendUserno+userid)){
|
|
|
+ taskList=(ArrayList<Chat>)redisUtil.get(sendUserno+userid);
|
|
|
+ }else {
|
|
|
+ taskList=(ArrayList<Chat>)redisUtil.get(userid+sendUserno);
|
|
|
+ }
|
|
|
+ return taskList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 小程序登录
|
|
|
+ * @param sysLoginModel
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/xcxLogin", method = RequestMethod.POST)
|
|
|
+ public Result<JSONObject> xcxLogin(@RequestBody SysLoginModel sysLoginModel) throws Exception {
|
|
|
+ Result<JSONObject> result = new Result<JSONObject>();
|
|
|
+ String username = sysLoginModel.getUsername();
|
|
|
+ String password = sysLoginModel.getPassword();
|
|
|
+
|
|
|
+ //1. 校验用户是否有效
|
|
|
+ SysUser sysUser = sysUserService.getUserByName(username);
|
|
|
+ result = sysUserService.checkUserIsEffective(sysUser);
|
|
|
+ if(!result.isSuccess()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //2. 校验用户名或密码是否正确
|
|
|
+ String userpassword = PasswordUtil.encrypt(username, password, sysUser.getSalt());
|
|
|
+ String syspassword = sysUser.getPassword();
|
|
|
+ if (!syspassword.equals(userpassword)) {
|
|
|
+ result.error500("用户名或密码错误");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ String orgCode = sysUser.getOrgCode();
|
|
|
+ if(oConvertUtils.isEmpty(orgCode)) {
|
|
|
+ //如果当前用户无选择部门 查看部门关联信息
|
|
|
+ List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
|
|
|
+ if (departs == null || departs.size() == 0) {
|
|
|
+ result.error500("用户暂未归属部门,不可登录!");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ orgCode = departs.get(0).getOrgCode();
|
|
|
+ sysUser.setOrgCode(orgCode);
|
|
|
+ this.sysUserService.updateUserDepart(username, orgCode);
|
|
|
+ }
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ //用户登录信息
|
|
|
+ obj.put("userInfo", sysUser);
|
|
|
+
|
|
|
+ // 生成token
|
|
|
+ String token = JwtUtil.sign(username, syspassword);
|
|
|
+ // 设置超时时间
|
|
|
+ redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
|
|
|
+ redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
|
|
|
+
|
|
|
+ //token 信息
|
|
|
+ obj.put("token", token);
|
|
|
+ result.setResult(obj);
|
|
|
+ result.setSuccess(true);
|
|
|
+ result.setCode(200);
|
|
|
+ baseCommonService.addLog("用户名: " + username + ",登录成功[小程序]!", CommonConstant.LOG_TYPE_1, null);
|
|
|
+
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 小程序微信快捷登录
|
|
|
+ * @param sysLoginModel
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/kjLogin", method = RequestMethod.POST)
|
|
|
+ public Result<JSONObject> kjLogin(@RequestBody WeixinXCX weixinXCX) throws Exception {
|
|
|
+ Result<JSONObject> result = new Result<JSONObject>();
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ String tokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
|
|
|
+ tokenUrl = tokenUrl.replace("APPID","wx4a24204875735130").replace("SECRET","09316748df749be5f0232c0b280a0f8f").replace("JSCODE", weixinXCX.getCode());
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ WeixinXCX weixinXCX1 = mapper.readValue(restTemplateUtil.doGet(tokenUrl).getBody(), new TypeReference<WeixinXCX>() {
|
|
|
+ });
|
|
|
+ String phoneNumber = null;
|
|
|
+
|
|
|
+ cn.hutool.json.JSONObject userencryptedData = AES.wxDecrypt(weixinXCX.getEncryptedData(),weixinXCX1.getSession_key(),weixinXCX.getIv());
|
|
|
+ if (StringUtils.isNotBlank((String) userencryptedData.get("phoneNumber"))){
|
|
|
+ phoneNumber = (String) userencryptedData.get("phoneNumber");
|
|
|
+ }
|
|
|
+ SysUser sysUser = sysUserService.getUserByPhone(phoneNumber);
|
|
|
+ result = sysUserService.checkUserIsEffective(sysUser);
|
|
|
+ if(!result.isSuccess()) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ userInfo(sysUser, result);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
|
|
|
}
|