From 8c230a5bac38668753a6d65ee104e015da5c9bfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=96?= Date: Tue, 28 May 2024 13:42:30 +0800 Subject: [PATCH] SSO 0.4 --- .../main/java/shkd/plugin/SSOLoginPugin.java | 68 ++++++++++--------- 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/shkd-cosmic-debug/src/main/java/shkd/plugin/SSOLoginPugin.java b/shkd-cosmic-debug/src/main/java/shkd/plugin/SSOLoginPugin.java index 7429ec1..46299d8 100644 --- a/shkd-cosmic-debug/src/main/java/shkd/plugin/SSOLoginPugin.java +++ b/shkd-cosmic-debug/src/main/java/shkd/plugin/SSOLoginPugin.java @@ -29,7 +29,8 @@ public class SSOLoginPugin implements ThirdSSOAuthHandler { private String service; private boolean encodeServiceUrl; private String casSeverLoginUrl; - Cas10TicketValidator ticketValidator10; + private String casServerUrlPrefix; + Cas10TicketValidator ticketValidator; private AuthenticationRedirectStrategy authenticationRedirectStrategy; public SSOLoginPugin() { @@ -43,21 +44,22 @@ public class SSOLoginPugin implements ThirdSSOAuthHandler { this.initialServiceData(request); if (this.casSeverLoginUrl == null) { this.casSeverLoginUrl = this.initCasLoginUrl(); + this.casServerUrlPrefix = this.casSeverLoginUrl.substring(0, this.casSeverLoginUrl.indexOf("sso") + 3); } String urlToRedirectTo = ""; String path = request.getRequestURI(); if (path.contains("/auth/logout.do")) { - urlToRedirectTo = initCasLogoutUrl() + "?service=" + service; + // urlToRedirectTo = initCasLogoutUrl() + "?service=" + service; + urlToRedirectTo = StringUtils.getPathString(this.casServerUrlPrefix) + "/logout?service=" + this.serverName; // urlToRedirectTo = StringUtils.getPathString(this.casSeverLoginUrl) + "logout?service=" + RevProxyUtil.getURLContextPath(request); } else { String serviceUrl = this.constructServiceUrl(request, response); logger.debug(String.format("Constructed service url: %s", serviceUrl)); - urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casSeverLoginUrl, this.getProtocol().getServiceParameterName(), serviceUrl, false, false, (String) null); + urlToRedirectTo = CommonUtils.constructRedirectUrl(this.casSeverLoginUrl, + this.getProtocol().getServiceParameterName(), serviceUrl, false, false, (String) null); } - logger.debug(String.format("redirecting to \"%s\"", urlToRedirectTo)); - try { this.authenticationRedirectStrategy.redirect(request, response, urlToRedirectTo); } catch (IOException var7) { @@ -93,6 +95,7 @@ public class SSOLoginPugin implements ThirdSSOAuthHandler { this.initialServiceData(request); if (this.casSeverLoginUrl == null) { this.casSeverLoginUrl = this.initCasLoginUrl(); + this.casServerUrlPrefix = this.casSeverLoginUrl.substring(0, this.casSeverLoginUrl.indexOf("sso") + 3); } String ticket = this.retrieveTicketFromRequest(request); @@ -100,35 +103,34 @@ public class SSOLoginPugin implements ThirdSSOAuthHandler { try { logger.info(String.format("Attempting to validate ticket: %s", ticket)); //logger.debug(String.format("Attempting to validate ticket: %s", ticket)); - String service2 = this.constructServiceUrl(request, response); - logger.info(String.format("Attempting to validate service2: %s", service2)); - //logger.debug(String.format("Attempting to validate service2: %s", service2)); - // "https://oa-uat.elmleaf.com.cn/sso/proxyValidate" - String tempXml = HttpRequest.post("http://10.157.226.9:8088/sso/proxyValidate") - .contentType("application/x-www-form-urlencoded") - .form("ticket",ticket) - .form("service", service2).execute().body(); - logger.info(String.format("Attempting to validate tempXml: %s", tempXml)); - if (tempXml != null) { - JSONObject user = XML.toJSONObject(tempXml); - String username = user.getJSONObject("cas:serviceResponse") - .getJSONObject("cas:authenticationSuccess").getStr("cas:user"); - if(username != null){ - result.setUser(username); - result.setSucess(true); - } +// String service2 = this.constructServiceUrl(request, response); +// logger.info(String.format("Attempting to validate service2: %s", service2)); +// //logger.debug(String.format("Attempting to validate service2: %s", service2)); +// // "https://oa-uat.elmleaf.com.cn/sso/proxyValidate" +// String tempXml = HttpRequest.post("http://10.157.226.9:8088/sso/proxyValidate") +// .contentType("application/x-www-form-urlencoded") +// .form("ticket",ticket) +// .form("service", service2).execute().body(); +// logger.info(String.format("Attempting to validate tempXml: %s", tempXml)); +// if (tempXml != null) { +// JSONObject user = XML.toJSONObject(tempXml); +// String username = user.getJSONObject("cas:serviceResponse") +// .getJSONObject("cas:authenticationSuccess").getStr("cas:user"); +// if(username != null){ +// result.setUser(username); +// result.setSucess(true); +// } +// } + if (this.ticketValidator == null) { + this.ticketValidator = new Cas10TicketValidator(this.casServerUrlPrefix); + } + Assertion assertion = this.ticketValidator.validate(ticket, this.constructServiceUrl(request, response)); + if (assertion != null) { + logger.debug(String.format("Successfully authenticated user: %s", assertion.getPrincipal().getName())); + String userName = assertion.getPrincipal().getName(); + result.setUser(userName); + result.setSucess(true); } - //if (this.ticketValidator == null) { - // this.ticketValidator = new Cas11TicketValidator(this.casSeverLoginUrl); - //} - //String userName = null; - //Assertion assertion = this.ticketValidator.validate2(ticket, this.constructServiceUrl(request, response)); - //logger.debug(String.format("Successfully authenticated user: %s", assertion.getPrincipal().getName())); - //if (assertion != null) { - // userName = assertion.getPrincipal().getName(); - // result.setUser(userName); - // result.setSucess(true); - //} } catch (Exception var7) { logger.error(var7); }