Jetty 9嵌入了Security + Servlets + StaticResources

时间:2013-06-06 22:04:18

标签: security servlets static jetty

我在最近几天左右尝试运行嵌入式Jetty服务器,但我无法像我想的那样配置我的安全处理程序..

初始化代码如下:

interfaceServer = new Server(configManager.getServerPort());
// Initializing the security handler
ServletContextHandler appContext = new ServletContextHandler(interfaceServer, "/", ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);
// SECURITY HANDLER
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.setConstraintMappings(getConstraintMappings());
securityHandler.setLoginService(getLoginService());
securityHandler.setAuthenticator(getAuthenticator());
securityHandler.setCheckWelcomeFiles(true);
// Set security
appContext.setSecurityHandler(securityHandler);
appContext.setResourceBase(webDir);
appContext.setWelcomeFiles(new String[] {"gui.tmpl"});
// Add servlets
appContext.addServlet(new ServletHolder(new SyncServlet(systemOutBypass)), "/sync");
appContext.addServlet(new ServletHolder(new CommandServlet(configManager)), "/exec");
appContext.addServlet(new ServletHolder(new ConfigServlet(configManager)), "/conf");
appContext.addServlet(new ServletHolder(new FeedServlet(configManager)), "/feed");
appContext.addServlet(new ServletHolder(new ProxyServlet(configManager)), "/proxy");
appContext.addServlet(new ServletHolder(new BrowseServlet(configManager)), "/browse");
appContext.addServlet(DefaultServlet.class, "/");
// Apply to server
interfaceServer.setHandler(appContext);
interfaceServer.start();

助手功能:

private static ConstraintMapping[] getConstraintMappings() {
    // CONSTRAINT
    Constraint constraint = new Constraint();
    constraint.setName(Constraint.__FORM_AUTH);
    constraint.setRoles(new String[]{"user", "admin"});
    constraint.setAuthenticate( true );
    // MAPPINGS
    ConstraintMapping mapping = new ConstraintMapping();
    mapping.setPathSpec( "*.tmpl" );
    mapping.setConstraint( constraint );

    return new ConstraintMapping[] {mapping};
}

private static LoginService getLoginService(){
    // HASH LOGIN SERVICE
    HashLoginService loginService = new HashLoginService();
    loginService.putUser("admin", new Password("admin"), new String[] {"user"});
    return loginService;
    }

private static Authenticator getAuthenticator(){
    // FORM AUTH
    return new FormAuthenticator("/login.tmpl", "/login.tmpl", false);
}

像这样配置它可以工作,但只有当我输入http://127.0.0.1:8083/gui.tmplhttp://127.0.0.1:8083/显示gui.tmpl文件但是没有加载所有其他东西而没有加载时我被重定向到登录页面重定向我..如果我将setPathSpec路径更改为“/”安全性应用于整个应用程序,但静态文件不可访问,因此登录表单没有样式(我被迫使用内联css设置样式)

所需行为是安全性,仅应用于文件夹或文件集以及可访问的静态文件,如CSS,图像等。是否可能?如果没有,唯一的解决方案是内联样式?请帮忙!

你可以在Bitbucket,实验分支找到完整的项目(以防万一......)

1 个答案:

答案 0 :(得分:1)

您需要以正确的方式(订单)链接处理程序。将appContext添加到securityHandler,而不是其他方式。

interfaceServer = new Server(configManager.getServerPort());
// Initializing the security handler
ServletContextHandler appContext = new ServletContextHandler(interfaceServer, "/", ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);
// SECURITY HANDLER
ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.setConstraintMappings(getConstraintMappings());
securityHandler.setLoginService(getLoginService());
securityHandler.setAuthenticator(getAuthenticator());
securityHandler.setCheckWelcomeFiles(true);
securityHandler.setHandler(appContext);
// Set security
appContext.setResourceBase(webDir);
appContext.setWelcomeFiles(new String[] {"gui.tmpl"});
// Add servlets
appContext.addServlet(new ServletHolder(new SyncServlet(systemOutBypass)), "/sync");
appContext.addServlet(new ServletHolder(new CommandServlet(configManager)), "/exec");
appContext.addServlet(new ServletHolder(new ConfigServlet(configManager)), "/conf");
appContext.addServlet(new ServletHolder(new FeedServlet(configManager)), "/feed");
appContext.addServlet(new ServletHolder(new ProxyServlet(configManager)), "/proxy");
appContext.addServlet(new ServletHolder(new BrowseServlet(configManager)), "/browse");
appContext.addServlet(DefaultServlet.class, "/");
// Apply to server
interfaceServer.setHandler(securityHandler);
interfaceServer.start();