Struts2+Spring+Hibernate step by step 11 ssh拦截验证用户登录到集成

简介:

注意:该系列文章从教师王健写了一部分ssh集成开发指南

引言:

之前没有引入拦截器之前,我们使用Filter过滤器验证用户是否登录,在使用struts2之后,全然能够使用拦截器,验证用户是否已经登录,假设没有登录。则显示登录页面,要求其先登录。

第一步:书写一个方法拦截器例如以下:

说明:由于在当前程序中。仅仅有一个Action类,即OneAction.java,而当中的excute方法又是登录方法,所以对于execute方法不能拦截。而对于其它方法则必须拦截,所以用法拦截器。代码例如以下:

package com.xuzheng.filter;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

@SuppressWarnings("serial")
public class ValidateLogin extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation invo) throws Exception {
		//验证用户是否已经登录
		if(ActionContext.getContext().getSession().get("user")!=null){
			System.out.println("用户已经登录......");
			return invo.invoke();
		}else{
			System.out.println("你还没有登录......");
			return Action.LOGIN;
		}
		
	}

}


第二步:将此拦截器配置到struts.xml中。例如以下:

<interceptors>
			<!-- 1、编写自己的拦截器 -->
			<interceptor name="validateLogin1" class="com.xuzheng.filter.ValidateLogin">
				<param name="excludeMethods">execute</param>
			</interceptor>
			<!-- 2、配置一个拦截器栈 -->
			<interceptor-stack name="validateLogin">
				<interceptor-ref name="defaultStack"></interceptor-ref>
				<interceptor-ref name="validateLogin1"></interceptor-ref>
			</interceptor-stack>
		</interceptors>
		<!-- 3、配置默认拦截器 -->
		<default-interceptor-ref name="validateLogin"></default-interceptor-ref>
		<!-- 4、配置全局结果转向 -->
		<global-results>
			<result name="login">/index.jsp</result>
		</global-results>

第三步:公布项目,假设在地址栏中直接输入例如以下:

http://127.0.0.1:8080/ssh/one!update.action 则会跳转至登录页面



图-1

至此,ssh整合系列教程到此完结。

源码下载:

step by step ssh 10

版权声明:本文博客原创文章。博客,未经同意,不得转载。







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4667716.html,如需转载请自行联系原作者


相关文章
|
1月前
|
Java Spring 容器
Spring系列文章:Spring6集成MyBatis3.5
Spring系列文章:Spring6集成MyBatis3.5
|
13天前
|
安全 Java 测试技术
Spring Boot集成支付宝支付:概念与实战
【4月更文挑战第29天】在电子商务和在线业务应用中,集成有效且安全的支付解决方案是至关重要的。支付宝作为中国领先的支付服务提供商,其支付功能的集成可以显著提升用户体验。本篇博客将详细介绍如何在Spring Boot应用中集成支付宝支付功能,并提供一个实战示例。
36 2
|
1天前
|
NoSQL Java MongoDB
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
【5月更文挑战第11天】本文介绍了如何将非关系型数据库MongoDB与Spring Boot框架集成,以实现高效灵活的数据管理。Spring Boot简化了Spring应用的构建和部署,MongoDB则以其对灵活数据结构的处理能力受到青睐。集成步骤包括:添加MongoDB依赖、配置连接信息、创建数据访问对象(DAO)以及进行数据操作。通过这种方式,开发者可以充分利用两者优势,应对各种数据需求。在实际应用中,结合微服务架构等技术,可以构建高性能、可扩展的系统。掌握MongoDB与Spring Boot集成对于提升开发效率和项目质量至关重要,未来有望在更多领域得到广泛应用。
【MongoDB 专栏】MongoDB 与 Spring Boot 的集成实践
|
4天前
|
安全 Java 数据库连接
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
在IntelliJ IDEA中通过Spring Boot集成达梦数据库:从入门到精通
|
13天前
|
弹性计算 运维 Shell
基于key验证多主机ssh访问
【4月更文挑战第30天】
22 1
|
28天前
|
缓存 Java Spring
单体项目中资源管理模块集成Spring Cache
该内容是关于将Spring Cache集成到资源管理模块以实现缓存同步的说明。主要策略包括:查询时添加到缓存,增删改时删除相关缓存。示例代码展示了@Service类中使用@Transactional和@Cacheable注解进行缓存操作,以及在RedisTemplate中处理缓存的示例。
24 5
|
1月前
|
Java 测试技术 Spring
Spring系列文章:Spring集成Log4j2⽇志框架、整合JUnit
Spring系列文章:Spring集成Log4j2⽇志框架、整合JUnit
|
1月前
|
SQL Java 数据库连接
jpa、hibernate、spring-data-jpa、jdbcTemplate
jpa、hibernate、spring-data-jpa、jdbcTemplate
|
1月前
|
消息中间件 Java Linux
RabbitMQ教程:Linux下安装、基本命令与Spring Boot集成
RabbitMQ教程:Linux下安装、基本命令与Spring Boot集成
|
2月前
|
XML Java API
Spring Boot中使用集成swagger-bootstrap-ui
Spring Boot中使用集成swagger-bootstrap-ui
26 0