SpringMVC攔截器——實(shí)現(xiàn)登錄驗(yàn)證攔截器的示例代碼
本例實(shí)現(xiàn)登陸時(shí)的驗(yàn)證攔截,采用SpringMVC攔截器來實(shí)現(xiàn)
當(dāng)用戶點(diǎn)擊到網(wǎng)站主頁(yè)時(shí)要進(jìn)行攔截,用戶登錄了才能進(jìn)入網(wǎng)站主頁(yè),否則進(jìn)入登陸頁(yè)面
核心代碼
首先是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>首頁(yè)</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" >觸發(fā)異常</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;
/**
* 這個(gè)例子講解了如何定義MVC三層注解,使用@Resource進(jìn)行注入,以及使用@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("測(cè)試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";
}
}
}
當(dāng)點(diǎn)擊用戶中心時(shí),觸發(fā)攔截,相關(guān)配置如下
在spring-mvc.xml中加上攔截配置,攔截所有URL中包含/user/的請(qǐng)求,當(dāng)然請(qǐng)求用戶中心時(shí)就會(huì)觸發(fā)這個(gè)攔截器了
<mvc:interceptors>
<mvc:interceptor>
<!-- 攔截所有URL中包含/user/的請(qǐng)求 -->
<mvc:mapping path="/user/**"/>
<bean class="com.jikexueyuan.demo.springmvc.lesson4.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
然后是bean指向的具體的interceptor類,如果session保存的用戶信息為null,則跳到login頁(yè)面,postHandle和afterCompletion方法都不執(zhí)行,反之都執(zhí)行。
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("尚未登錄,調(diào)到登錄頁(yè)面");
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");
}
}
至此,簡(jiǎn)單的springmvc攔截器就完成了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+jsp項(xiàng)目啟動(dòng)出現(xiàn)404的解決方法
這篇文章主要介紹了SpringBoot+jsp項(xiàng)目啟動(dòng)出現(xiàn)404的解決方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-03-03
淺談Java中Spring Boot的優(yōu)勢(shì)
在本篇文章中小編給大家分析了Java中Spring Boot的優(yōu)勢(shì)以及相關(guān)知識(shí)點(diǎn)內(nèi)容,興趣的朋友們可以學(xué)習(xí)參考下。2018-09-09
Idea如何導(dǎo)入一個(gè)SpringBoot項(xiàng)目的方法(圖文教程)
這篇文章主要介紹了Idea如何導(dǎo)入一個(gè)SpringBoot項(xiàng)目的方法(圖文教程),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
java實(shí)現(xiàn)實(shí)時(shí)通信聊天程序
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)實(shí)時(shí)通信聊天程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
SpringBoot多種環(huán)境自由切換的實(shí)現(xiàn)
本文主要介紹了SpringBoot多種環(huán)境自由切換的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Java Thread之Sleep()使用方法及總結(jié)
這篇文章主要介紹了Java Thread之Sleep()使用方法及總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
SpringSecurity自定義AuthenticationProvider無法@Autowire的解決
這篇文章主要介紹了SpringSecurity自定義AuthenticationProvider無法@Autowire的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

