spring-mvc登录拦截器

想要实现下图功能:

首先编写jsp前台页面

<body>
		<body style="margin:150px 0 0 600px">
		<div id="p" class="easyui-panel" title="登录"     
		        style="width:350px;height:260px;padding:10px;background:#fafafa;"   
		        data-options="iconCls:'icon-man'">  
		   <form id="loginform" method="post">   
			   	<table style="padding: 20px 0 10px 30px">
			   		<tr>
			   			<td>用户名:</td>
			   			<td><input class="easyui-validatebox" type="text" name="user_name" data-options="required:true" /></td>
			   		</tr>
			   		<tr>
			   			<td>密码:</td>
			   			<td><input class="easyui-validatebox" type="text" name="password" data-options="required:true" /></td>
			   		</tr>
			   		<!-- <tr>
			   			<td>验证码:</td>
			   			<td><input type="text" class="easyui-validatebox" id="code" name="code" placeholder="验证码" maxlength="4" value="" style="width: 60px" data-options="required:true" tipPosition="top"/>
			   				 <img src="user/getCode.action" title="看不清,点击刷新" οnclick="reloadValidCode()"
								style="vertical-align: middle;" id="imgcode"/></td>
			   		</tr> -->
			   		
			   		<tr >
			   			<td></td>
			   			<td>
			   			<a id="btn" href="#" οnclick="login()" class="easyui-linkbutton c7" data-options="iconCls:'icon-ok'">登录</a> 
			   			<a id="btn" href="#" οnclick="regisger()" class="easyui-linkbutton" data-options="iconCls:'icon-add'" style="margin-left: 55px">注册</a> 
			   			</td>
			   		</tr>
			   		
			   	</table>
			</form>  
		
		</div> 	

<script type="text/javascript">
/* function keyEnter(){ 
	if (event.keyCode == 13) { 
		login();
	} 
}  */
/* document.onkeydown =keyEnter;
function reloadValidCode() {
	//加入时间戳
	$("#imgcode").prop('src',"user/getCode.action?timed="+ new Date().getMilliseconds());
} */
function login(){
	$('#loginform').form('submit', {    
	    url:"${ctx}/user/login.action",   
	    onSubmit: function(){
	    	//gettips("code","验证码错误","top");
			var isValid = $(this).form('validate');
			return isValid;	// 返回false终止表单提交
		},
	    success:function(data){ 
	    	var data = eval('(' + data + ')');  
	        if(data.success){
	        	window.location.href="${ctx}/user/list.action";
	        }else{
	        	$.messager.alert('警告',data.msg,'error',function(){
	        		reloadValidCode();
	        	});
	        }
	    }    
	});  
}
</script>	
</body>

在controller层编写

        @RequestMapping("tologin")
	public ModelAndView tologin(){
		return new ModelAndView("user/login");
	}
        @RequestMapping("login")
        @ResponseBody
	public ResJson login(HttpServletRequest request , String user_name , String password){
		ResJson resJson = new ResJson();
		//根据user_name查询user
		User user = userService.login(user_name);
		if (user==null) {
			resJson.setSuccess(false);
			resJson.setMsg("用户名密码错误");
		}else {
			if (password.equals(user .getPassword())) {
				request.getSession().setAttribute(Const.SESSION_USER_NAME, user .getUser_name());
				request.getSession().setAttribute(Const.SESSION_USER, user);
				resJson.setSuccess(true);
			}else {
				resJson.setMsg("用户名密码错误");
			}
		}
		
		return resJson;
	} 

在Dao.xml文件里编写

<!--登录-->
  <select id="login" parameterType="com.model.user.User" resultType="com.model.user.User">
  	select
			   a.user_name,
			   a.password
		from 
				ztl_user a
		where a.user_name=#{user_name}
  </select>

最后在spring-mvc.xml中加入拦截器地址

        <!-- 拦截器  -->
	<mvc:interceptors>
		<bean class="//SystemInterceptor配置文件的地址" />
	</mvc:interceptors>

配置SystemInterceptor拦截器

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

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

public class Systemlnterceptor implements HandlerInterceptor{

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

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

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
		// TODO Auto-generated method stub
		//获取请求地址
		String requestURI = request.getRequestURI();
		//不需要拦截的请求
		String No=".*/((tologin)|(loginOpt)).*";
		//判断请求
		if (requestURI.matches(No)) {
			return true;
		}
		String id=(String)request.getSession().getAttribute("id");
		if (id!=null) {
			return true;
		}
		request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		return false;
	}

}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值