wangliang 1 anno fa
parent
commit
9d31c0cd0d

+ 1 - 35
sop-business/src/main/java/com/qmth/sop/business/bean/result/LoginResult.java

@@ -8,10 +8,8 @@ import com.qmth.sop.common.enums.AppSourceEnum;
 import com.qmth.sop.common.util.ServletUtil;
 import com.qmth.sop.common.util.ServletUtil;
 import io.swagger.annotations.ApiModelProperty;
 import io.swagger.annotations.ApiModelProperty;
 
 
-import javax.management.relation.RoleResult;
 import java.io.Serializable;
 import java.io.Serializable;
 import java.util.Arrays;
 import java.util.Arrays;
-import java.util.List;
 import java.util.Set;
 import java.util.Set;
 
 
 /**
 /**
@@ -51,12 +49,6 @@ public class LoginResult implements Serializable {
     @ApiModelProperty(value = "应用来源")
     @ApiModelProperty(value = "应用来源")
     AppSourceEnum appSource;
     AppSourceEnum appSource;
 
 
-    @ApiModelProperty(value = "密码修改次数")
-    Integer pwdCount;
-
-    @ApiModelProperty(name = "返回url")
-    String returnUrl;
-
     @ApiModelProperty(name = "版本号")
     @ApiModelProperty(name = "版本号")
     String version;
     String version;
 
 
@@ -68,14 +60,6 @@ public class LoginResult implements Serializable {
         this.version = version;
         this.version = version;
     }
     }
 
 
-    public String getReturnUrl() {
-        return returnUrl;
-    }
-
-    public void setReturnUrl(String returnUrl) {
-        this.returnUrl = returnUrl;
-    }
-
     public LoginResult() {
     public LoginResult() {
 
 
     }
     }
@@ -89,18 +73,8 @@ public class LoginResult implements Serializable {
         this.accessToken = accessToken;
         this.accessToken = accessToken;
         this.roleList = roleList;
         this.roleList = roleList;
         this.appSource = appSource;
         this.appSource = appSource;
-        this.pwdCount = sysUser.getPwdCount();
-        this.version = version;
-    }
-
-    public LoginResult(SysUser sysUser, String sessionId, String accessToken, OrgNativeBean orgInfo, AppSourceEnum appSource, String version) {
-        this.loginName = sysUser.getLoginName();
-        this.realName = sysUser.getRealName();
-        this.sessionId = sessionId;
-        this.accessToken = accessToken;
-        this.orgInfo = orgInfo;
-        this.appSource = appSource;
         this.version = version;
         this.version = version;
+        this.time = System.currentTimeMillis();
     }
     }
 
 
     public AppSourceEnum getAppSource() {
     public AppSourceEnum getAppSource() {
@@ -175,14 +149,6 @@ public class LoginResult implements Serializable {
         this.time = time;
         this.time = time;
     }
     }
 
 
-    public Integer getPwdCount() {
-        return pwdCount;
-    }
-
-    public void setPwdCount(Integer pwdCount) {
-        this.pwdCount = pwdCount;
-    }
-
     public class OrgNativeBean implements Serializable {
     public class OrgNativeBean implements Serializable {
 
 
         @ApiModelProperty(value = "id")
         @ApiModelProperty(value = "id")

+ 36 - 44
sop-business/src/main/java/com/qmth/sop/business/cache/impl/CommonCacheServiceImpl.java

@@ -21,10 +21,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.CollectionUtils;
 
 
 import javax.annotation.Resource;
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Objects;
-import java.util.Set;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Collectors;
 
 
 /**
 /**
@@ -285,50 +282,45 @@ public class CommonCacheServiceImpl implements CommonCacheService {
      */
      */
     @Override
     @Override
     public AuthBean userAuthCache(Long userId) {
     public AuthBean userAuthCache(Long userId) {
-        AuthBean authBean = null;
-        try {
-            SysUser user = this.userCache(userId);
-            if (Objects.isNull(user)) {
-                throw ExceptionResultEnum.USER_NO_DATA.exception();
-            }
-            //查询用户角色和权限
-            List<SysUserRole> sysUserRoleList = this.userRolePrivilegeCache(user.getId());
-            if (Objects.nonNull(sysUserRoleList) && sysUserRoleList.size() > 0) {
-                Set<Long> roleIds = sysUserRoleList.stream().map(s -> s.getRoleId()).collect(Collectors.toSet());
-                List<SysRolePrivilege> sysRolePrivilegeList = new ArrayList<>();
-                for (Long l : roleIds) {
-                    sysRolePrivilegeList.addAll(this.rolePrivilegeCache(l));
-                }
-                Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
-                QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
-                sysRoleQueryWrapper.lambda().in(SysRole::getId, roleIds)
-                        .eq(SysRole::getEnable, true);
-                List<SysRole> sysRoleList = sysRoleService.list(sysRoleQueryWrapper);
-                int count = Objects.nonNull(sysRoleList) && sysRoleList.size() > 0 ? (int) sysRoleList.stream().filter(s -> Objects.equals(s.getName(), RoleTypeEnum.ADMIN.getDesc())).count() : 0;
-                QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
-                if (count > 0) {//超级系统管理员
-                    sysPrivilegeQueryWrapper.lambda()
-                            .eq(SysPrivilege::getType, PrivilegeEnum.URL)
-                            .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
-                    List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
-                    authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
-                } else {
-                    SysOrg org = Objects.nonNull(user.getOrgId()) ? this.orgCache(user.getOrgId()) : null;
-                    List<SysPrivilege> sysPrivilegeList = new ArrayList<>();
-                    if (privilegeIds.size() > 0) {
-                        sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds)
-                                .eq(SysPrivilege::getType, PrivilegeEnum.URL)
-                                .eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
-                        sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
+        AuthBean authBean = (AuthBean) cacheService.get(SystemConstant.USER_OAUTH_CACHE, String.valueOf(userId));
+        if (Objects.isNull(authBean)) {
+            try {
+                SysUser user = this.userCache(userId);
+                Optional.ofNullable(user).orElseThrow(() -> ExceptionResultEnum.USER_NO_DATA.exception());
+                //查询用户角色和权限
+                List<SysUserRole> sysUserRoleList = this.userRolePrivilegeCache(user.getId());
+                if (Objects.nonNull(sysUserRoleList) && sysUserRoleList.size() > 0) {
+                    Set<Long> roleIds = sysUserRoleList.stream().map(s -> s.getRoleId()).collect(Collectors.toSet());
+                    List<SysRolePrivilege> sysRolePrivilegeList = new ArrayList<>();
+                    for (Long l : roleIds) {
+                        sysRolePrivilegeList.addAll(this.rolePrivilegeCache(l));
                     }
                     }
-                    authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), org);
+                    Set<Long> privilegeIds = sysRolePrivilegeList.stream().map(s -> s.getPrivilegeId()).collect(Collectors.toSet());
+                    List<SysRole> sysRoleList = sysRoleService.list(new QueryWrapper<SysRole>().lambda().in(SysRole::getId, roleIds).eq(SysRole::getEnable, true));
+                    int count = Objects.nonNull(sysRoleList) && sysRoleList.size() > 0 ? (int) sysRoleList.stream().filter(s -> Objects.equals(s.getName(), RoleTypeEnum.ADMIN.getDesc())).count() : 0;
+                    QueryWrapper<SysPrivilege> sysPrivilegeQueryWrapper = new QueryWrapper<>();
+                    if (count > 0) {//超级系统管理员
+                        sysPrivilegeQueryWrapper.lambda().eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
+                        List<SysPrivilege> sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
+                        authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()));
+                    } else {
+                        SysOrg org = Objects.nonNull(user.getOrgId()) ? this.orgCache(user.getOrgId()) : null;
+                        List<SysPrivilege> sysPrivilegeList = new ArrayList<>();
+                        if (privilegeIds.size() > 0) {
+                            sysPrivilegeQueryWrapper.lambda().in(SysPrivilege::getId, privilegeIds).eq(SysPrivilege::getType, PrivilegeEnum.URL).eq(SysPrivilege::getProperty, PrivilegePropertyEnum.AUTH);
+                            sysPrivilegeList = sysPrivilegeService.list(sysPrivilegeQueryWrapper);
+                        }
+                        authBean = new AuthBean(sysRoleList, sysPrivilegeList.stream().map(s -> s.getUrl()).collect(Collectors.toSet()), org);
+                    }
+                }
+                if (Objects.nonNull(authBean)) {
+                    cacheService.put(SystemConstant.USER_OAUTH_CACHE, String.valueOf(userId), authBean);
                 }
                 }
+            } catch (Exception e) {
+                log.error(SystemConstant.LOG_ERROR, e);
+                throw ExceptionResultEnum.ERROR.exception("添加用户鉴权缓存失败");
             }
             }
-        } catch (Exception e) {
-            log.error(SystemConstant.LOG_ERROR, e);
-            throw ExceptionResultEnum.ERROR.exception("添加用户鉴权缓存失败");
         }
         }
-        cacheService.put(SystemConstant.USER_OAUTH_CACHE, String.valueOf(userId), authBean);
         return authBean;
         return authBean;
     }
     }
 
 

+ 12 - 11
sop-business/src/main/java/com/qmth/sop/business/service/impl/SysUserServiceImpl.java

@@ -6,6 +6,7 @@ import com.qmth.sop.business.bean.auth.AuthBean;
 import com.qmth.sop.business.bean.auth.ExpireTimeBean;
 import com.qmth.sop.business.bean.auth.ExpireTimeBean;
 import com.qmth.sop.business.bean.result.LoginResult;
 import com.qmth.sop.business.bean.result.LoginResult;
 import com.qmth.sop.business.cache.CommonCacheService;
 import com.qmth.sop.business.cache.CommonCacheService;
+import com.qmth.sop.business.entity.SysRole;
 import com.qmth.sop.business.entity.SysUser;
 import com.qmth.sop.business.entity.SysUser;
 import com.qmth.sop.business.entity.TBSession;
 import com.qmth.sop.business.entity.TBSession;
 import com.qmth.sop.business.mapper.SysUserMapper;
 import com.qmth.sop.business.mapper.SysUserMapper;
@@ -15,6 +16,7 @@ import com.qmth.sop.business.util.AuthUtil;
 import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.contant.SystemConstant;
 import com.qmth.sop.common.enums.AppSourceEnum;
 import com.qmth.sop.common.enums.AppSourceEnum;
 import com.qmth.sop.common.enums.ExceptionResultEnum;
 import com.qmth.sop.common.enums.ExceptionResultEnum;
+import com.qmth.sop.common.util.IpUtil;
 import com.qmth.sop.common.util.ServletUtil;
 import com.qmth.sop.common.util.ServletUtil;
 import com.qmth.sop.common.util.SessionUtil;
 import com.qmth.sop.common.util.SessionUtil;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -46,7 +48,6 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
         if (!sysUser.getEnable()) {
         if (!sysUser.getEnable()) {
             throw ExceptionResultEnum.USER_ENABLE.exception();
             throw ExceptionResultEnum.USER_ENABLE.exception();
         }
         }
-
         Platform platform = ServletUtil.getRequestPlatform();
         Platform platform = ServletUtil.getRequestPlatform();
         String deviceId = ServletUtil.getRequestDeviceId();
         String deviceId = ServletUtil.getRequestDeviceId();
         //添加用户鉴权缓存
         //添加用户鉴权缓存
@@ -55,24 +56,24 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
 
 
         //生成token
         //生成token
         String token = SystemConstant.getNanoId();
         String token = SystemConstant.getNanoId();
-        commonCacheService.userCache(sysUser.getId());
         //添加用户会话缓存
         //添加用户会话缓存
-        Set<String> roleNamesSet = new HashSet<>(), roleTypes = new HashSet<>();
-        List<String> roleNames = new ArrayList<>();
-        roleNames.addAll(roleNamesSet);
-        Collections.sort(roleNames);
-
-        String sessionId = SessionUtil.digest(sysUser.getId(), Math.abs(roleNames.toString().hashCode()), platform.name());
+        Set<String> roleNamesSet = new LinkedHashSet<>(), roleTypes = new HashSet<>();
+        for (SysRole s : authBean.getRoleList()) {
+            roleNamesSet.add(s.getName());
+            if (Objects.nonNull(s.getType())) {
+                roleTypes.add(s.getType().name());
+            }
+        }
+        String sessionId = SessionUtil.digest(sysUser.getId(), Math.abs(roleNamesSet.toString().hashCode()), platform.name());
         ExpireTimeBean expireTime = AuthUtil.getExpireTime(platform);
         ExpireTimeBean expireTime = AuthUtil.getExpireTime(platform);
-        TBSession tbSession = new TBSession(sessionId, String.valueOf(sysUser.getId()), roleNames.toString(),
-                platform.name(), platform.name(), deviceId, ServletUtil.getRequest().getLocalAddr(), token,
+        TBSession tbSession = new TBSession(sessionId, String.valueOf(sysUser.getId()), roleNamesSet.toString(),
+                platform.name(), platform.name(), deviceId, IpUtil.getRemoteIp(), token,
                 expireTime.getDate().getTime(), appSource);
                 expireTime.getDate().getTime(), appSource);
         tbSessionService.saveOrUpdate(tbSession);
         tbSessionService.saveOrUpdate(tbSession);
         commonCacheService.setUserSession(sessionId, tbSession);
         commonCacheService.setUserSession(sessionId, tbSession);
 
 
         LoginResult loginResult = new LoginResult(sysUser, sessionId, token, Objects.nonNull(roleTypes) && roleTypes.size() > 0 ? roleTypes : roleNamesSet, appSource, SystemConstant.VERSION_VALUE);
         LoginResult loginResult = new LoginResult(sysUser, sessionId, token, Objects.nonNull(roleTypes) && roleTypes.size() > 0 ? roleTypes : roleNamesSet, appSource, SystemConstant.VERSION_VALUE);
         loginResult.setOrgInfo(Objects.nonNull(authBean.getOrg()) ? loginResult.new OrgNativeBean(authBean.getOrg()) : null);
         loginResult.setOrgInfo(Objects.nonNull(authBean.getOrg()) ? loginResult.new OrgNativeBean(authBean.getOrg()) : null);
-        loginResult.setTime(System.currentTimeMillis());
         return loginResult;
         return loginResult;
     }
     }
 }
 }

+ 72 - 0
sop-common/src/main/java/com/qmth/sop/common/util/IpUtil.java

@@ -0,0 +1,72 @@
+package com.qmth.sop.common.util;
+
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.Objects;
+
+/**
+ * @Description: ip util
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2022/9/2
+ */
+public class IpUtil {
+    private final static Logger log = LoggerFactory.getLogger(IpUtil.class);
+
+    /**
+     * 获取过程ip(默认不包含代理ip)
+     *
+     * @return
+     */
+    public static String getRemoteIp() {
+        HttpServletRequest request = ServletUtil.getRequest();
+        if (Objects.nonNull(request)) {
+            return getRemoteIp(true);
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * excludeProxyIp
+     *
+     * @param excludeProxyIp 是否排除代理ip
+     * @return
+     */
+    public static String getRemoteIp(boolean excludeProxyIp) {
+        HttpServletRequest request = ServletUtil.getRequest();
+        String ip = request.getHeader("X-Forwarded-For");
+        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getHeader("x-real-ip");
+        }
+        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getHeader("Proxy-Client-IP");
+        }
+        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getHeader("WL-Proxy-Client-IP");
+        }
+        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getHeader("HTTP_CLIENT_IP");
+        }
+        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
+        }
+        if (StringUtils.isBlank(ip) || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getRemoteAddr();
+        }
+
+        if (excludeProxyIp) {
+            //对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
+            if (ip != null && ip.length() > 15) { //"***.***.***.***".length() = 15
+                if (ip.indexOf(",") > 0) {
+                    ip = ip.substring(0, ip.indexOf(","));
+                }
+            }
+        }
+        return ip;
+    }
+}

+ 9 - 10
sop-server/src/main/java/com/qmth/sop/server/api/SysController.java

@@ -89,18 +89,17 @@ public class SysController {
             SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.SMS_NORMAL_CODE);
             SysConfig sysConfig = commonCacheService.addSysConfigCache(SystemConstant.SMS_NORMAL_CODE);
             Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置万能短信验证码"));
             Optional.ofNullable(sysConfig).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置万能短信验证码"));
 
 
-            if (Objects.equals(sysConfig.getConfigValue().trim(), login.getCode().trim())) {
-                sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, login.getMobileNumber()));
-                if (CollectionUtils.isEmpty(sysUserList)) {
-                    throw ExceptionResultEnum.ERROR.exception("用户不存在");
-                }
-                if (sysUserList.size() > 1) {
-                    throw ExceptionResultEnum.ERROR.exception("查出多个用户");
-                }
-                sysUser = sysUserList.get(0);
-            } else {//走发送验证码模式
+            if (!Objects.equals(sysConfig.getConfigValue().trim(), login.getCode().trim())) {
 
 
             }
             }
+            sysUserList = sysUserService.list(new QueryWrapper<SysUser>().lambda().eq(SysUser::getMobileNumber, login.getMobileNumber()));
+            if (CollectionUtils.isEmpty(sysUserList)) {
+                throw ExceptionResultEnum.ERROR.exception("用户不存在");
+            }
+            if (sysUserList.size() > 1) {
+                throw ExceptionResultEnum.ERROR.exception("查出多个用户");
+            }
+            sysUser = sysUserList.get(0);
         }
         }
         Optional.ofNullable(sysUser).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未查询到用户"));
         Optional.ofNullable(sysUser).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未查询到用户"));
         return ResultUtil.ok(sysUserService.login(login.getPassword(), sysUser, AppSourceEnum.SYSTEM));
         return ResultUtil.ok(sysUserService.login(login.getPassword(), sysUser, AppSourceEnum.SYSTEM));