陈昶聿
2026-08-26 933855e14ac2b613176966d491b929984a02b20c
ruoyi-admin/src/main/java/com/ruoyi/web/controller/sso/SSOController.java
@@ -8,11 +8,13 @@
import com.ruoyi.common.utils.OkHttpExample;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import com.smartor.domain.SSOTokenResponse;
import com.smartor.domain.SSOUserInfo;
import com.smartor.domain.VO.ServiceSubtaskVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -21,10 +23,7 @@
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.web.util.UriComponentsBuilder;
@@ -88,6 +87,9 @@
    private TokenService tokenService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysConfigService sysConfigService;
    private final RestTemplate restTemplate;
@@ -145,35 +147,101 @@
        // 重定向地址对象(重定向地址
        RedirectView redirectView = new RedirectView();
        try {
            SSOTokenResponse accessToken = getAccessTokenLyra(code, accessTokenUrl, clientId, redirectUri, clientSecret,true);
            SSOTokenResponse accessToken = getAccessTokenLyra(code, accessTokenUrl, clientId, clientSecret, redirectUri,true);
            log.info("获取到的token:" + accessToken);
            SSOUserInfo userInfo = getUserInfoLyra(accessToken.getAccess_token(), userInfoUrl,true);
            log.info("获取到的用户信息:" + userInfo);
            createLocalSession(userInfo);
//            createLocalSession(userInfo);
            if (StringUtils.isEmpty(path)) {
                throw new BaseException("请配置前端地址");
            }
            String reviewUrl = "";
            reviewUrl = UriComponentsBuilder.fromHttpUrl(path)
                    .path("/loginSSO")
                    .queryParam("token", accessToken.getAccess_token())
                    .queryParam("orgid", userInfo.getZuZhiJGID())
                    .queryParam("orgname", userInfo.getZuZhiJGMC())
                    .queryParam("ZuHuID", userInfo.getYongHuID())
                    .queryParam("deptCode", "null")
                    .build()
                    .toUriString();
            log.info("单点登陆重定向地址为:{}", reviewUrl);
            redirectView.setUrl(reviewUrl);
            redirectView.setStatusCode(HttpStatus.FOUND);
            String userName =  userInfo.getZhiGongID();
            String orgid = userInfo.getZuZhiJGID();
            String campusid = "1";
            String deptId = null;
            String token = null;
            log.info("【SSO登录】获取用户Token参数:" + userInfo);
            String loginToken = loginService.ssoLogin(userName, orgid, deptId, campusid, token);
            if (StringUtils.isEmpty(loginToken)) {
                log.error("【SSO登录】登录失败,userName={}, orgid={}", userName, orgid);
                return buildRedirect(path + "/login");
            }
            String redirectUrl = path + "/loginSSO?token=" + loginToken + "&orgid=" + orgid + "&orgname=null&ZuHuID=null&deptCode=null";
            log.info("【SSO登录】重定向地址:{}", redirectUrl);
            return buildRedirect(redirectUrl);
        } catch (Exception e) {
            log.error("SSO登录失败", e);
            redirectView.setUrl(path + "/login?error=sso_failed");
            return redirectView;
        }
    }
    /**
     * ssoGoServiceSubtaskList
     * code 是/mediinfo-lyra-authserver/connect/authorize 由his提供
     * 登录客户端ID是固定的 clientId = 1553588321874087936  clientSecret = suifangxt
     */
    @GetMapping("ssoGoServiceSubtaskList")
    public RedirectView ssoGoServiceSubtaskList(@RequestParam(value = "code", required = false) String code,
                                                @RequestParam(value = "sfzh", required = false) String sfzh) {
        log.info("收到ssoGoServiceSubtaskList请求,开始重定向到授权服务器,code = {}, sfzh = {}",code, sfzh);
        String path = sysConfigService.selectConfigByKey("sys.qddz");
        String redirectUri = "https://9.208.2.190:8092/prod-api/sso/ssoLoginLyra";
        String lyraPath = "https://9.0.124.104:13021";
        String clientId = "1553588321874087936";
        String clientSecret = "suifangxt";
        String scope = "openid";
//        String authorizeUrl = lyraPath + "/mediinfo-lyra-authserver/connect/authorize";
        String accessTokenUrl = lyraPath + "/mediinfo-lyra-authserver/connect/token";
        String userInfoUrl =  lyraPath + "/mediinfo-lyra-authserver/connect/userinfo";
        // 重定向地址对象(重定向地址
        RedirectView redirectView = new RedirectView();
        try {
            SSOTokenResponse accessToken = getAccessTokenLyra(code, accessTokenUrl, clientId, clientSecret, redirectUri,true);
            log.info("获取到的token:" + accessToken);
            SSOUserInfo userInfo = getUserInfoLyra(accessToken.getAccess_token(), userInfoUrl,true);
            log.info("获取到的用户信息:" + userInfo);
//            createLocalSession(userInfo);
            if (StringUtils.isEmpty(path)) {
                throw new BaseException("请配置前端地址");
            }
            String userName =  userInfo.getZhiGongID();
            String orgid = userInfo.getZuZhiJGID();
            String campusid = "1";
            String deptId = null;
            String token = null;
            log.info("【SSO登录】获取用户Token参数:" + userInfo);
            String loginToken = loginService.ssoLogin(userName, orgid, deptId, campusid, token);
            if (StringUtils.isEmpty(loginToken)) {
                log.error("【SSO登录】登录失败,userName={}, orgid={}", userName, orgid);
                return buildRedirect(path + "/login");
            }
            String redirectUrl = path + "/loginSSO?token=" + loginToken + "&orgid=" + orgid +
                     "&sfzh=" + sfzh + "&orgname=null&ZuHuID=null&deptCode=null";
            log.info("【SSO登录】重定向地址:{}", redirectUrl);
            return buildRedirect(redirectUrl);
        } catch (Exception e) {
            log.error("SSO登录失败", e);
            redirectView.setUrl(path + "/login?error=sso_failed");
            return redirectView;
        }
    }
    /**
     * 构建重定向视图
     */
    private RedirectView buildRedirect(String url) {
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl(url);
        redirectView.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
        return redirectView;
    }