【发布时间】:2016-03-22 14:33:21
【问题描述】:
我正在使用HttpService 在 OSGi 包中注册一个 Servlet。我创建了自己的 HttpContext 类来处理安全性 - BasicAuthentication 并检查 ActiveDirectory。
Dictionary<String, String> params = new Hashtable<String, String>();
params.put("jersey.config.server.provider.classnames", SettingsService.class.getName());
HttpContext ctx = new HttpContext()
{
@Override
public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException
{
// validation against Active Directory here
return ADAuth.authenticate(request, response);
}
@Override
public URL getResource(String name)
{
return null;
}
@Override
public String getMimeType(String name)
{
return null;
}
};
httpService.registerServlet("/rest", new MyServlet(), params, ctx); //$NON-NLS-1$
httpService.registerResources("/web", "/web", null);
到目前为止一切顺利。我现在想为使用的登录设置角色,以便我可以使用@RolesAllowed 注释。角色将取决于 Active Directory 组。
如何设置角色?我尝试使用
设置角色HttpSession session = request.getSession(true);
Subject subject = (Subject) session.getAttribute("javax.security.auth.subject");
if (subject == null) {
subject = new Subject();
subject.getPrincipals().add(new PlainRolePrincipal(groupName));
session.setAttribute("javax.security.auth.subject", subject);
}
但 request.isUserInRole 总是返回 false。
更新
当我进入request.isUserInRole 时,我最终得到了这段代码:
if (_authentication instanceof Authentication.Deferred)
setAuthentication(((Authentication.Deferred)_authentication).authenticate(this));
if (_authentication instanceof Authentication.User)
return ((Authentication.User)_authentication).isUserInRole(_scope,role);
return false;
_authentication 值为空。应在何时/何处设置?
【问题讨论】:
标签: authentication osgi httpservice