struts下的拦截器应用-登录验证

该博客详细介绍了如何在Struts2框架下利用拦截器进行登录验证。通过自定义拦截器继承MethodFilterInterceptor,确保只有登录成功的用户才能访问特定页面,否则会被重定向到登录页面。内容涵盖login页面、拦截器编写、action类处理、struts.xml配置及user实体等。
摘要由CSDN通过智能技术生成

目录

测试用的login页面:

自己编写的interceptor 需要继承MethodFilterInterceptor父类:

创建的action类需要在登录时将登录信息传入session中供拦截器识别:

 strust.xml配置:

其他的action和部分页面: 

user实体:


在普通的action登录验证中虽然能验证密码 却可以直接访问登录后的界面 用拦截器后可以拦截请求 使访问到别的页面的用户跳转到登录页面进行登录

测试用的login页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="${pageContext.request.contextPath}/login.action">
	用户名:<input type="text" name="username"><br>
	密码<input type="password" name="password"><br>
	<input type="submit" value="登录">
</form>
</body>
</html>

自己编写的interceptor 需要继承MethodFilterInterceptor父类:

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
import com.xin.domain.User;
import org.apache.struts2.ServletActionContext;

public class CheckLoginInterceptor extends MethodFilterInterceptor {
    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {
        System.out.println("校验登录的拦截器....");
        // TODO Auto-generated method stub
        //1.获取登录成功传入的sesson的user
        User user = (User) ServletActionContext.getRequest().getSess
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值