【问题标题】:How to set user roles when handling security in an OSGi Servlet在 OSGi Servlet 中处理安全性时如何设置用户角色
【发布时间】: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


    【解决方案1】:

    您只创建了一个新的主题实例。这不会自动更新会话中的那个。

    除此之外,jaas 中的问题始终是角色主体没有标准。您选择将角色编码为 PlainRolePrincipal。我不确定这是请求检查的内容。您必须查看该代码以了解它如何确定主体是否为角色主体。

    一个典型的情况是它检查某个类或接口名称,但我不确定你的情况。

    【讨论】:

    • 谢谢。接得好。不幸的是,我只是没有将我的代码完全复制到问题中。问题依然存在。
    猜你喜欢
    • 2013-03-21
    • 2016-12-08
    • 2019-04-17
    • 2011-05-14
    • 2012-03-14
    • 2013-12-28
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多