Security tomcat 7基于表单的身份验证

Security tomcat 7基于表单的身份验证,security,authentication,tomcat,servlets,form-authentication,Security,Authentication,Tomcat,Servlets,Form Authentication,给定一个Servlet HelloServlet: @WebServlet("/HelloServlet") public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public HelloServlet() { // TODO Auto-gene

给定一个Servlet HelloServlet:

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor.
     */
    public HelloServlet() {
    // TODO Auto-generated constructor stub
    }


   @Override
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.print("hello my Friend: " + request.getRemoteUser());
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("This is the Test Servlet");

    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String headerName = (String) headerNames.nextElement();
        out.print("<br/>Header Name: <em>" + headerName);
        String headerValue = request.getHeader(headerName);
        out.print("</em>, Header Value: <em>" + headerValue);
        out.println("</em>");
    }
    }
....
}
@WebServlet(“/HelloServlet”)
公共类HelloServlet扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
/**
*默认构造函数。
*/
公共HelloServlet(){
//TODO自动生成的构造函数存根
}
@凌驾
受保护的无效数据集(HttpServletRequest请求,
HttpServletResponse响应)引发ServletException,IOException{
//TODO自动生成的方法存根
System.out.print(“你好,我的朋友:+request.getRemoteUser());
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
println(“这是测试Servlet”);
枚举headerNames=request.getHeaderNames();
while(headerNames.hasMoreElements()){
字符串headerName=(字符串)headerName.nextElement();
打印(“
标题名称:“+headerName”); 字符串headerValue=request.getHeader(headerName); 打印输出(“,表头值:+headerValue”); out.println(“”); } } .... }
使用web.xml中声明的tomcat安全策略:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>my application</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>

    <auth-constraint>
        <role-name>tomcat</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login.jsp</form-login-page>
        <form-error-page>/login-failed.jsp</form-error-page>
    </form-login-config>
</login-config>
<url-pattern>/*</url-pattern>

我的申请
/*
得到
邮递
雄猫
形式
/login.jsp
/login-failed.jsp
以及conf/tomcat-users.xml中的tomcat角色定义

  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>

“server.xml”中的域是:


,我尝试使用url localhost/jsfWorkgroup/HelloServlet访问Servlet“HelloServlet”

与预期一样,我(重新)被引导到登录页面:

<form method="POST" action="j_security_check">
<table>
  <tr>
    <td colspan="2">Login to the Tomcat-Demo application:</td>
  </tr>
  <tr>
    <td>Name:</td>
    <td><input type="text" name="j_username" /></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input type="password" name="j_password"/ ></td>
  </tr>
  <tr>
    <td colspan="2"><input type="submit" value="Go" /></td>
  </tr>
</table>
</form>

登录到Tomcat演示应用程序:
姓名:
密码:
无论我使用哪个id令牌:

  • 用户名:tomcat密码:tomcat
  • 用户名:两个密码:tomcat
  • 我仍然看到failure/login-failed.jsp

    以下是我的看法:tomcat会将我重定向到to login页面,但不会读取conf/tomcat-users.xml以使我的登录有效(即使在多次重新启动之后)

    你觉得怎么样


    配置:Tomcat 7.0.23,Eclipse Indigo

    Tomcat示例web.xml包括以下部分

    
    角色1
    雄猫
    
    你可能需要


    Tomcat包括一个示例war,其中包含一个使用Tomcat users.xml的身份验证,与您正在尝试的类似。如果部署了tomcat主页/webapps/examples,请尝试访问http://localhost/examples/jsp/security/protected/。确保已删除tomcat users.XML的角色/用户部分周围的XML注释。默认情况下,它们被注释掉

    <!-- Un comment me 
    <role rolename="tomcat"/>
    <role rolename="role1"/>
    <user username="tomcat" password="tomcat" roles="tomcat"/>
    <user username="both" password="tomcat" roles="tomcat,role1"/>
    <user username="role1" password="tomcat" roles="role1"/>
    -->
    
    
    


    您可以考虑用以帮助诊断AUTH问题。

    p>根据“PD40”的示例,我尝试了示例/JSP/安全/保护/示例,但不是在Eclipse IDE中,Tomcat通常与其他服务器(GalasFISH、JBOSS、ECT……)一起嵌入,而是启动了Tomcat服务器作为独立服务器。(在它的/bin目录中)…它在那里工作

    但当它试图在Eclipse中的Tomcat中运行基于安全性的Web应用程序时,它再次失败,甚至使用了上述配置


    我不知道我是否正确,但Web应用程序安全性只有在tomcat在eclipse之外运行时才受支持。

    这对我来说太晚了,我无法回答,但可能有人会发现这很有用


    实际上,如果您面临的问题是无法让tomcat配置通过eclipse工作并在eclipse之外运行,那么只需从eclipse服务器选项卡中删除服务器并再次添加。这应该可以解决问题。

    我发现,如果您更改嵌入eclipse中的tomcat-users.xml中的用户配置,您可以必须重新启动eclipse,而不仅仅是服务器才能识别新用户。我想eclipse会缓存tomact-user.xml文件。

    您已经通过 在web.xml中定义安全页面:

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>my application</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
    
        <auth-constraint>
            <role-name>tomcat</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>FORM</auth-method>
        <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/login-failed.jsp</form-error-page>
        </form-login-config>
    </login-config>
    
    <url-pattern>/*</url-pattern>
    
    /*
    
    该通配符引用内容路径中的所有页面。
    因此,您可以获得一个无限循环的重定向到登录页面。

    您可以从server.xml发布配置吗?@pd40我相应地更新了我的答案。请仔细检查
    conf/tomcat users.xml中的注释。
    :在默认配置中,所有内容都被注释掉,以防止任何类型的访问。我添加了它,但没有真正更改。您的登录名是.j吗sp在表单字段中发布j_用户名和j_密码?我尝试了examples/jsp/security/protected/local。在我取消对角色/用户部分的注释之前,它不起作用。只是检查一下。您可能已经这样做了。。是的。。examples/jsp/security/protected/的+1仍然保持不变。它确实按照预期工作,但只有在我部署t时才起作用他在独立的tomcat中与eclipse外的Web应用程序作战…所以我开始思考…Prasad永远不会太迟。你可以在“服务器”中更新你的tomcat配置由Eclipse自动创建的项目。例如,更新tomcat-users.xml并重新启动tomcat。它应该可以正常工作。这确实解释了问题,但您有解决方案吗?您必须将应用程序的默认内容与登录和“登录错误”分开页面中,只需将它们放在不同的目录中。例如/work/中的内容和/loginPages/中的“loginerror”。当然,您必须分别更改web.xml设置。