你必须在这里有所区别。如果您使用 WebApplicationBundle (WAB) 来部署 Servlet,您将拥有 Web 应用程序的所有常规元素。包括基本或基于表单的身份验证。
由于您使用的是 OSGi 注册 Servlet 的方式,因此您只能通过 HttpContext 来执行此操作。下面的示例取自Pax Web Samples,它使用基本身份验证。
public class AuthHttpContext implements HttpContext {
public boolean handleSecurity(HttpServletRequest req,
HttpServletResponse res) throws IOException {
if (req.getHeader("Authorization") == null) {
res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
if (authenticated(req)) {
return true;
} else {
res.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
}
protected boolean authenticated(HttpServletRequest request) {
request.setAttribute(AUTHENTICATION_TYPE, HttpServletRequest.BASIC_AUTH);
String authzHeader = request.getHeader("Authorization");
String usernameAndPassword = new String(Base64.decodeBase64(authzHeader.substring(6).getBytes()));
int userNameIndex = usernameAndPassword.indexOf(":");
String username = usernameAndPassword.substring(0, userNameIndex);
String password = usernameAndPassword.substring(userNameIndex + 1);
// Here I will do lame hard coded credential check. HIGHLY NOT RECOMMENDED!
boolean success = ((username.equals("admin") && password
.equals("admin")));
if (success)
request.setAttribute(REMOTE_USER, "admin");
return success;
}
...
}
对于基于表单,您需要一个额外的 HttpContext。对于您需要确保注册正确的 HttpContext 的每个匹配路径,也可以在Pax Web Samples中找到以下代码。
public final class Activator implements BundleActivator {
...
public void start(BundleContext bc) throws Exception {
httpServiceRef = bc.getServiceReference(HttpService.class);
if (httpServiceRef != null) {
httpService = (HttpService) bc.getService(httpServiceRef);
...
httpService.registerServlet("/status-with-auth",
new StatusServlet(), null, new AuthHttpContext());
}
}
...
}