rk
2025-09-22 cf2391a86bdea88196d49cd33949570f74c0985d
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.doumee.api.business;
 
import com.alibaba.fastjson.JSONObject;
import com.doumee.api.BaseController;
import com.doumee.biz.system.SystemDictDataBiz;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import com.doumee.core.utils.HtmlUtil;
import com.doumee.core.utils.HttpUtils;
import com.doumee.core.utils.Utils;
import com.doumee.dao.system.model.SystemLoginLog;
import com.doumee.dao.system.model.SystemUser;
import com.doumee.service.system.SystemLoginLogService;
import com.doumee.service.system.SystemUserService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;
 
@Api(tags = "单点登录")
@RestController
@RequestMapping("/business/web")
@Slf4j
public class WebController extends BaseController {
 
    @Autowired
    private SystemDictDataBiz systemDictDataBiz;
 
    @Autowired
    private SystemLoginLogService systemLoginLogService;
    @Autowired
    private SystemUserService systemUserService;
    @Value("${project.version}")
    private String systemVersion;
 
    /**
     * location.assign('https://sso.gongfuhf.cn/iamsso/oauth2/authorize?response_type=code&client_id=CmUngBQPfmzRNuUGLmqqQo&redirect_uri=http://ggfw.gongfuhf.cn:80/web/loginAuth.shtml');
     * 放在单点登录检测页
     *
     * @param testId
     * @param code
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping("/loginAuth")
    public void loginAuth(String testId, String code, HttpServletRequest request,
                          HttpServletResponse response) throws Exception {
 
        SystemLoginLog loginLog = new SystemLoginLog();
        loginLog.setSystemVersion(systemVersion);
        loginLog.setLoginTime(new Date());
        loginLog.setIp(Utils.User_Client.getIP(request));
        loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp()));
        loginLog.setPlatform(Utils.User_Client.getPlatform(request));
        loginLog.setClientInfo(Utils.User_Client.getBrowser(request));
        loginLog.setOsInfo(Utils.User_Client.getOS(request));
        loginLog.setServerIp(Utils.Server.getIP());
 
        SystemUser systemUser = new SystemUser();
 
        if (Constants.IS_DEBUG) {
            //测试模式查询用户信息
            SystemUser queryDto = new SystemUser();
            // queryDto.setUsername(username);
            queryDto.setDeleted(Boolean.FALSE);
            SystemUser sysresult = systemUserService.findOne(queryDto);
            systemUser = sysresult;
        } else {
            String params = "grant_type=authorization_code&code=" + code;
            // 换成你的客户端 id 和密钥
            params += String.format("&client_id=%s&client_secret=%s",
                    systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_CLIENT_ID).getCode(),
                    systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_CLIENT_KEY).getCode());
            String result = HttpUtils.doHttpPost(
                    systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_DOMAIN).getCode(), params);
            if (StringUtils.isNotBlank(result)) {
 
                JSONObject resultDetails = JSONObject.parseObject(result);
                String userInfoJson = "";
                String accessToken = resultDetails.getString("access_token");
                String uid = resultDetails.getString("uid");
                System.out.println("accessToken:  " + accessToken);
                System.out.println("id:  " + uid);
                userInfoJson = HttpUtils
                        .doHttpGet(systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_INTFACE_URL).getCode()
                                + uid + "?access_token=" + accessToken);
                if (StringUtils.isNotBlank(userInfoJson)) {
                    JSONObject user = JSONObject.parseObject(userInfoJson);
 
                    // 根据ID查询用户信息[查询sys_user ssoUserId]
                    String gfUserid = user.getString("id");
                    SystemUser queryDto = new SystemUser();
                    // queryDto.setUsername(username);
                    queryDto.setDeleted(Boolean.FALSE);
                    SystemUser sysresult = systemUserService.findOne(queryDto);
                    systemUser = sysresult;
                } else {
                    throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "单点登录获取用户信息异常");
                }
            } else {
                throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "单点登录获取token异常");
            }
        }
 
 
        if (systemUser == null) {
            //用户不存在新增用户
        }
 
        loginLog.setLoginUsername(systemUser.getUsername());
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(systemUser.getUsername(), systemUser.getPassword());
        subject.login(token);
        String url = "https://hefei.dtkey.cn/hfznzz_test";
        HtmlUtil.writerHtml(response,
                "<html><script type=\"text/javascript\">window.open ('" + request.getScheme() + "://"
                        + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"
                        + url + "','_top')</script></html>");
        try {
            subject.login(token);
            loginLog.setUserId(((LoginUserInfo) subject.getPrincipal()).getId());
            loginLog.setSuccess(Boolean.TRUE);
            systemLoginLogService.create(loginLog);
        } catch (AuthenticationException e) {
            log.error(ResponseStatus.ACCOUNT_INCORRECT.getMessage(), e);
            loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage());
            loginLog.setSuccess(Boolean.FALSE);
            systemLoginLogService.create(loginLog);
            throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), !(e.getCause() instanceof BusinessException) ? "账号或密码不正确" : e.getCause().getMessage());
        }
 
 
    }
}