var externalAuthServer = "http://external-auth-server.com/"
if(request.headers.get("Authorization") == null) { response.statusCode = parseInt("401", 10); response.reasonPhrase = "Unauthorized"; response.content = "Authorization header is required."; } else { // Call the external authentication server // Send the Authorization header to the external server curlRequest = new Request(externalAuthServer+"authenticate"); curlRequest.method = "POST"; curlRequest.setHeader("Authorization", request.headers.get("Authorization"));
response = httpClient.send(curlRequest);
if(response.status == parseInt("200", 10)) {
// At this point, the user is authenticated
// and you can proceed to the target URL.
// For instance, if you want to call
// a protected API proxy on Apigee
// called "my-protected-api", then:
var targetUrl = "http://my-org.apigee.net/my-protected-api" +
request.getUri().getQuery();
targetRequest = new Request(targetUrl);
targetResponse = targetHttpClient.send(targetRequest);
} else {
// Authentication failed.
// Set appropriate error codes and error messages.
response.statusCode = parseInt("401", 10);
response.reasonPhrase = "Unauthorized";
response.content = "Authentication failed.";
}
}
请注意,这只是一个示例,可以根据您的身份验证服务器和代理的需求进行定制。