package com.dy.sso.busi; 
 | 
  
 | 
import com.dy.common.aop.SsoVo; 
 | 
import com.dy.common.util.MD5; 
 | 
import com.dy.common.webUtil.BaseResponse; 
 | 
import com.dy.common.webUtil.BaseResponseUtils; 
 | 
import com.dy.pmsGlobal.aop.Log; 
 | 
import com.dy.pmsGlobal.pojoBa.BaUser; 
 | 
import com.mysql.cj.util.StringUtils; 
 | 
import jakarta.servlet.http.HttpServletRequest; 
 | 
import jakarta.servlet.http.HttpServletResponse; 
 | 
import jakarta.servlet.http.HttpSession; 
 | 
import jakarta.validation.Valid; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.http.MediaType; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import javax.imageio.ImageIO; 
 | 
import java.awt.*; 
 | 
import java.awt.image.BufferedImage; 
 | 
import java.io.IOException; 
 | 
import java.util.UUID; 
 | 
  
 | 
/** 
 | 
 * 单点登录, 
 | 
 */ 
 | 
@Slf4j 
 | 
@RestController 
 | 
@RequestMapping(path="sso") 
 | 
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") 
 | 
public class SsoCtrl { 
 | 
  
 | 
    //在属性上注解@Autowired时,会警告 Field injection is not recommended(不再推荐使用字段注入) 
 | 
    private SsoSv sv ; 
 | 
  
 | 
    //@Autowired 
 | 
    //private CacheManager cacheManager ; 
 | 
  
 | 
    @Autowired 
 | 
    public void setSv(SsoSv sv ){ 
 | 
        this.sv = sv ; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 客户端请求用户登录,客户端提交Json数据 
 | 
     * @param vo 用户登录值对象 
 | 
     * @return 登录用户值对象 
 | 
     */ 
 | 
    @PostMapping(path = "login", consumes = MediaType.APPLICATION_JSON_VALUE)//前端提交json数据 
 | 
    @Log("用户登录") 
 | 
    public BaseResponse<UserVo> login(@RequestBody  @Valid LoginVo vo,HttpSession session) { 
 | 
        // 从Session中获取保存的验证码 
 | 
        String sessionCaptcha = (String) session.getAttribute("captcha"); 
 | 
        // 首先验证验证码 
 | 
        if (vo.captcha != null && vo.captcha.equalsIgnoreCase(sessionCaptcha)) { 
 | 
            session.removeAttribute("captcha"); 
 | 
            return this.doLogin(vo) ; 
 | 
        } else { 
 | 
            // 验证码错误,返回登录页面并显示错误信息 
 | 
            return BaseResponseUtils.buildFail("验证码错误"); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 客户端请求用户登录,客户端提交form表单 
 | 
     * @param loginVo 登录用户form表单对象 
 | 
     * @return 登录用户值对象 
 | 
     */ 
 | 
    @PostMapping(path = "loginForm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)//前端提交form表单数据 
 | 
    public BaseResponse<UserVo> loginForm(@Valid LoginVo loginVo,HttpSession session){ 
 | 
        // 从Session中获取保存的验证码 
 | 
        String sessionCaptcha = (String) session.getAttribute("captcha"); 
 | 
        // 首先验证验证码 
 | 
        if (loginVo.captcha != null && loginVo.captcha.equalsIgnoreCase(sessionCaptcha)) { 
 | 
            session.removeAttribute("captcha"); 
 | 
            return this.doLogin(loginVo) ; 
 | 
        } else { 
 | 
            // 验证码错误,返回登录页面并显示错误信息 
 | 
            return BaseResponseUtils.buildFail("验证码错误"); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 通过UUID退出登录,因为参数是uuid,所以此调用必须是后端相关代码调用,因为前端得不到cookie中的uuid 
 | 
     * @param hr HttpServletRequest 
 | 
     * @return 正常退出登录返回true,否则返回false 
 | 
     */ 
 | 
    @GetMapping(path = "logout") 
 | 
    @Log("退出登录") 
 | 
    public BaseResponse<Boolean> logout(HttpServletRequest hr){ 
 | 
        String token = hr.getHeader("token") ; 
 | 
        if(token != null){ 
 | 
            this.sv.logout(token) ; 
 | 
            return BaseResponseUtils.buildSuccess(true); 
 | 
        }else{ 
 | 
            return BaseResponseUtils.buildFail("未从header中得到token"); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    /** 
 | 
     * 此方法供子模块系统调用,所以不公开在API接口中 
 | 
     * 方法功能:得到登录用户id,否则返回null 
 | 
     * @param token 登录用户token 
 | 
     * @return 登录用户ID 
 | 
     */ 
 | 
    @GetMapping(path = "loginUserId") 
 | 
    public Long loginUserId(String token){ 
 | 
        BaUser userPo = this.sv.getByUuid(token) ; 
 | 
        return userPo == null ? null : userPo.id ; 
 | 
    } 
 | 
    /** 
 | 
     * 此方法供子模块系统调用,所以不公开在API接口中 
 | 
     * 方法功能:验证是否已经登录 
 | 
     * @param token 登录用户token 
 | 
     * @return SsoVo 
 | 
     */ 
 | 
    @GetMapping(path = "ssoCheck") 
 | 
    public SsoVo ssoCheck(String token){ 
 | 
        BaUser userPo = this.sv.getByUuid(token) ; 
 | 
        SsoVo vo = new SsoVo(); 
 | 
        if(userPo != null){ 
 | 
            vo.logined = true ; 
 | 
            vo.hasPower = true ;//默认有权限 
 | 
        }else{ 
 | 
            vo.logined = false ; 
 | 
            vo.hasPower = true ;//默认有权限 
 | 
        } 
 | 
        return vo ; 
 | 
    } 
 | 
    /** 
 | 
     * 此方法供子模块系统调用,所以不公开API接口。 
 | 
     * 方法功能:验证是否已经登录,如果登录了,再验证权限 
 | 
     * @param token 登录用户token 
 | 
     * @param privilege 验证一个权限 
 | 
     * @param allPrivilege 验证所有权限 
 | 
     * @param anyPrivilege 验证任何一个权限 
 | 
     * @return SsoVo 
 | 
     */ 
 | 
    @GetMapping(path = "ssoPowerCheck") 
 | 
    public SsoVo ssoPowerCheck(String token, String privilege, String[] allPrivilege, String[] anyPrivilege){ 
 | 
        BaUser userPo = this.sv.getByUuid(token) ; 
 | 
        SsoVo vo = new SsoVo(); 
 | 
        if(userPo != null){ 
 | 
            vo.logined = true ; 
 | 
            vo.hasPower = false ;//默认是无权限 
 | 
            if(userPo.supperAdmin != null && userPo.supperAdmin == 1){ 
 | 
                vo.hasPower = true ; 
 | 
            }else{ 
 | 
                if(privilege.equals("-1")){ 
 | 
                    //无需权限验证 
 | 
                    vo.hasPower = true ; 
 | 
                }else{ 
 | 
                    if(userPo.privileges != null && userPo.privileges.size() > 0){ 
 | 
                        vo.hasPower = this.hasOnePrivilege(privilege, userPo) ; 
 | 
                        if(!vo.hasPower){ 
 | 
                            vo.hasPower = this.hasAllPrivilege(allPrivilege, userPo) ; 
 | 
                            if(!vo.hasPower){ 
 | 
                                vo.hasPower = this.hasAnyPrivilege(anyPrivilege, userPo) ; 
 | 
                            } 
 | 
                        } 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        }else{ 
 | 
            vo.logined = false ; 
 | 
            vo.hasPower = false ; 
 | 
        } 
 | 
        return vo ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获得当前登录用户 
 | 
     * @param token 登录用户token 
 | 
     * @return SsoVo 
 | 
     */ 
 | 
    @GetMapping(path = "ssoCurUser") 
 | 
    public CurUserVo ssoCurUser(String token){ 
 | 
        BaUser userPo = this.sv.getByUuid(token) ; 
 | 
        CurUserVo vo = new CurUserVo(); 
 | 
        if(userPo != null){ 
 | 
            vo.id = userPo.id ; 
 | 
            vo.name = userPo.name; 
 | 
        } 
 | 
        return vo ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 生成登录验证码 
 | 
     * @param response 
 | 
     * @param session 
 | 
     * @throws IOException 
 | 
     */ 
 | 
    @GetMapping("/captcha") 
 | 
    public void captcha(HttpServletResponse response, HttpSession session) throws IOException { 
 | 
        // 设置响应的类型格式为图片格式 
 | 
        response.setContentType("image/jpeg"); 
 | 
        // 禁止图像缓存 
 | 
        response.setHeader("Pragma", "no-cache"); 
 | 
        response.setHeader("Cache-Control", "no-cache"); 
 | 
        response.setDateHeader("Expires", 0); 
 | 
  
 | 
        int width = 100, height = 50; 
 | 
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
 | 
        Graphics g = image.getGraphics(); 
 | 
        // 设定背景色 
 | 
        g.setColor(Color.WHITE); 
 | 
        g.fillRect(0, 0, width, height); 
 | 
        // 设定字体 
 | 
        g.setFont(new Font("Arial", Font.BOLD, 30)); 
 | 
        // 随机生成验证码 
 | 
        String captcha = generateCaptcha(); 
 | 
        // 将验证码存入Session 
 | 
        session.setAttribute("captcha", captcha); 
 | 
        // 在图片上绘制验证码 
 | 
        g.setColor(Color.BLACK); 
 | 
        g.drawString(captcha, 15, 35); 
 | 
        g.dispose(); 
 | 
        // 输出图片 
 | 
        ImageIO.write(image, "JPEG", response.getOutputStream()); 
 | 
    } 
 | 
  
 | 
  
 | 
    ///////////////////////////////////////////////////////////////// 
 | 
    // 
 | 
    // 以下私有方法 
 | 
    // 
 | 
    ///////////////////////////////////////////////////////////////// 
 | 
  
 | 
    /** 
 | 
     * 生成四位随机数 
 | 
     * @return 
 | 
     */ 
 | 
    private String generateCaptcha() { 
 | 
//        Random r = new Random(); 
 | 
//        return r.nextInt(9000) + 1000 + ""; 
 | 
        return "1234"; 
 | 
    } 
 | 
    /** 
 | 
     * 用户登录 
 | 
     * @param vo 登录用户form表单对象 
 | 
     * @return 登录用户值对象 
 | 
     */ 
 | 
    private BaseResponse<UserVo> doLogin(LoginVo vo){ 
 | 
        String uuid ; 
 | 
        BaUser userPo ; 
 | 
        try { 
 | 
            //Boolean flag = cacheManager.getCacheNames().isEmpty() ; 
 | 
            uuid = UUID.randomUUID().toString(); 
 | 
            if(!StringUtils.isNullOrEmpty(vo.password)){ 
 | 
                /* 
 | 
                如果前端进行了base64加密 
 | 
                po.password = new String(Base64.getDecoder().decode(po.password)) ; 
 | 
                */ 
 | 
                vo.password = MD5.encrypt(vo.password) ; 
 | 
            } 
 | 
            userPo = this.sv.login(uuid, vo.phone, vo.password); 
 | 
        } catch (Exception e) { 
 | 
            log.error("用户登录异常", e); 
 | 
            return BaseResponseUtils.buildException(e.getMessage()) ; 
 | 
        } 
 | 
  
 | 
        if(userPo != null){ 
 | 
            UserVo uVo = UserVoMapper.INSTANCT.po2vo(userPo); 
 | 
            uVo.token = uuid ; 
 | 
            return BaseResponseUtils.buildSuccess(uVo); 
 | 
        }else{ 
 | 
            return BaseResponseUtils.buildFail("登录失败"); 
 | 
        } 
 | 
    } 
 | 
    /** 
 | 
     * 判断登录用户是否拥有指定的一个权限 
 | 
     * @param privilege 指定的一个权限 
 | 
     * @param userPo 当前登录用户 
 | 
     * @return 是否有权限 
 | 
     */ 
 | 
    private boolean hasOnePrivilege(String privilege, BaUser userPo){ 
 | 
        boolean hasPrivilege  = false ; 
 | 
        if (privilege != null && !privilege.trim().equals("")) { 
 | 
            int intPri = Integer.parseInt(privilege); 
 | 
            for (Integer pri : userPo.privileges) { 
 | 
                if (pri == intPri) { 
 | 
                    hasPrivilege = true; 
 | 
                    break; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return hasPrivilege ; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 判断登录用户是否拥有指定的多个权限 
 | 
     * @param allPrivilege 指定的多个权限 
 | 
     * @param userPo 当前登录用户 
 | 
     * @return 是否有权限 
 | 
     */ 
 | 
    private boolean hasAllPrivilege(String[] allPrivilege, BaUser userPo){ 
 | 
        boolean hasPrivilege  = false ; 
 | 
        if(allPrivilege != null && allPrivilege.length > 0){ 
 | 
            int intPri ; 
 | 
            int count = 0 ; 
 | 
            for(String strPri : allPrivilege){ 
 | 
                intPri = Integer.parseInt(strPri) ; 
 | 
                for(Integer pri : userPo.privileges){ 
 | 
                    if(pri == intPri){ 
 | 
                        count++ ; 
 | 
                        break ; 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
            if(count  == allPrivilege.length){ 
 | 
                hasPrivilege = true ; 
 | 
            } 
 | 
        } 
 | 
        return hasPrivilege ; 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 判断登录用户是否拥有指定的某个权限 
 | 
     * @param anyPrivilege 指定的多个权限 
 | 
     * @param userPo 当前登录用户 
 | 
     * @return 是否有权限 
 | 
     */ 
 | 
    private boolean hasAnyPrivilege(String[] anyPrivilege, BaUser userPo){ 
 | 
        boolean hasPrivilege  = false ; 
 | 
        int intPri ; 
 | 
        if(anyPrivilege != null && anyPrivilege.length > 0){ 
 | 
            for(String strPri : anyPrivilege){ 
 | 
                intPri = Integer.parseInt(strPri) ; 
 | 
                for(Integer pri : userPo.privileges){ 
 | 
                    if(pri == intPri){ 
 | 
                        hasPrivilege = true ; 
 | 
                        break ; 
 | 
                    } 
 | 
                } 
 | 
                if(hasPrivilege){ 
 | 
                    break ; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return hasPrivilege ; 
 | 
    } 
 | 
  
 | 
  
 | 
} 
 |