基于SpringMVC實(shí)現(xiàn)網(wǎng)頁(yè)登錄攔截
1.簡(jiǎn)介
SpringMVC的處理器攔截器類(lèi)似于Servlet開(kāi)發(fā)中的過(guò)濾器Filter,用于對(duì)處理器進(jìn)行預(yù)處理和后處理。
攔截器和過(guò)濾器的區(qū)別在于攔截器使AOP思想的具體應(yīng)用
過(guò)濾器
- servlet規(guī)范中的一部分,任何java web工程都可以使用
- 在url-pattern中配置了/*之后,可以對(duì)所有要訪問(wèn)的資源進(jìn)行攔截
- 需要重寫(xiě)方法
攔截器
- SpringMVC框架自己的,只有使用了SpringMVC框架的工程才能使用
- 攔截器只會(huì)攔截訪問(wèn)的控制器方法, 如果訪問(wèn)的是jsp/html/css/image/js是不會(huì)進(jìn)行攔截的
- 不需要重寫(xiě)方法
2.自定義攔截器
??>新建一個(gè)Module,添加web支持
??>配置web.xml,applicationContext.xml,添加一個(gè)controller的包
??>編寫(xiě)測(cè)試?
@RestController
public class TestController {
@GetMapping("/t1")
public String test(){
System.out.println("TestController-->test()執(zhí)行了");
return "ok";
}
}
添加Artifact中的lib,以及配置Tomcat,啟動(dòng)測(cè)試出現(xiàn),證明Spring配置好了

??>編寫(xiě)攔截器
package com.hxl.config;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
//在請(qǐng)求處理的方法之前執(zhí)行
//return true;執(zhí)行下一個(gè)攔截器
//如果返回false就不執(zhí)行下一個(gè)攔截器
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("------------處理前------------");
return true;
}
//在請(qǐng)求處理方法執(zhí)行之后執(zhí)行
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("------------處理后------------");
}
//在dispatcherServlet處理后執(zhí)行,做清理工作.
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("------------清理------------");
}
}
??>在applicationContext.xml中配置攔截器
<!--關(guān)于攔截器的配置-->
<mvc:interceptors>
<mvc:interceptor>
<!--/** 包括路徑及其子路徑-->
<!--/admin/* 攔截的是/admin/add等等這種 , /admin/add/user不會(huì)被攔截-->
<!--/admin/** 攔截的是/admin/下的所有-->
<mvc:mapping path="/**"/>
<!--bean配置的就是攔截器-->
<bean class="com.hxl.config.MyInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
前面的我們都不動(dòng),運(yùn)行,我們可以看到效果

那么接下來(lái)就用一個(gè)實(shí)例來(lái)體驗(yàn)一下攔截器(登錄)
在WEB-INF下的所有頁(yè)面或者資源,只能通過(guò)controller或者servlet進(jìn)行訪問(wèn)
3. 登錄攔截
3.1 先做一個(gè)頁(yè)面
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/goLogin" rel="external nofollow" >登錄</a>
<a href="${pageContext.request.contextPath}/goMain" rel="external nofollow" >首頁(yè)</a>
</body>
</html>
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>首頁(yè)</h1>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄</title>
</head>
<body>
<h1>登錄頁(yè)面</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
用戶(hù)名:<input type="text" name="username">
密碼:<input type="text" name="password">
<input type="submit" value="登錄">
</form>
</body>
</html>
LoginController
package com.hxl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@RequestMapping("/goMain")
public String goMain(){
return "main";
}
@RequestMapping("/goLogin")
public String goLogin(){
return "login";
}
@RequestMapping("/login")
public String login(HttpSession session,String username,String password){
// 向session記錄用戶(hù)身份信息
System.out.println("接收前端==="+username);
session.setAttribute("user", username);
return "main";
}
}
測(cè)試


因?yàn)樵赪EB-INF下的 頁(yè)面我們不能直接訪問(wèn),所以在index中進(jìn)行跳轉(zhuǎn),然后請(qǐng)求到login.jsp。此時(shí)我們?cè)谶@里輸入,就會(huì)在session中攜帶賬號(hào)密碼。
但此時(shí)是不符合的,因?yàn)槲覀冞€沒(méi)有登錄就可以去首頁(yè),所以我們需要寫(xiě)一個(gè)攔截器的功能
3.2 登錄攔截
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<a href="${pageContext.request.contextPath}/user/goLogin" rel="external nofollow" >登錄</a>
<a href="${pageContext.request.contextPath}/user/goMain" rel="external nofollow" >首頁(yè)</a>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄</title>
</head>
<body>
<h1>登錄頁(yè)面</h1>
<form action="${pageContext.request.contextPath}/user/login" method="post">
用戶(hù)名:<input type="text" name="username">
密碼:<input type="text" name="password">
<input type="submit" value="登錄">
</form>
</body>
</html>
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>首頁(yè)</h1>
<p>${username}</p>
<p>
<a href="${pageContext.request.contextPath}/user/goOut" rel="external nofollow" >注銷(xiāo)</a>
</p>
</body>
</html>
LoginInterceptor
此時(shí)我們?nèi)?xiě)一個(gè)登錄的攔截器,來(lái)判斷它到底什么時(shí)候進(jìn)行攔截
package com.hxl.config;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
//放行:判斷什么情況下沒(méi)有登錄
//登錄頁(yè)面放行
if(request.getRequestURI().contains("goLogin")){
return true;
}
if(request.getRequestURI().contains("login")){
return true;
}
//用戶(hù)已登錄,第一次登錄的時(shí)候也是沒(méi)有session的。
if(session.getAttribute("user") != null){
return true;
}
//判斷什么情況下沒(méi)有登錄
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
}
LoginController
這里面我們有一個(gè)類(lèi)url,下面的請(qǐng)求都需要加上/user,在配置攔截器的時(shí)候可以加一個(gè),只攔截user請(qǐng)求下的。以及注銷(xiāo)功能
package com.hxl.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/user")
public class LoginController {
@RequestMapping("/goMain")
public String goMain(){
return "main";
}
@RequestMapping("/goLogin")
public String goLogin(){
return "login";
}
@RequestMapping("/login")
public String login(HttpSession session, String username, String password, Model model){
// 向session記錄用戶(hù)身份信息
System.out.println("接收前端==="+username);
session.setAttribute("user", username);
model.addAttribute("username",username);
return "main";
}
@RequestMapping("/goOut")
public String goOut(HttpSession session){
/*//銷(xiāo)毀,下面的好
session.invalidate();*/
//移除
session.removeAttribute("user");
return "main";
}
}
applicationContext.xml
<!--關(guān)于攔截器的配置-->
<mvc:interceptors>
<mvc:interceptor>
<!--/** 包括路徑及其子路徑-->
<!--/admin/* 攔截的是/admin/add等等這種 , /admin/add/user不會(huì)被攔截-->
<!--/admin/** 攔截的是/admin/下的所有-->
<mvc:mapping path="/**"/>
<!--bean配置的就是攔截器-->
<bean class="com.hxl.config.MyInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<!--user下面的請(qǐng)求-->
<mvc:mapping path="/user/**"/>
<bean class="com.hxl.config.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
測(cè)試
此時(shí)我們啟動(dòng)之后,如果沒(méi)有登錄,那么會(huì)重定向到goLogin頁(yè)面,然后登錄,登錄之后跳轉(zhuǎn)到main頁(yè)面,其中有注銷(xiāo)功能,沒(méi)有注銷(xiāo)之前可以去index頁(yè)面點(diǎn)擊首頁(yè)正常跳轉(zhuǎn),如果注銷(xiāo)了,session沒(méi)有了,那么就會(huì)跳轉(zhuǎn)到登錄頁(yè)面。?
以上就是基于SpringMVC實(shí)現(xiàn)網(wǎng)頁(yè)登錄攔截的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC網(wǎng)頁(yè)登錄攔截的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringCloud Alibaba 基本開(kāi)發(fā)框架搭建過(guò)程
這篇文章主要介紹了SpringCloud Alibaba 基本開(kāi)發(fā)框架搭建過(guò)程,開(kāi)發(fā)工具選用的idea,本文通過(guò)圖文實(shí)例相結(jié)合給大家分享搭建全過(guò)程,需要的朋友可以參考下2021-06-06
關(guān)于Mybatis動(dòng)態(tài)sql中test的坑點(diǎn)總結(jié)
這篇文章主要介紹了關(guān)于Mybatis動(dòng)態(tài)sql中test的坑點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Mybatis Generator自動(dòng)生成對(duì)應(yīng)文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Mybatis Generator自動(dòng)生成對(duì)應(yīng)的文件的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-09-09
使用Hutool編寫(xiě)生成隨機(jī)數(shù)的工具類(lèi)
Hutool?是一個(gè)?Java?工具類(lèi)庫(kù),提供了豐富的工具方法,其中?RandomUtil?是?Hutool?中用于生成隨機(jī)數(shù)的工具類(lèi),下面我們來(lái)看看它的具體使用吧2025-02-02
Spark使用IDEA編寫(xiě)wordcount的示例演示
這篇文章主要介紹了Spark使用IDEA編寫(xiě)wordcount的示例演示,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
使用SpringBoot簡(jiǎn)單實(shí)現(xiàn)無(wú)感知的刷新 Token功能
實(shí)現(xiàn)無(wú)感知的刷新 Token 是一種提升用戶(hù)體驗(yàn)的常用技術(shù),可以在用戶(hù)使用應(yīng)用時(shí)自動(dòng)更新 Token,無(wú)需用戶(hù)手動(dòng)干預(yù),這種技術(shù)在需要長(zhǎng)時(shí)間保持用戶(hù)登錄狀態(tài)的應(yīng)用中非常有用,以下是使用Spring Boot實(shí)現(xiàn)無(wú)感知刷新Token的一個(gè)場(chǎng)景案例和相應(yīng)的示例代碼2024-09-09
Springboot如何解決前端請(qǐng)求跨域的問(wèn)題
這篇文章主要介紹了Springboot如何解決前端請(qǐng)求跨域的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07

