| | |
| | | * SSO登录入口 - 信通院会调用这个地址 |
| | | * 访问路径:http://域名:8095/sso/login |
| | | */ |
| | | @GetMapping("ssoLogin") |
| | | public RedirectView ssoLogin() { |
| | | @GetMapping("") |
| | | public void ssoLogin() { |
| | | log.info("收到SSO登录请求,开始重定向到授权服务器"); |
| | | |
| | | // Authorize鉴权接口 |
| | | String param = "client_id=" + clientId + "&redirect_uri=" + internalRedirectUri + "&response_type=code" + "&state=" + state + "&scope=" + scope; |
| | | log.info("【Authorize鉴权接口】入参为:{}", param); |
| | | String s = HttpUtils.sendGet(internalAuthorizeUrl, param); |
| | | Map<String, String> result = getResult(s); |
| | | String code = result.get("code"); |
| | | try { |
| | | SSOTokenResponse accessToken = getAccessToken(code, true); |
| | | SSOUserInfo userInfo = getUserInfo(accessToken.getAccess_token(), true); |
| | | |
| | | createLocalSession(userInfo); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * SSO登录入口 - 信通院会调用这个地址 |
| | | * 访问路径:http://域名:8095/sso/login |
| | | */ |
| | | @GetMapping("ssoLoginLyra") |
| | | public RedirectView ssoLoginLyra() { |
| | | log.info("收到SSO登录请求,开始重定向到授权服务器"); |
| | | String id = clientId; |
| | | String redirectUri = internalRedirectUri; |
| | |
| | | Map<String, String> result = getResult(s); |
| | | String code = result.get("code"); |
| | | try { |
| | | SSOTokenResponse accessToken = getAccessToken(code, true); |
| | | SSOUserInfo userInfo = getUserInfo(accessToken.getAccess_token(), true); |
| | | SSOTokenResponse accessToken = getAccessTokenLyra(code, true); |
| | | SSOUserInfo userInfo = getUserInfoLyra(accessToken.getAccess_token(), true); |
| | | |
| | | createLocalSession(userInfo); |
| | | |
| | |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | |
| | | MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
| | | params.add("client_id", clientId); |
| | | params.add("client_secret", clientSecret); |
| | | params.add("code", code); |
| | | params.add("grant_type", "authorization_code"); |
| | | params.add("redirect_uri", getRedirectUri(isInternal)); |
| | | |
| | | HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers); |
| | | |
| | | ResponseEntity<String> response = restTemplate.exchange(getTokenUrl(isInternal), HttpMethod.POST, request, String.class); |
| | | |
| | | log.info("Token响应: {}", response.getBody()); |
| | | |
| | | if (response.getBody() == null || response.getBody().trim().isEmpty()) { |
| | | throw new RuntimeException("Token响应为空"); |
| | | } |
| | | |
| | | SSOTokenResponse tokenResponse = JSON.parseObject(response.getBody(), SSOTokenResponse.class); |
| | | |
| | | if (tokenResponse == null || StringUtils.isEmpty(tokenResponse.getAccess_token())) { |
| | | throw new RuntimeException("获取access_token失败"); |
| | | } |
| | | |
| | | return tokenResponse; |
| | | } |
| | | |
| | | /** |
| | | * 获取用户信息 |
| | | */ |
| | | private SSOUserInfo getUserInfo(String accessToken, boolean isInternal) throws Exception { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.set("Authorization", "Bearer " + accessToken); |
| | | |
| | | HttpEntity<String> entity = new HttpEntity<>(headers); |
| | | |
| | | ResponseEntity<String> response = restTemplate.exchange(getUserinfoUrl(isInternal), HttpMethod.GET, entity, String.class); |
| | | |
| | | log.info("用户信息响应: {}", response.getBody()); |
| | | |
| | | if (response.getBody() == null || response.getBody().trim().isEmpty()) { |
| | | throw new RuntimeException("用户信息响应为空"); |
| | | } |
| | | |
| | | SSOUserInfo userInfo = JSON.parseObject(response.getBody(), SSOUserInfo.class); |
| | | |
| | | if (userInfo == null || StringUtils.isEmpty(userInfo.getName())) { |
| | | throw new RuntimeException("获取用户信息失败或用户名为空"); |
| | | } |
| | | |
| | | return userInfo; |
| | | } |
| | | |
| | | /** |
| | | * 获取访问令牌 |
| | | */ |
| | | private SSOTokenResponse getAccessTokenLyra(String code, boolean isInternal) throws Exception { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); |
| | | |
| | | MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); |
| | | String id = clientId; |
| | | String secret = clientSecret; |
| | | String url = getTokenUrl(isInternal); |
| | |
| | | /** |
| | | * 获取用户信息 |
| | | */ |
| | | private SSOUserInfo getUserInfo(String accessToken, boolean isInternal) throws Exception { |
| | | private SSOUserInfo getUserInfoLyra(String accessToken, boolean isInternal) throws Exception { |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.set("Authorization", "Bearer " + accessToken); |
| | | |