Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 嵌入式Jetty-以编程方式添加基于表单的身份验证_Java_Ldap_Jetty_Forms Authentication - Fatal编程技术网

Java 嵌入式Jetty-以编程方式添加基于表单的身份验证

Java 嵌入式Jetty-以编程方式添加基于表单的身份验证,java,ldap,jetty,forms-authentication,Java,Ldap,Jetty,Forms Authentication,是否有一种方法可以通过编程方式添加如下所示的基于表单的身份验证? 我正在使用自己的LdapLoginModule。最初我使用基本身份验证,它工作正常,但现在我希望在登录页面上有更多的控制(如显示徽标等) 有好的样品吗 我使用的是嵌入式jetty v8.1.7。对于嵌入式jetty,我不使用任何web.xml。jetty服务器是以编程方式启动的 <login-config> <auth-method>FORM</auth-method> <

是否有一种方法可以通过编程方式添加如下所示的基于表单的身份验证? 我正在使用自己的
LdapLoginModule
。最初我使用基本身份验证,它工作正常,但现在我希望在登录页面上有更多的控制(如显示徽标等)

有好的样品吗

我使用的是嵌入式jetty v8.1.7。对于嵌入式jetty,我不使用任何web.xml。jetty服务器是以编程方式启动的

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>Test JAAS Realm</realm-name>
    <form-login-config>
        <form-login-page>/login.html</form-login-page>
        <form-error-page>/error.jsp</form-error-page>
    </form-login-config>
</login-config>

形式
测试JAAS领域
/login.html
/error.jsp

创建一个
FormAuthenticator
,并在
SecurityHandler
上为
ServletContextHandler
设置它。这段代码创建了一个带有2个servlet的普通服务器。第一个servlet用一个hello消息响应经过身份验证的用户名。第二个servlet实现了一个简单的登录表单

您应该能够将代码粘贴到
main[]
中并运行(在类路径中需要以下JAR;
jetty server
jetty servlet
jetty security
)。要进行测试,请将浏览器指向
http://localhost:8080
,在看到回复
hello username
之前,应该提示您输入凭据(用户名/密码)

Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);

context.addServlet(new ServletHolder(new DefaultServlet() {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().append("hello " + request.getUserPrincipal().getName());
  }
}), "/*");

context.addServlet(new ServletHolder(new DefaultServlet() {
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().append("<html><form method='POST' action='/j_security_check'>"
      + "<input type='text' name='j_username'/>"
      + "<input type='password' name='j_password'/>"
      + "<input type='submit' value='Login'/></form></html>");
    }
}), "/login");

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

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

ConstraintSecurityHandler securityHandler = new ConstraintSecurityHandler();
securityHandler.addConstraintMapping(constraintMapping);
HashLoginService loginService = new HashLoginService();
loginService.putUser("username", new Password("password"), new String[] {"user"});
securityHandler.setLoginService(loginService);

FormAuthenticator authenticator = new FormAuthenticator("/login", "/login", false);
securityHandler.setAuthenticator(authenticator);

context.setSecurityHandler(securityHandler);

server.start();
server.join();
Server服务器=新服务器(8080);
ServletContextHandler context=new ServletContextHandler(服务器“/”,ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY);
addServlet(新的ServletHolder(新的DefaultServlet()){
@凌驾
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
response.getWriter().append(“hello”+request.getUserPrincipal().getName());
}
}), "/*");
addServlet(新的ServletHolder(新的DefaultServlet()){
@凌驾
受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException{
response.getWriter().append(“”
+ ""
+ ""
+ "");
}
}),“/login”);
约束=新约束();
constraint.setName(constraint.\u FORM\u AUTH);
setRoles(新字符串[]{“用户”、“管理员”、“主持人”});
constraint.setAuthenticate(true);
ConstraintMapping ConstraintMapping=新的ConstraintMapping();
constraintMapping.setConstraint(约束);
constraintMapping.setPathSpec(“/*”);
ConstraintSecurityHandler securityHandler=新的ConstraintSecurityHandler();
addConstraintMapping(constraintMapping);
HashLoginService loginService=新的HashLoginService();
loginService.putUser(“用户名”,新密码(“密码”),新字符串[]{“用户”});
securityHandler.setLoginService(loginService);
FormAuthenticator authenticator=新的FormAuthenticator(“/login”,“/login”,false);
setAuthenticator(验证器);
setSecurityHandler(securityHandler);
server.start();
join();

谢谢!这对我很有帮助,而且我能让它工作。这很有帮助,但我想知道你是怎么知道要这么做的。码头文件是。。。而不是粗鲁,稀疏。例如,
ConstraintMapping
完全没有文档记录,谷歌根本没有提供任何相关信息。您是否必须阅读内部Jetty代码才能学习它?