SpringMVC拦截器——实现登录验证拦截器的示例代码
程序员文章站
2024-03-05 22:52:01
本例实现登陆时的验证拦截,采用springmvc拦截器来实现
当用户点击到网站主页时要进行拦截,用户登录了才能进入网站主页,否则进入登陆页面
核心代码
首先是in...
本例实现登陆时的验证拦截,采用springmvc拦截器来实现
当用户点击到网站主页时要进行拦截,用户登录了才能进入网站主页,否则进入登陆页面
核心代码
首先是index.jsp,显示链接
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>" rel="external nofollow" > <title>首页</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <body> <div style="margin:0 auto;padding-top:100px;font-size:18px;" align="center"> <p><a href="loginpage.html" rel="external nofollow" >登陆</a></p> <p><a href="user/home.html" rel="external nofollow" >用户中心</a></p> <p><a href="exception.html" rel="external nofollow" >触发异常</a></p> </div> </body> </html>
controller类
package com.jikexueyuan.demo.springmvc.lesson4.controller; import javax.annotation.resource; import javax.servlet.http.httpservletrequest; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.requestparam; import com.jikexueyuan.demo.springmvc.lesson4.constant.global; import com.jikexueyuan.demo.springmvc.lesson4.exception.myexception; import com.jikexueyuan.demo.springmvc.lesson4.model.user; import com.jikexueyuan.demo.springmvc.lesson4.service.loginservice; /** * 这个例子讲解了如何定义mvc三层注解,使用@resource进行注入,以及使用@requestmapping、@requestparam 、@sessionattributes */ @controller public class logincontroller extends basecontroller { @resource loginservice service; @resource httpservletrequest request; @requestmapping("/exception") public void exception() throws myexception{ throw new myexception("测试springmvc中的异常捕获"); } @requestmapping("/loginpage") public string tologinpage(){ return "/web-inf/jsp/login.jsp"; } @requestmapping("/user/home") public string touserhome(){ return "/web-inf/jsp/userhome.jsp"; } @requestmapping("/logout") public string logout(){ request.getsession().removeattribute(global.user_session_key); return "redirect:/"; } @requestmapping(value = "/dologin", method = requestmethod.post) public string dologin(@requestparam string username, @requestparam string password){ try { user user = service.dologin(username, password); request.getsession().setattribute(global.user_session_key, user); return "redirect:/user/home.html"; } catch (exception e) { return "/web-inf/jsp/login.jsp"; } } }
当点击用户中心时,触发拦截,相关配置如下
在spring-mvc.xml中加上拦截配置,拦截所有url中包含/user/的请求,当然请求用户中心时就会触发这个拦截器了
<mvc:interceptors> <mvc:interceptor> <!-- 拦截所有url中包含/user/的请求 --> <mvc:mapping path="/user/**"/> <bean class="com.jikexueyuan.demo.springmvc.lesson4.interceptor.logininterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
然后是bean指向的具体的interceptor类,如果session保存的用户信息为null,则跳到login页面,posthandle和aftercompletion方法都不执行,反之都执行。
package com.jikexueyuan.demo.springmvc.lesson4.interceptor; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.web.servlet.handlerinterceptor; import org.springframework.web.servlet.modelandview; import com.jikexueyuan.demo.springmvc.lesson4.constant.global; public class logininterceptor implements handlerinterceptor { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { object user = request.getsession().getattribute(global.user_session_key); if (user == null) { system.out.println("尚未登录,调到登录页面"); response.sendredirect("/loginpage.html"); return false; } return true; } @override public void posthandle(httpservletrequest request, httpservletresponse response, object handler, modelandview modelandview) throws exception { system.out.println("posthandle"); } @override public void aftercompletion(httpservletrequest request, httpservletresponse response, object handler, exception ex) throws exception { system.out.println("aftercompletion"); } }
至此,简单的springmvc拦截器就完成了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。