All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gccloud.starter.sso.cas.service.impl.PassportGcCloudNoUserServiceImpl Maven / Gradle / Ivy

package com.gccloud.starter.sso.cas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gccloud.starter.common.config.GlobalConfig;
import com.gccloud.starter.common.config.bean.DefaultColumnValue;
import com.gccloud.starter.common.constant.GlobalConst;
import com.gccloud.starter.common.entity.SysOrgEntity;
import com.gccloud.starter.common.entity.SysRoleEntity;
import com.gccloud.starter.common.module.user.dto.SysUserDTO;
import com.gccloud.starter.common.utils.XmlUtils;
import com.gccloud.starter.core.service.ISysOrgService;
import com.gccloud.starter.core.service.ISysRoleService;
import com.gccloud.starter.core.service.ISysTokenService;
import com.gccloud.starter.core.service.ISysUserService;
import com.gccloud.starter.core.vo.SysTokenVO;
import com.gccloud.starter.plugins.cache.common.IStarterCache;
import com.gccloud.starter.sso.cas.cache.TicketCache;
import com.gccloud.starter.sso.cas.config.CasConfig;
import com.gccloud.starter.sso.cas.service.ICasNoUserService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 集成公司的统一认证
 *
 * @author liuchengbiao
 * @date 2021年07月27日11:01:16
 */
@Service
@Slf4j
@ConditionalOnProperty(prefix = "cas", name = "noUserServiceClass", havingValue = "com.gccloud.starter.sso.cas.service.impl.PassportGcCloudNoUserServiceImpl")
public class PassportGcCloudNoUserServiceImpl implements ICasNoUserService {

    @Resource
    private ISysUserService userService;
    @Resource
    private ISysRoleService roleService;
    @Resource
    private ISysOrgService orgService;
    @Resource
    private GlobalConfig globalConfig;
    @Resource
    private ISysTokenService tokenService;
    @Resource
    private IStarterCache starterCache;
    @Resource
    private CasConfig casConfig;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, String ticket, String serviceResponse) {
        // 解析用户信息
        String username = XmlUtils.getTextForElement(serviceResponse, "user");
        String realName = XmlUtils.getTextForElement(serviceResponse, "realname");
        String telephone = XmlUtils.getTextForElement(serviceResponse, "telephone");
        String email = XmlUtils.getTextForElement(serviceResponse, "email");
        String code = XmlUtils.getTextForElement(serviceResponse, "code");
        // 创建用户对象
        SysUserDTO userDTO = new SysUserDTO();
        userDTO.setUsername(username);
        userDTO.setRealName(realName);
        userDTO.setPhone(telephone);
        userDTO.setEmail(email);
        // 设置随机密码,也可以自己设置默认密码
        String pwd = RandomStringUtils.randomAlphabetic(10);
        String encodePwd = new Sha256Hash(new Sha256Hash(pwd).toHex() + pwd).toHex();
        userDTO.setPassword(encodePwd);
        userDTO.setIdCard("***");
        userDTO.setStatus(GlobalConst.User.Status.NORMAL);
        // 自动创建机构信息
        String orgName = XmlUtils.getTextForElement(serviceResponse, "organize");
        LambdaQueryWrapper orgQueryWrapper = new LambdaQueryWrapper<>();
        orgQueryWrapper.eq(SysOrgEntity::getName, orgName);
        SysOrgEntity org = orgService.getOne(orgQueryWrapper);
        if (org == null) {
            org = new SysOrgEntity();
            org.setName(orgName);
            org.setCode(code);
            org.setParentId(GlobalConst.Org.SUPER_PARENT_ID);
            org.setOrderNum(1);
            orgService.save(org);
            LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(SysOrgEntity::getIds, GlobalConst.Org.SUPER_PARENT_ID + GlobalConst.Org.SEPARATOR + org.getId());
            updateWrapper.set(SysOrgEntity::getNames, org.getName());
            updateWrapper.set(SysOrgEntity::getCodes, org.getCode());
            updateWrapper.eq(SysOrgEntity::getId, org.getId());
            orgService.update(updateWrapper);
        }
        userDTO.setBelongOrgIdList(Lists.newArrayList(org.getId()));

        SysRoleEntity ordinaryRole = roleService.getByCode("ordinary");
        userDTO.setRoleIdList(Lists.newArrayList(ordinaryRole.getId()));

        DefaultColumnValue defaultColumnValue = globalConfig.getDefaultColumnValue();
        userDTO.setTenantId(defaultColumnValue.getTenantId());
        userService.add(userDTO);

        SysTokenVO token = tokenService.create(userDTO.getId());
        TicketCache ticketCache = new TicketCache();
        ticketCache.setTicket(ticket);
        ticketCache.setToken(token.getToken());
        starterCache.put(TicketCache.class, ticketCache.getTicket(), ticketCache);
        try {
            // 重定向
            response.sendRedirect(casConfig.getUiUrl() + "/sys/cas?token=" + token.getToken());
        } catch (IOException e) {
            log.error(ExceptionUtils.getStackTrace(e));
            return;
        }
    }

    public static void main(String[] args) {
        String serviceResponse = "\n" +
                "    \n" +
                "        liu.chengbiao\n" +
                "        \n" +
                "            160449\n" +
                "            false\n" +
                "            2021-07-22T15:25:41.830+08:00[Asia/Shanghai]\n" +
                "            UsiAuthenticationHandler\n" +
                "            18226640609\n" +
                "            刘成彪\n" +
                "            UsiCredential\n" +
                "            云网科技-创+研究院-技术中台PBU-基础平台产品部\n" +
                "            UsiAuthenticationHandler\n" +
                "            false\n" +
                "            29224\n" +
                "            [email protected]\n" +
                "            liu.chengbiao\n" +
                "            \n" +
                "    \n" +
                "";
        System.out.println(XmlUtils.getTextForElement(serviceResponse, "user"));
        System.out.println(XmlUtils.getTextForElement(serviceResponse, "realname"));
        System.out.println(XmlUtils.getTextForElement(serviceResponse, "telephone"));
        System.out.println(XmlUtils.getTextForElement(serviceResponse, "email"));

        System.out.println(RandomStringUtils.randomAlphabetic(8));
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy