gpt4 book ai didi

java - Jetty:以编程方式进行身份验证在重新启动后不起作用

转载 作者:行者123 更新时间:2023-12-01 11:19:22 24 4
gpt4 key购买 nike

我是 Jetty 的新手,正在开发一个测试应用程序来进行基本身份验证。项目中没有web.xml。一切都由代码处理。

我按照这个例子:https://github.com/eclipse/jetty.project/blob/master/examples/embedded/src/main/java/org/eclipse/jetty/embedded/SecuredHelloHandler.java

这是测试代码

Server server = new Server(8080);
LoginService loginService = new HashLoginService("MyRealm", path);
loginService.setRefreshInterval(5);
server.addBean(loginService);

ConstraintSecurityHandler security = new ConstraintSecurityHandler();
server.setHandler(security);

Constraint constraint = new Constraint();
constraint.setName("auth");
constraint.setAuthenticate(true);
constraint.setRoles(new String[] { "user", "admin" });

ConstraintMapping mapping = new ConstraintMapping();
mapping.setPathSpec("/*");
mapping.setConstraint(constraint);

security.setConstraintMappings(Collections.singletonList(mapping));
security.setAuthenticator(new BasicAuthenticator());
security.setLoginService(loginService);

HelloWorld hw = new HelloWorld();
security.setHandler(hw);

server.start();

这是我的测试领域.properties

guest:CRYPT:guVeRgi5kAY4k,user,admin

代码由测试 PDE 项目中的测试按钮触发。当服务器第一次启动时,它会提示输入用户名/密码的窗口。我输入了“guest”和“guest”,结果成功了。但之后,当我重新启动服务器时,它再也没有要求输入用户名/密码。该页面在未经身份验证的情况下加载。如果我更改了领域属性文件中的密码,该提示将再次出现,并且仍然仅出现一次。我错过了什么?谢谢

最佳答案

标准 HTTP Cookie 和 Servlet session 行为正在发生。

服务器重启不会导致客户端提供的Cookies不再起作用。您需要配置 Cookie 和 session 行为以满足您的需求(搜索 SessionCookieConfig 及其同类)。

关于java - Jetty:以编程方式进行身份验证在重新启动后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439116/

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