ludc
2023-09-25 cb103af8e81c601c4e5927df6aec8cdaf039475d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.vci.ubcs.code.controller;
 
import com.vci.ubcs.code.service.IPasswordFreeLoginService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;
 
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import java.io.IOException;
 
import static com.vci.ubcs.starter.util.AESUtils.aesDecrypt;
import static com.vci.ubcs.starter.util.AESUtils.aesEncrypt;
 
/**
 * @author ludc
 * @date 2023/9/12 9:07
 */
@RestController
@RequestMapping("/passwordFree")
@Api(value = "免密登录接口", tags = "免密登录接口")
@Slf4j
public class PasswordFreeLoginController {
 
    @Value("${password-free.client-id:a104c4fd2f0e4958}")
    private String clientId;//应用ID
 
    @Value("${password-free.secret-key:9fbd170bd83eb869}")
    private String secretKey;//应用秘钥
 
    @Resource
    private IPasswordFreeLoginService passwordFreeLoginService;
 
    @PostMapping("/login")
    public R passwordFreeLogin(@RequestParam("userName") String username, HttpServletRequest request) {
        boolean status;
        try {
            status = passwordFreeLoginService.pwdFreeLoginByBoolean(username,request);
        }catch (Exception e){
            throw new ServiceException("免密登录获取token失败:"+e.getMessage());
        }
        return R.status(status);
    }
 
    /**
     * 单点登录
     * @param empCode
     * @return
     * @throws Exception
     */
    @GetMapping("/ssoLogin")
    public String oaSsoLogin(@RequestParam("empCode") String empCode){
        String token;
        try {
            token = passwordFreeLoginService.ssoFreeLogin(empCode);
        }catch (Exception e){
            throw new ServiceException("单点登录获取token失败:"+e.getMessage());
        }
        return token;
    }
 
    /**
     * 单点登录
     * @param empCode
     * @return
     * @throws Exception
     */
    @GetMapping("/test")
    public String test(@RequestParam("empCode") String empCode){
        String enStr2;
        try {
            // 加密
            String pwdfree = aesEncrypt("pwdfree", secretKey);
            String pwdfree2 = aesEncrypt(pwdfree, clientId);
            // 解密
            String enStr1 = aesDecrypt(empCode, secretKey);
            enStr2 = aesDecrypt(enStr1, clientId);
        }catch (Exception e){
            throw new ServiceException("单点登录获取token失败:"+e.getMessage());
        }
        return enStr2;
    }
 
}