gpt4 book ai didi

jetty - 带有嵌入式 Jetty 7 服务器且没有 web.xml 文件的基本身份验证

转载 作者:行者123 更新时间:2023-12-04 06:45:52 25 4
gpt4 key购买 nike

我有一个作为服务运行的 Jetty 7 的嵌入式实现,并且想要为 servlet 添加没有 web.xml 文件的基本身份验证。

我使用描述的步骤创建了我的凭据 here

我认为我可以创建服务器,创建具有基本身份验证的安全处理程序并将 HashLoginService 附加到安全管理器。但我显然遗漏了几件事,因为我从来没有提示输入凭据。

下面是代码。任何帮助将不胜感激。

    server = new Server(port);
server.addConnector(getSslChannelConnector(securePort));
server.setGracefulShutdown(1000);
server.setStopAtShutdown(true);

// create the context handler for the server
ServletContextHandler sch = new ServletContextHandler(server, WEBAPP_CONTEXT);

// attach the security handler to it that has basic authentication
sch.setSecurityHandler(getSecurityHandler());

// define the processing servlet.
sch.addServlet(new ServletHolder(new ProcessingServlet()), "/process");

.
.
private SecurityHandler getSecurityHandler() {

// add authentication
Constraint constraint = new Constraint(Constraint.__BASIC_AUTH,"user");
constraint.setAuthenticate(true);
constraint.setRoles(new String[]{"user","admin"});

// map the security constraint to the root path.
ConstraintMapping cm = new ConstraintMapping();
cm.setConstraint(constraint);
cm.setPathSpec("/*");

// create the security handler, set the authentication to Basic
// and assign the realm.
ConstraintSecurityHandler csh = new ConstraintSecurityHandler();
csh.setAuthenticator(new BasicAuthenticator());
csh.setRealmName(REALM);
csh.addConstraintMapping(cm);

// set the login service
csh.setLoginService(getHashLoginService());

return csh;

}
private HashLoginService getHashLoginService() {

// create the login service, assign the realm and read the user credentials
// from the file /tmp/realm.properties.
HashLoginService hls = new HashLoginService();
hls.setName(REALM);
hls.setConfig("/tmp/realm.properties");
hls.setRefreshInterval(0);
return hls;
}

最佳答案

我得到了这个工作并发布了一个示例 webapp here

关于jetty - 带有嵌入式 Jetty 7 服务器且没有 web.xml 文件的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com