在 Java EE 7 中使用 j_security_check 验证用户
Authenticating a user with j_security_check in Java EE 7
我现在练习Java EE 7。我在尝试使用容器提供的方式对用户进行身份验证时遇到问题,即 j_security_check
.
- 应用服务器:
Apache Tomcat 7
- Project/Application 姓名:
ServletDrill
- 用
@WebServlet
注释的资源(Servlet)
我的Web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServletDrill</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<security-constraint>
<web-resource-collection>
<web-resource-name>To_Auth</web-resource-name>
<url-pattern>/auth/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>valid</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/FormAuth.jsp</form-login-page>
<form-error-page>/LogInErr.jsp</form-error-page>
</form-login-config>
</login-config>
</web-app>
我的tomcat-users.xml
:
<tomcat-users>
<role rolename="valid"/>
<user username="username" password="pass" roles="valid"/>
</tomcat-users>
我的<form>
:
<form action="/ServletDrill/j_security_check" accept-charset="UTF-8" method="post">
<fieldset id="postForm">
<legend>j_security_check method</legend>
<div class="partContainer">
<div class="left"><label for="user" >User Name: </label></div>
<div class="right"><input type="text" name="j_username" id="user" required="required" maxlength="20"></div>
</div>
<hr/>
<div class="partContainer">
<div class="left"><label for="pass" >Pass: </label></div>
<div class="right"><input type="password" name="j_password" id="pass" required="required" maxlength="20"></div>
</div>
<hr/>
<div class="partContainer">
<div class="left"><input type="submit" value="Log In"></div>
<div class="right"><input type="reset" value="Reset"></div>
</div>
</fieldset>
</form>
我的受保护资源(Servlet):
@WebServlet("/auth/NeedsPriorAuth")
public final class NeedsPriorAuth extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Welcome, "+request.getRemoteUser()+". The user has been authenticated before hand").append("\n Auth Type: "+request.getAuthType());
}
}
当我执行以下 link,
<a href="/ServletDrill/auth/NeedsPriorAuth">Access Protected Servlet</a>
,用户重定向到以下页面进行身份验证,
<form-login-page>/FormAuth.jsp</form-login-page>
(我在上面发布的 <form>
)。
尽管传递了正确的凭据(在上面 tomcat-users.xml
发布),用户仍重定向到以下错误页面(在上面 Web.xml
发布):
<form-error-page>/LogInErr.jsp</form-error-page>
造成我如此不便的罪魁祸首是什么?我已经在这个问题上停留了好几天了。
<realm>
怎么样,我需要吗?
有什么想法吗?
好了,最后我解决了这个问题。我需要应用的修改:
<form action="j_security_check" accept-charset="UTF-8" method="post">
也就是说,action 没有应用程序上下文前缀。
另一件事是,在我的例子中,有 2 个 tomcat-users.xml 文件的实例:
- 在 Apache Tomcat v7 目录下(服务器应用程序)
- 以及可从
Project Explorer in Eclipse
访问的那个,在 Servers2 name 下
当你修改角色、用户名和密码时,如果你想看到影响发生,请在后面的实例示例下修改;服务器重启之后。
我现在练习Java EE 7。我在尝试使用容器提供的方式对用户进行身份验证时遇到问题,即 j_security_check
.
- 应用服务器:
Apache Tomcat 7
- Project/Application 姓名:
ServletDrill
- 用
@WebServlet
注释的资源(Servlet)
我的Web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ServletDrill</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<security-constraint>
<web-resource-collection>
<web-resource-name>To_Auth</web-resource-name>
<url-pattern>/auth/*</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>valid</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/FormAuth.jsp</form-login-page>
<form-error-page>/LogInErr.jsp</form-error-page>
</form-login-config>
</login-config>
</web-app>
我的tomcat-users.xml
:
<tomcat-users>
<role rolename="valid"/>
<user username="username" password="pass" roles="valid"/>
</tomcat-users>
我的<form>
:
<form action="/ServletDrill/j_security_check" accept-charset="UTF-8" method="post">
<fieldset id="postForm">
<legend>j_security_check method</legend>
<div class="partContainer">
<div class="left"><label for="user" >User Name: </label></div>
<div class="right"><input type="text" name="j_username" id="user" required="required" maxlength="20"></div>
</div>
<hr/>
<div class="partContainer">
<div class="left"><label for="pass" >Pass: </label></div>
<div class="right"><input type="password" name="j_password" id="pass" required="required" maxlength="20"></div>
</div>
<hr/>
<div class="partContainer">
<div class="left"><input type="submit" value="Log In"></div>
<div class="right"><input type="reset" value="Reset"></div>
</div>
</fieldset>
</form>
我的受保护资源(Servlet):
@WebServlet("/auth/NeedsPriorAuth")
public final class NeedsPriorAuth extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append("Welcome, "+request.getRemoteUser()+". The user has been authenticated before hand").append("\n Auth Type: "+request.getAuthType());
}
}
当我执行以下 link,
<a href="/ServletDrill/auth/NeedsPriorAuth">Access Protected Servlet</a>
,用户重定向到以下页面进行身份验证,
<form-login-page>/FormAuth.jsp</form-login-page>
(我在上面发布的 <form>
)。
尽管传递了正确的凭据(在上面 tomcat-users.xml
发布),用户仍重定向到以下错误页面(在上面 Web.xml
发布):
<form-error-page>/LogInErr.jsp</form-error-page>
造成我如此不便的罪魁祸首是什么?我已经在这个问题上停留了好几天了。
<realm>
怎么样,我需要吗?
有什么想法吗?
好了,最后我解决了这个问题。我需要应用的修改:
<form action="j_security_check" accept-charset="UTF-8" method="post">
也就是说,action 没有应用程序上下文前缀。 另一件事是,在我的例子中,有 2 个 tomcat-users.xml 文件的实例:
- 在 Apache Tomcat v7 目录下(服务器应用程序)
- 以及可从
Project Explorer in Eclipse
访问的那个,在 Servers2 name 下
当你修改角色、用户名和密码时,如果你想看到影响发生,请在后面的实例示例下修改;服务器重启之后。