|
@@ -12,22 +12,34 @@
|
|
|
*/
|
|
*/
|
|
|
package vip.xiaonuo.disk.service.impl;
|
|
package vip.xiaonuo.disk.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.dev33.satoken.stp.SaLoginModel;
|
|
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollStreamUtil;
|
|
import cn.hutool.core.collection.CollStreamUtil;
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
|
|
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 lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
+import vip.xiaonuo.auth.api.SaBaseLoginUserApi;
|
|
|
|
|
+import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
|
|
|
|
|
+import vip.xiaonuo.auth.modular.login.enums.AuthDeviceTypeEnum;
|
|
|
|
|
+import vip.xiaonuo.auth.modular.login.enums.AuthExceptionEnum;
|
|
|
|
|
+import vip.xiaonuo.common.cache.CommonCacheOperator;
|
|
|
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
|
|
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
|
|
|
import vip.xiaonuo.common.exception.CommonException;
|
|
import vip.xiaonuo.common.exception.CommonException;
|
|
|
import vip.xiaonuo.common.page.CommonPageRequest;
|
|
import vip.xiaonuo.common.page.CommonPageRequest;
|
|
|
|
|
+import vip.xiaonuo.common.pojo.CommonResult;
|
|
|
import vip.xiaonuo.disk.domain.CollegeUser;
|
|
import vip.xiaonuo.disk.domain.CollegeUser;
|
|
|
import vip.xiaonuo.disk.mapper.CollegeUserMapper;
|
|
import vip.xiaonuo.disk.mapper.CollegeUserMapper;
|
|
|
import vip.xiaonuo.disk.param.collegeUser.CollegeUserAddParam;
|
|
import vip.xiaonuo.disk.param.collegeUser.CollegeUserAddParam;
|
|
@@ -35,14 +47,18 @@ import vip.xiaonuo.disk.param.collegeUser.CollegeUserEditParam;
|
|
|
import vip.xiaonuo.disk.param.collegeUser.CollegeUserIdParam;
|
|
import vip.xiaonuo.disk.param.collegeUser.CollegeUserIdParam;
|
|
|
import vip.xiaonuo.disk.param.collegeUser.CollegeUserPageParam;
|
|
import vip.xiaonuo.disk.param.collegeUser.CollegeUserPageParam;
|
|
|
import vip.xiaonuo.disk.service.CollegeUserService;
|
|
import vip.xiaonuo.disk.service.CollegeUserService;
|
|
|
|
|
+import vip.xiaonuo.disk.util.HttpRequest;
|
|
|
import vip.xiaonuo.sys.modular.user.entity.SysUser;
|
|
import vip.xiaonuo.sys.modular.user.entity.SysUser;
|
|
|
import vip.xiaonuo.sys.modular.user.mapper.SysUserMapper;
|
|
import vip.xiaonuo.sys.modular.user.mapper.SysUserMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 下发用户日志表Service接口实现类
|
|
* 下发用户日志表Service接口实现类
|
|
@@ -54,10 +70,27 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
@Service
|
|
@Service
|
|
|
public class CollegeUserServiceImpl extends ServiceImpl<CollegeUserMapper, CollegeUser> implements CollegeUserService {
|
|
public class CollegeUserServiceImpl extends ServiceImpl<CollegeUserMapper, CollegeUser> implements CollegeUserService {
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ private static final String LOGIN_ERROR_TIMES_KEY_PREFIX = "login-error-times:";
|
|
|
|
|
+ @Value("${sso.url}")
|
|
|
|
|
+ private String ssoUrl;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${sso.APP_ID_TT}")
|
|
|
|
|
+ private String APP_ID_TT;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${sso.PRIVATE_KEY_TT}")
|
|
|
|
|
+ private String PRIVATE_KEY_TT;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private CommonCacheOperator commonCacheOperator;
|
|
|
|
|
+
|
|
|
private AtomicInteger userAddCounter = new AtomicInteger(0);
|
|
private AtomicInteger userAddCounter = new AtomicInteger(0);
|
|
|
|
|
|
|
|
@Resource
|
|
@Resource
|
|
|
private SysUserMapper sysUserMapper;
|
|
private SysUserMapper sysUserMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Resource(name = "loginUserApi")
|
|
|
|
|
+ private SaBaseLoginUserApi loginUserApi;
|
|
|
@Override
|
|
@Override
|
|
|
public Page<CollegeUser> page(CollegeUserPageParam collegeUserPageParam) {
|
|
public Page<CollegeUser> page(CollegeUserPageParam collegeUserPageParam) {
|
|
|
QueryWrapper<CollegeUser> queryWrapper = new QueryWrapper<>();
|
|
QueryWrapper<CollegeUser> queryWrapper = new QueryWrapper<>();
|
|
@@ -179,4 +212,95 @@ public class CollegeUserServiceImpl extends ServiceImpl<CollegeUserMapper, Colle
|
|
|
return collegeUser;
|
|
return collegeUser;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public CommonResult<String> getUser(HttpServletRequest httpServletRequest) {
|
|
|
|
|
+ //根据code获取token
|
|
|
|
|
+ String code = httpServletRequest.getParameter("code");
|
|
|
|
|
+ String url = ssoUrl+ "/oauth2/oauth/token";
|
|
|
|
|
+ String key = APP_ID_TT;
|
|
|
|
|
+
|
|
|
|
|
+ String grant_type ="authorization_code";
|
|
|
|
|
+ String secret = PRIVATE_KEY_TT;
|
|
|
|
|
+ HashMap<String,String> param = new HashMap<String,String>(2);
|
|
|
|
|
+
|
|
|
|
|
+ //资源认证服务器地址
|
|
|
|
|
+ param.put("code",code);
|
|
|
|
|
+ param.put("grant_type",grant_type);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ log.info("根据code获取token请求参数============"+param.toString());
|
|
|
|
|
+
|
|
|
|
|
+ String mapflag= HttpRequest.sendForm(url,param,key,secret);
|
|
|
|
|
+ JSONObject jsonmsg = JSONObject.parseObject(mapflag);
|
|
|
|
|
+ String token = jsonmsg.getString("access_token");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //根据token获取用户信息请求参数
|
|
|
|
|
+ JSONObject paramUser = new JSONObject();
|
|
|
|
|
+ paramUser.put("token",token);
|
|
|
|
|
+ paramUser.put("key",key);
|
|
|
|
|
+ paramUser.put("secret",secret);
|
|
|
|
|
+ String userUrl = ssoUrl+"/resourcesb/resources/index";
|
|
|
|
|
+
|
|
|
|
|
+ log.info("根据token获取用户信息请求参数============"+paramUser.toString());
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ String rs = HttpRequest.sendPost(userUrl, paramUser.toString());
|
|
|
|
|
+ JSONObject userRs = JSONObject.parseObject(rs);
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject user=userRs.getJSONObject("data");
|
|
|
|
|
+ String account=user.getString("iamUserCode");
|
|
|
|
|
+
|
|
|
|
|
+ SaBaseLoginUser saBaseLoginUser = loginUserApi.getUserByAccount(account);
|
|
|
|
|
+
|
|
|
|
|
+ if(ObjectUtil.isEmpty(saBaseLoginUser)){
|
|
|
|
|
+ return CommonResult.error("用户不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ // 删除redis 中的key
|
|
|
|
|
+ clearLoginErrorTimes(account);
|
|
|
|
|
+
|
|
|
|
|
+ String selftoken=execLoginB(saBaseLoginUser, AuthDeviceTypeEnum.PC.getValue());
|
|
|
|
|
+
|
|
|
|
|
+ return CommonResult.ok(selftoken);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private String execLoginB(SaBaseLoginUser saBaseLoginUser, String device) {
|
|
|
|
|
+ // 校验状态
|
|
|
|
|
+ if(!saBaseLoginUser.getEnabled()) {
|
|
|
|
|
+ throw new CommonException(AuthExceptionEnum.ACCOUNT_DISABLED.getValue());
|
|
|
|
|
+ }
|
|
|
|
|
+ // 执行登录
|
|
|
|
|
+ StpUtil.login(saBaseLoginUser.getId(), new SaLoginModel().setDevice(device).setExtra("name", saBaseLoginUser.getName()));
|
|
|
|
|
+ // 角色集合
|
|
|
|
|
+ List<cn.hutool.json.JSONObject> roleList = loginUserApi.getRoleListByUserId(saBaseLoginUser.getId());
|
|
|
|
|
+ // 角色id集合
|
|
|
|
|
+ List<String> roleIdList = roleList.stream().map(jsonObject -> jsonObject.getStr("id")).collect(Collectors.toList());
|
|
|
|
|
+ // 角色码集合
|
|
|
|
|
+ List<String> roleCodeList = roleList.stream().map(jsonObject -> jsonObject.getStr("code")).collect(Collectors.toList());
|
|
|
|
|
+ // 角色id和用户id集合
|
|
|
|
|
+ List<String> userAndRoleIdList = CollectionUtil.unionAll(roleIdList, CollectionUtil.newArrayList(saBaseLoginUser.getId()));
|
|
|
|
|
+ // 获取按钮码
|
|
|
|
|
+ saBaseLoginUser.setButtonCodeList(loginUserApi.getButtonCodeListListByUserAndRoleIdList(userAndRoleIdList));
|
|
|
|
|
+ // 获取移动端按钮码
|
|
|
|
|
+ saBaseLoginUser.setMobileButtonCodeList(loginUserApi.getMobileButtonCodeListListByUserIdAndRoleIdList(userAndRoleIdList));
|
|
|
|
|
+ // 获取数据范围
|
|
|
|
|
+ saBaseLoginUser.setDataScopeList(Convert.toList(SaBaseLoginUser.DataScope.class,
|
|
|
|
|
+ loginUserApi.getPermissionListByUserIdAndRoleIdList(userAndRoleIdList, saBaseLoginUser.getOrgId())));
|
|
|
|
|
+ // 获取权限码
|
|
|
|
|
+ saBaseLoginUser.setPermissionCodeList(saBaseLoginUser.getDataScopeList().stream()
|
|
|
|
|
+ .map(SaBaseLoginUser.DataScope::getApiUrl).collect(Collectors.toList()));
|
|
|
|
|
+ // 获取角色码
|
|
|
|
|
+ saBaseLoginUser.setRoleCodeList(roleCodeList);
|
|
|
|
|
+ // 缓存用户信息,此处使用TokenSession为了指定时间内无操作则自动下线
|
|
|
|
|
+ StpUtil.getTokenSession().set("loginUser", saBaseLoginUser);
|
|
|
|
|
+ // 返回token
|
|
|
|
|
+ return StpUtil.getTokenInfo().tokenValue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void clearLoginErrorTimes(String userAccount) {
|
|
|
|
|
+ String loginErrorKey = LOGIN_ERROR_TIMES_KEY_PREFIX + userAccount;
|
|
|
|
|
+ // 删除redis中的key
|
|
|
|
|
+ commonCacheOperator.remove(loginErrorKey);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|