SSM 框架拦截器:实现用户验证

这里写一个 SSM 框架拦截器实现用户验证的例子##

废话不多说,直接看代码。首先,配置拦截器,springMVC.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
	<!--  配置用注解的方式实现处理器 -->
	<context:component-scan base-package="com.user.*"></context:component-scan>
	
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
	
	
	<!-- 配置拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.movesys.interceptor.TimeInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
		 
	
</beans>

拦截器代码LoginInterceptor.java如下:

package com.user.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.user.bean.User;


/**
 * 登陆验证
 * @author LIU
 * @version 1.0 2017-10-31
 */
public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}

	
	/**
	 * Handler执行之前调用这个方法 
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object arg2) throws Exception {
		// TODO Auto-generated method stub
		
		 //获取请求的URL  
        String url = request.getRequestURI();  
        //URL:login.jsp是公开的;这个demo是除了login.jsp是可以公开访问的,其它的URL都进行拦截控制  
        if(url.indexOf("login.action")>=0){  
            return true;  
        }  
        //获取Session  
        HttpSession session = request.getSession();  
        User currUser = (User)session.getAttribute("currUser");  
          
        if(currUser != null){  
            return true;  
        }  
        
        //不符合条件的,跳转到登录界面  
        request.getRequestDispatcher("login.jsp").forward(request, response);  
          
		return false;
	}

}

感谢您的阅读,欢迎参观我的个人网站:知行乐集【www.zhixinglj.cn】

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SSM指的是Spring、SpringMVC、MyBatis三大框架的集成,是目前最为流行的Java Web开发框架之一。在Eclipse中利用SSM框架实现登录需要进行以下步骤: 1. 搭建SSM框架环境 首先需要在Eclipse中搭建好SSM框架的环境,包括Spring、SpringMVC、MyBatis等组件,以及相关依赖包的导入。 2. 设计数据库 登录功能需要用到数据库存储用户信息,因此需要在MySQL等数据库软件中创建相关表,并将表结构导入到Eclipse中。 3. 创建登录页面 在Eclipse中创建登录页面,包括用户名、密码的输入框,以及登录按钮等控件。同时还需要在页面中添加相关的CSS和JavaScript文件,实现页面的美化和交互效果。 4. 编写控制器代码 创建Java类作为登录页面的控制器,利用SpringMVC框架中的注解将其注册为控制器。在控制器中编写相关代码,实现登录功能。具体内容包括获取用户输入的用户名和密码,与数据库中存储的信息进行比对,验证用户身份并进行页面跳转等操作。 5. 配置MyBatis 在MyBatis中配置Mapper接口,实现数据库操作。这样就能够方便地从数据库中获取用户信息,以及将用户信息存储到数据库中。 6. 实现登录验证 利用Spring框架中的AOP机制,实现登录验证拦截器,并在控制器中使用拦截器进行登录验证。这样可以避免非法用户未经授权进行操作。 以上就是在Eclipse中使用SSM框架实现登录的基本步骤,具体实现过程可能还需根据项目需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值