Jsp 如何使用struts2实现基于表单的身份验证
我想在struts2中实现基于表单的身份验证 我的目录结构是: My web.xml:Jsp 如何使用struts2实现基于表单的身份验证,jsp,struts2,Jsp,Struts2,我想在struts2中实现基于表单的身份验证 我的目录结构是: My web.xml: <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/ja
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/protected/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>manager</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
<!-- Default login configuration uses form-based authentication -->
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Example Form-Based Authentication Area</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description> An administrator </description>
<role-name>
manager
</role-name>
</security-role>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
当我插入相同的loginid和密码,但我重定向到错误页面时,我就这样做了
有人能给一个好的链接相同的
该示例应包含一个受保护的文件夹,一个用于登录的操作类
谢谢 您必须提供用户名和密码组合,该组合对应于已在GlassFish服务器的文件域中创建并已分配给manager组的用户 是一个关于如何创建文件域的链接 是一个关于如何创建jdbc安全领域的链接 这是一个有效的例子。您可以检查它是否与您的代码匹配 是更好地理解基于表单的身份验证的链接 希望这有帮助。:) 编辑: 基于表单的身份验证背后的思想是,您可以编写一个JSP或Servlet,用以下字段和操作表示表单:
<form action="j_security_check" method="post">
<input type="text" name="j_username"/>
<input type="password" name="j_password"/>
<input type="submit"/>
</form>
提交表单时,servlet容器使用您定义的机制(例如JAAS)为您检查凭据。在web.xml中,可以设置以下内容:
<login-config>
<form-login-check>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-check>
</login-config>
/login.jsp
/error.jsp
这允许容器定位包含表单或错误处理代码的JSP或Servlet。我不认为这是共享代码的地方?最好提出你目前为止所做的工作以及面临的问题。实际上,我对struts很陌生,想学习如何在其中实现表单身份验证。。所以我想从一个完美的例子开始。。在互联网上有一些,但没有一个是完美的我。有些缺少action类,有些缺少受保护的文件夹。。你能帮我个忙吗?我不知道你说的受保护文件夹是什么意思?你需要什么?你能再解释一下吗?受保护的文件夹是任何一个文件夹,其中包含受保护的jsp页面,只是作为资源。如果你将jsp放在web inf中,最终用户甚至无法直接访问这些页面,我想他只是想检查表单提交和验证。不需要为表单创建db记录等检查您的工作流程。我相信他稍后会在基于表单的身份验证中添加这些内容,表单重定向到的操作不是struts操作。因此,他可以通过指向自己的struts Action来检查工作流。如果j_security_check失败,它将始终转发到web.xmlYes中提到的错误页面,但他提到他正在输入用户名和密码,在这种情况下,它不应转发到错误页面。我的观点是,你的帖子不是这个问题的解决方案+1哦,我把原来的问题搞错了(与其他概念混在一起)
public class LoginAction extends ActionSupport {
String j_username, j_password;
public String getJ_password() {
return j_password;
}
public void setJ_password(String j_password) {
this.j_password = j_password;
}
public String getJ_username() {
return j_username;
}
public void setJ_username(String j_username) {
this.j_username = j_username;
}
@Override
public String execute() throws Exception {
if (getJ_username().equals(getJ_password())) {
return SUCCESS;
} else {
this.addActionError("Error..!");
return ERROR;
}
}
@Override
public void validate() {
if ((getJ_username() == null) || (getJ_username().length() == 0)) {
this.addActionError("Username Empty");
}
if ((getJ_password() == null) || (getJ_password().length() == 0)) {
this.addActionError("Password Empty");
}
}
<form action="j_security_check" method="post">
<input type="text" name="j_username"/>
<input type="password" name="j_password"/>
<input type="submit"/>
</form>
<login-config>
<form-login-check>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-check>
</login-config>