【问题标题】:How do you provide security authentication for a ResourceHandler in Jetty without XML?如何在没有 XML 的情况下为 Jetty 中的 ResourceHandler 提供安全认证?
【发布时间】:2012-07-13 20:09:36
【问题描述】:

我正在尝试为我的 Jetty 服务器上的一个页面提供身份验证。我以编程方式完成所有操作,因此不涉及 xml。

我发现我可以使用 ConstraintSecurityHandler 保护特定的上下文。虽然这适用于我正在运行的 Servlet,但我尝试将其扩展为也适用于 ResourceHandler,但我遇到了问题。我正在尝试的代码如下所示。

如果我先放置 ResourceHandler 块,则无法弹出身份验证。 如果我把 ResourceHandler 块放在 SecurityHandler 块之后,那么会弹出认证,但是认证后,ResourceHandler 页面(/resources)没有出现,Jetty 给我一个 404。

有什么方法可以通过密码保护由 ResourceHandler 托管的页面??

final static String REALM = "REALM";

.
.
.

public static void main(String[] args){   
.
.
.
    ResourceHandler resourceHandler = new ResourceHandler(); //set up resourceHandler to host directory of files at /resources
    resourceHandler.setDirectoriesListed(true);
    resourceHandler.setResourceBase("." + File.separator + "files");
    ContextHandler resourceContextHandler = new ContextHandler();
    resourceContextHandler.setContextPath("/resources");
    resourceContextHandler.setHandler(resourceHandler);
    handlers.addHandler(resourceContextHandler);
.
.
.
    ConstraintSecurityHandler csh = getConstraintSecurityHandler();
    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setSecurityHandler(csh);
    handlers.addHandler(servletContextHandler);
.
.
.

    server.setHandler(handlers);
    server.start();
    server.join();
}

private ConstraintSecurityHandler getConstraintSecurityHandler(){
    Constraint constraint = new Constraint(Constraint.__BASIC_AUTH, "user");
    constraint.setRoles(new String[]{"user","admin"});
    constraint.setAuthenticate(true);

    ConstraintMapping statsConstraintMapping = new ConstraintMapping();
    statsConstraintMapping.setConstraint(constraint);
    statsConstraintMapping.setPathSpec("/resources"); //directory I want to protect

    ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
    csh.setAuthenticator(new BasicAuthenticator());
    csh.setRealmName(REALM);
    csh.setConstraintMappings(new ConstraintMapping[] {statsConstraintMapping});

    csh.setLoginService(getHashLoginService());

    return csh;
}

private HashLoginService getHashLoginService() {
    HashLoginService loginServ = new HashLoginService();
    loginServ.setName(REALM);
    loginServ.setConfig("realm.properties"); //location of authentication file
    loginServ.setRefreshInterval(1);
    return loginServ;
}

【问题讨论】:

    标签: security jetty constraints password-protection


    【解决方案1】:

    安全处理程序位于资源处理程序之前,因此在处理链中首先咨询安全处理程序。

    见:

    https://github.com/eclipse/jetty.project/blob/master/examples/embedded/src/main/java/org/eclipse/jetty/embedded/SecuredHelloHandler.java

    【讨论】:

    • 为了澄清,安全处理程序必须指向 webapp 处理程序,因此它保护 webapp。我有一个处理程序列表 (ContextHandlerCollection),只是将安全处理程序添加到此列表中 - 这不起作用。
    • 你能告诉我这是如何使用 jetty.xml 文件吗?
    猜你喜欢
    • 2014-06-25
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2018-12-25
    • 2021-06-09
    • 2022-08-23
    相关资源
    最近更新 更多