feat: 优化代码

This commit is contained in:
wangyu 2024-10-12 22:48:42 +08:00
parent 34aa45f799
commit ac46f7e57b
4 changed files with 27 additions and 56 deletions

View File

@ -36,7 +36,7 @@ public class CASReceipt implements Serializable {
log.trace("entering getReceipt(ProxyTicketValidator=[" + ptv + "])"); log.trace("entering getReceipt(ProxyTicketValidator=[" + ptv + "])");
} }
if (!ptv.isAuthenticationSuccesful()) { if (!ptv.isAuthenticationSuccessful()) {
try { try {
ptv.validate(); ptv.validate();
} catch (Exception e) { } catch (Exception e) {
@ -46,7 +46,7 @@ public class CASReceipt implements Serializable {
} }
} }
if (!ptv.isAuthenticationSuccesful()) { if (!ptv.isAuthenticationSuccessful()) {
log.error("validation of [" + ptv + "] was not successful."); log.error("validation of [" + ptv + "] was not successful.");
throw new CASAuthenticationException("Unable to validate ProxyTicketValidator [" + ptv + "]"); throw new CASAuthenticationException("Unable to validate ProxyTicketValidator [" + ptv + "]");
} else { } else {

View File

@ -24,7 +24,7 @@ public class ProxyTicketValidator extends ServiceTicketValidator {
pv.validate(); pv.validate();
System.out.println(pv.getResponse()); System.out.println(pv.getResponse());
System.out.println(); System.out.println();
if (pv.isAuthenticationSuccesful()) { if (pv.isAuthenticationSuccessful()) {
System.out.println("user: " + pv.getUser()); System.out.println("user: " + pv.getUser());
System.out.println("proxies:\n " + pv.getProxyList()); System.out.println("proxies:\n " + pv.getProxyList());
} else { } else {

View File

@ -9,6 +9,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
public class SecureURL { public class SecureURL {
private static final Log log = LogFactory.getLog(SecureURL.class); private static final Log log = LogFactory.getLog(SecureURL.class);
@ -40,12 +41,11 @@ public class SecureURL {
output.write(chByte); output.write(chByte);
} }
String var7 = output.toString("utf-8"); return output.toString(StandardCharsets.UTF_8);
return var7;
} finally { } finally {
try { try {
if (r != null) { if (r != null) {
((BufferedReader) r).close(); r.close();
} }
} catch (IOException var14) { } catch (IOException var14) {
} }

View File

@ -1,6 +1,7 @@
package dev.flyfish.boot.cas.validator; package dev.flyfish.boot.cas.validator;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import org.xml.sax.Attributes; import org.xml.sax.Attributes;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
@ -15,16 +16,26 @@ import java.io.StringReader;
@ToString @ToString
public class ServiceTicketValidator { public class ServiceTicketValidator {
@Getter
@Setter
private String casValidateUrl; private String casValidateUrl;
@Getter
@Setter
private String proxyCallbackUrl; private String proxyCallbackUrl;
private String st; private String st;
@Setter
private String service; private String service;
@Getter
private String pgtIou; private String pgtIou;
@Getter
private String user; private String user;
@Getter
private String errorCode; private String errorCode;
@Getter
private String errorMessage; private String errorMessage;
private String entireResponse; private String entireResponse;
private String ss; private String ss;
@Setter
@Getter @Getter
private boolean renew = false; private boolean renew = false;
private boolean attemptedAuthentication; private boolean attemptedAuthentication;
@ -43,7 +54,7 @@ public class ServiceTicketValidator {
sv.validate(); sv.validate();
System.out.println(sv.getResponse()); System.out.println(sv.getResponse());
System.out.println(); System.out.println();
if (sv.isAuthenticationSuccesful()) { if (sv.isAuthenticationSuccessful()) {
System.out.println("user: " + sv.getUser()); System.out.println("user: " + sv.getUser());
System.out.println("pgtIou: " + sv.getPgtIou()); System.out.println("pgtIou: " + sv.getPgtIou());
} else { } else {
@ -53,54 +64,14 @@ public class ServiceTicketValidator {
} }
public void setCasValidateUrl(String x) {
this.casValidateUrl = x;
}
public String getCasValidateUrl() {
return this.casValidateUrl;
}
public void setProxyCallbackUrl(String x) {
this.proxyCallbackUrl = x;
}
public void setRenew(boolean b) {
this.renew = b;
}
public String getProxyCallbackUrl() {
return this.proxyCallbackUrl;
}
public void setServiceTicket(String x) { public void setServiceTicket(String x) {
this.st = x; this.st = x;
} }
public void setService(String x) { public boolean isAuthenticationSuccessful() {
this.service = x;
}
public String getUser() {
return this.user;
}
public String getPgtIou() {
return this.pgtIou;
}
public boolean isAuthenticationSuccesful() {
return this.successfulAuthentication; return this.successfulAuthentication;
} }
public String getErrorMessage() {
return this.errorMessage;
}
public String getErrorCode() {
return this.errorCode;
}
public String getResponse() { public String getResponse() {
return this.entireResponse; return this.entireResponse;
} }
@ -109,7 +80,7 @@ public class ServiceTicketValidator {
if (this.casValidateUrl != null && this.st != null) { if (this.casValidateUrl != null && this.st != null) {
this.clear(); this.clear();
this.attemptedAuthentication = true; this.attemptedAuthentication = true;
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
sb.append(this.casValidateUrl); sb.append(this.casValidateUrl);
if (this.casValidateUrl.indexOf(63) == -1) { if (this.casValidateUrl.indexOf(63) == -1) {
sb.append('?'); sb.append('?');
@ -117,9 +88,9 @@ public class ServiceTicketValidator {
sb.append('&'); sb.append('&');
} }
sb.append("service=" + this.service + "&ticket=" + this.st); sb.append("service=").append(this.service).append("&ticket=").append(this.st);
if (this.proxyCallbackUrl != null) { if (this.proxyCallbackUrl != null) {
sb.append("&pgtUrl=" + this.proxyCallbackUrl); sb.append("&pgtUrl=").append(this.proxyCallbackUrl);
} }
if (this.renew) { if (this.renew) {
@ -170,9 +141,9 @@ public class ServiceTicketValidator {
public void startElement(String ns, String ln, String qn, Attributes a) { public void startElement(String ns, String ln, String qn, Attributes a) {
this.currentText = new StringBuffer(); this.currentText = new StringBuffer();
if (qn.equals("cas:authenticationSuccess")) { if (qn.equals(AUTHENTICATION_SUCCESS)) {
this.authenticationSuccess = true; this.authenticationSuccess = true;
} else if (qn.equals("cas:authenticationFailure")) { } else if (qn.equals(AUTHENTICATION_FAILURE)) {
this.authenticationFailure = true; this.authenticationFailure = true;
this.errorCode = a.getValue("code"); this.errorCode = a.getValue("code");
if (this.errorCode != null) { if (this.errorCode != null) {
@ -188,14 +159,14 @@ public class ServiceTicketValidator {
public void endElement(String ns, String ln, String qn) throws SAXException { public void endElement(String ns, String ln, String qn) throws SAXException {
if (this.authenticationSuccess) { if (this.authenticationSuccess) {
if (qn.equals("cas:user")) { if (qn.equals(USER)) {
ServiceTicketValidator.this.user = this.currentText.toString().trim(); ServiceTicketValidator.this.user = this.currentText.toString().trim();
} }
if (qn.equals("cas:proxyGrantingTicket")) { if (qn.equals(PROXY_GRANTING_TICKET)) {
this.pgtIou = this.currentText.toString().trim(); this.pgtIou = this.currentText.toString().trim();
} }
} else if (this.authenticationFailure && qn.equals("cas:authenticationFailure")) { } else if (this.authenticationFailure && qn.equals(AUTHENTICATION_FAILURE)) {
this.errorMessage = this.currentText.toString().trim(); this.errorMessage = this.currentText.toString().trim();
} }