下面列出了org.apache.http.client.AuthenticationHandler#org.apache.http.auth.MalformedChallengeException 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void parseChallenge(
final CharArrayBuffer buffer,
int beginIndex,
int endIndex ) throws MalformedChallengeException {
String challenge = buffer.substringTrimmed(beginIndex, endIndex);
if (log.isDebugEnabled()) {
log.debug("Received challenge '" + challenge + "' from the auth server");
}
if (state == State.UNINITIATED) {
token = base64codec.decode(challenge.getBytes());
state = State.CHALLENGE_RECEIVED;
} else {
log.debug("Authentication already attempted");
state = State.FAILED;
}
}
@Override
public Map<String, Header> getChallenges(
final HttpHost authhost, final HttpResponse response, final HttpContext context)
throws MalformedChallengeException
{
HttpRequest request = (HttpRequest) context.getAttribute(REQUEST_ATTRIBUTE);
if (request != null) {
Optional<Header> bearerTokenRequestOpt = getBearerHeader(request, HttpHeaders.AUTHORIZATION);
Optional<Header> bearerTokenResponseOpt = getBearerHeader(response, HttpHeaders.WWW_AUTHENTICATE);
/*
Add necessary bearer token from request to response if it's absent, it's required for avoid infinite loop in docker
(see NEXUS-23360)
*/
if (bearerTokenRequestOpt.isPresent() && !bearerTokenResponseOpt.isPresent()) {
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, bearerTokenRequestOpt.get().getValue());
}
}
return super.getChallenges(authhost, response, context);
}
private void processChallenges(
final Map<String, Header> challenges,
final AuthState authState,
final AuthenticationHandler authHandler,
final HttpResponse response,
final HttpContext context)
throws MalformedChallengeException, AuthenticationException {
AuthScheme authScheme = authState.getAuthScheme();
if (authScheme == null) {
// Authentication not attempted before
authScheme = authHandler.selectScheme(challenges, response, context);
authState.setAuthScheme(authScheme);
}
String id = authScheme.getSchemeName();
Header challenge = challenges.get(id.toLowerCase(Locale.ENGLISH));
if (challenge == null) {
throw new AuthenticationException(id +
" authorization challenge expected, but not found");
}
authScheme.processChallenge(challenge);
if (DEBUG) {
Logger.debug("Authorization challenge processed");
}
}
@Override
protected void parseChallenge(CharArrayBuffer buffer, int pos, int len) throws MalformedChallengeException {
super.parseChallenge(buffer, pos, len);
this.rotate = true;
}