下面列出了怎么用org.apache.commons.httpclient.auth.MalformedChallengeException的API类实例代码及写法,或者点击链接到github查看源代码。
protected void parseChallenge(
final CharArrayBuffer buffer,
int beginIndex, int endIndex) throws MalformedChallengeException {
String challenge = buffer.substringTrimmed(beginIndex, endIndex);
if (challenge.length() == 0) {
if (this.state == State.UNINITIATED) {
this.state = State.CHALLENGE_RECEIVED;
} else {
this.state = State.FAILED;
}
this.challenge = null;
} else {
this.state = State.MSG_TYPE2_RECEVIED;
this.challenge = challenge;
}
}
/**
* Authenticating requests with SPNEGO means that a request will execute before the client is sure that the
* server is mutually authenticated. This means that, at best, if mutual auth is requested, the client cannot
* trust that the server is giving accurate information, or in the case that the client has already sent data,
* further communication with the server should not happen.
* @param returnChallenge The Negotiate challenge from the response headers of a successful executed request
* @throws AuthenticationException If the response header does not allow for mutual authentication to be established.
*/
public void ensureMutualAuth(String returnChallenge) throws AuthenticationException {
try {
processChallenge(returnChallenge);
} catch (MalformedChallengeException mce) {
throw new AuthenticationException("Received invalid response header for mutual authentication", mce);
}
try {
String token = getNegotiateToken();
if (!spnegoNegotiator.established() || token != null) {
throw new AuthenticationException("Could not complete SPNEGO Authentication, Mutual Authentication Failed");
}
} catch (GSSException gsse) {
throw new AuthenticationException("Could not complete SPNEGO Authentication", gsse);
}
}
public void processChallenge(String challenge)
throws MalformedChallengeException {
if ("Basic".equalsIgnoreCase(challenge)) {
super.processChallenge("Basic \"default\"");
} else {
super.processChallenge(challenge);
}
}
/**
* {@inheritDoc}
* <p>
* Called using the challenge text parsed from the header that is associated with this scheme's name.
* May advance the authentication process across multiple calls.
* </p>
*/
@Override
public void processChallenge(String challenge) throws MalformedChallengeException {
// Parse Challenge Data
// Challenge is base64 string to be given to gss context
if (StringUtils.hasText(challenge)) {
// Remove leading auth scheme name and trim data
this.challenge = challenge.substring(EsHadoopAuthPolicies.NEGOTIATE.length()).trim();
}
}
/**
* Processes the given challenge token. Some authentication schemes
* may involve multiple challenge-response exchanges. Such schemes must be able
* to maintain the state information when dealing with sequential challenges
*
* @param authheader the challenge header
*
* @throws MalformedChallengeException is thrown if the authentication challenge
* is malformed
*/
public void processChallenge(final String authheader) throws MalformedChallengeException {
if (authheader == null) {
throw new IllegalArgumentException("Header may not be null");
}
//String authheader = header.getName();
/* TEST
if (authheader.equalsIgnoreCase(WWW_AUTH)) {
this.challengeState = ChallengeState.TARGET;
} else if (authheader.equalsIgnoreCase(PROXY_AUTH)) {
this.challengeState = ChallengeState.PROXY;
} else {
throw new MalformedChallengeException("Unexpected header name: " + authheader);
} */
CharArrayBuffer buffer;
int pos;
/* if (header instanceof FormattedHeader) {
buffer = ((FormattedHeader) header).getBuffer();
pos = ((FormattedHeader) header).getValuePos();
} else {
String s = header.getValue(); */
String s = authheader;
if (s == null) {
throw new MalformedChallengeException("Header value is null");
}
buffer = new CharArrayBuffer(s.length());
buffer.append(s);
pos = 0;
//}
while (pos < buffer.length() && EncodingUtils.isWhitespace(buffer.charAt(pos))) {
pos++;
}
int beginIndex = pos;
while (pos < buffer.length() && !EncodingUtils.isWhitespace(buffer.charAt(pos))) {
pos++;
}
int endIndex = pos;
String s2 = buffer.substring(beginIndex, endIndex);
if (!s2.equalsIgnoreCase(getSchemeName())) {
throw new MalformedChallengeException("Invalid scheme identifier: " + s2);
}
parseChallenge(buffer, pos, buffer.length());
}
protected abstract void parseChallenge(
CharArrayBuffer buffer, int beginIndex, int endIndex) throws MalformedChallengeException;
@Override
public void processChallenge(String challenge) throws MalformedChallengeException {
super.processChallenge(challenge);
this.rotate = true;
}
/**
* Processes the NTLM challenge.
*
* @param challenge
* the challenge string
*
* @throws MalformedChallengeException
* is thrown if the authentication challenge is malformed
*
* @since 3.0
*/
public void processChallenge(final String challenge)
throws MalformedChallengeException {
String s = AuthChallengeParser.extractScheme(challenge);
if (!s.equalsIgnoreCase(getSchemeName())) {
throw new MalformedChallengeException("Invalid NTLM challenge: "
+ challenge);
}
int i = challenge.indexOf(' ');
if (i != -1) {
s = challenge.substring(i, challenge.length());
this.ntlmchallenge = s.trim();
this.state = TYPE2_MSG_RECEIVED;
} else {
this.ntlmchallenge = "";
if (this.state == UNINITIATED) {
this.state = INITIATED;
} else {
this.state = FAILED;
}
}
}
/**
* {@inheritDoc}
* <p>
* Called using the challenge text parsed from the header that is associated with this scheme's name.
* </p>
*/
@Override
public void processChallenge(String challenge) throws MalformedChallengeException {
complete = true;
}