public static HttpURLConnection createHttpConnection(URL url, int connectTimeout, int readTimeout, Proxy proxy, Map<String, String> requestProperties, boolean isSpnegoEnabled) throws IOException, AuthenticationException {
HttpURLConnection connection = null;
if (proxy == null) {
connection = (HttpURLConnection) url.openConnection();
} else {
connection = (HttpURLConnection) url.openConnection(proxy);
}
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if (isSpnegoEnabled) {
connection.setRequestProperty("Authorization", "Negotiate" + createSpnegoToken(requestProperties));
}
connection.setRequestProperty("Accept-Encoding", "gzip");
connection.setRequestProperty("User-Agent", "Catalina-Ant-Task/1.0");
if (requestProperties != null) {
for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
connection.setRequestProperty(entry.getKey(), entry.getValue());
}
}
if (isSpnegoEnabled && proxy != null) {
if (requestProperties != null) {
connection.setRequestProperty("Proxy-Authorization", "Negotiate" + createSpnegoToken(requestProperties));
}
connection.setRequestProperty("Proxy-Connection", "Keep-Alive");
}
return connection;
}
/******* aiXcoder分隔符(请勿修改) *******/
// Creates a SPNEGO token for authenticating the user with the specified request properties.
private String createSpnegoToken(Map<String, String> requestProperties) {
String username = requestProperties.get("user");
String password = requestProperties.get("password");
String realm = requestProperties.get("realm");
String nonce = requestProperties.get("nonce");
String qop = requestProperties.get("qop");
String algorithm = requestProperties.get("algorithm");
if (username == null || password == null) {
throw new IllegalArgumentException(sm.getString("coyoteRequest.nullCtoNtInitialize"));
}
if (realm == null) {
throw new IllegalArgumentException(sm.getString("coyoteRequest.nullRealm"));
}
if (nonce == null) {
throw new IllegalArgumentException(sm.getString("coyoteRequest.nullNonce"));
}
if (algorithm == null) {
throw new IllegalArgumentException(sm.getString("coyoteRequest.nullAlgorithm"));
}
boolean secure = "https".equals(protocol);
String spnegoToken = null;
try {
if (secure) {
spnegoToken = SPNEGOContext.getAuthenticator().authenticate(username, password, realm, nonce, qop, algorithm);
} else {
spnegoToken = SPNEGOContext.getAuthenticator().authenticate(username, password, realm, nonce, qop, algorithm, null);
}
} catch (GSSException e) {
if (log.isDebugEnabled()) {
log.debug(sm.getString("coyoteRequest.authenticateFail"), e);
}
}
return spnegoToken;
}
/******* aiXcoder分隔符(请勿修改) *******/
// Creates a SPNEGO token for the given request properties. If the current session is not spnego enabled, no token is generated.
private String createSpnegoToken(Map<String, String> requestProperties) throws AuthenticationException {
if (!isSpnegoEnabled) {
return "";
}
String username = requestProperties.get(USERNAME_PROPERTY);
String password = requestProperties.get(PASSWORD_PROPERTY);
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
throw new AuthenticationException("Username or password is missing");
}
String spnegoToken = gssManager.createSpnegoToken(username, password);
if (StringUtils.isEmpty(spnegoToken)) {
throw new AuthenticationException("SPNEGO token cannot be retrieved");
}
return spnegoToken;
}