基于SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截
1.簡介
SpringMVC的處理器攔截器類似于Servlet開發(fā)中的過濾器Filter,用于對(duì)處理器進(jìn)行預(yù)處理和后處理。
攔截器和過濾器的區(qū)別在于攔截器使AOP思想的具體應(yīng)用
過濾器
- servlet規(guī)范中的一部分,任何java web工程都可以使用
- 在url-pattern中配置了/*之后,可以對(duì)所有要訪問的資源進(jìn)行攔截
- 需要重寫方法
攔截器
- SpringMVC框架自己的,只有使用了SpringMVC框架的工程才能使用
- 攔截器只會(huì)攔截訪問的控制器方法, 如果訪問的是jsp/html/css/image/js是不會(huì)進(jìn)行攔截的
- 不需要重寫方法
2.自定義攔截器
??>新建一個(gè)Module,添加web支持
??>配置web.xml,applicationContext.xml,添加一個(gè)controller的包
??>編寫測(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配置好了

??>編寫攔截器
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)行,我們可以看到效果

那么接下來就用一個(gè)實(shí)例來體驗(yàn)一下攔截器(登錄)
在WEB-INF下的所有頁面或者資源,只能通過controller或者servlet進(jìn)行訪問
3. 登錄攔截
3.1 先做一個(gè)頁面
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" >首頁</a>
</body>
</html>
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>首頁</h1>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄</title>
</head>
<body>
<h1>登錄頁面</h1>
<form action="${pageContext.request.contextPath}/login" method="post">
用戶名:<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記錄用戶身份信息
System.out.println("接收前端==="+username);
session.setAttribute("user", username);
return "main";
}
}
測(cè)試


因?yàn)樵赪EB-INF下的 頁面我們不能直接訪問,所以在index中進(jìn)行跳轉(zhuǎn),然后請(qǐng)求到login.jsp。此時(shí)我們?cè)谶@里輸入,就會(huì)在session中攜帶賬號(hào)密碼。
但此時(shí)是不符合的,因?yàn)槲覀冞€沒有登錄就可以去首頁,所以我們需要寫一個(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" >首頁</a>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄</title>
</head>
<body>
<h1>登錄頁面</h1>
<form action="${pageContext.request.contextPath}/user/login" method="post">
用戶名:<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>首頁</h1>
<p>${username}</p>
<p>
<a href="${pageContext.request.contextPath}/user/goOut" rel="external nofollow" >注銷</a>
</p>
</body>
</html>
LoginInterceptor
此時(shí)我們?nèi)懸粋€(gè)登錄的攔截器,來判斷它到底什么時(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();
//放行:判斷什么情況下沒有登錄
//登錄頁面放行
if(request.getRequestURI().contains("goLogin")){
return true;
}
if(request.getRequestURI().contains("login")){
return true;
}
//用戶已登錄,第一次登錄的時(shí)候也是沒有session的。
if(session.getAttribute("user") != null){
return true;
}
//判斷什么情況下沒有登錄
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
}
LoginController
這里面我們有一個(gè)類url,下面的請(qǐng)求都需要加上/user,在配置攔截器的時(shí)候可以加一個(gè),只攔截user請(qǐng)求下的。以及注銷功能
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記錄用戶身份信息
System.out.println("接收前端==="+username);
session.setAttribute("user", username);
model.addAttribute("username",username);
return "main";
}
@RequestMapping("/goOut")
public String goOut(HttpSession session){
/*//銷毀,下面的好
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)之后,如果沒有登錄,那么會(huì)重定向到goLogin頁面,然后登錄,登錄之后跳轉(zhuǎn)到main頁面,其中有注銷功能,沒有注銷之前可以去index頁面點(diǎn)擊首頁正常跳轉(zhuǎn),如果注銷了,session沒有了,那么就會(huì)跳轉(zhuǎn)到登錄頁面。?
以上就是基于SpringMVC實(shí)現(xiàn)網(wǎng)頁登錄攔截的詳細(xì)內(nèi)容,更多關(guān)于SpringMVC網(wǎng)頁登錄攔截的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringCloud Alibaba 基本開發(fā)框架搭建過程
這篇文章主要介紹了SpringCloud Alibaba 基本開發(fā)框架搭建過程,開發(fā)工具選用的idea,本文通過圖文實(shí)例相結(jié)合給大家分享搭建全過程,需要的朋友可以參考下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
使用SpringBoot簡單實(shí)現(xiàn)無感知的刷新 Token功能
實(shí)現(xiàn)無感知的刷新 Token 是一種提升用戶體驗(yàn)的常用技術(shù),可以在用戶使用應(yīng)用時(shí)自動(dòng)更新 Token,無需用戶手動(dòng)干預(yù),這種技術(shù)在需要長時(shí)間保持用戶登錄狀態(tài)的應(yīng)用中非常有用,以下是使用Spring Boot實(shí)現(xiàn)無感知刷新Token的一個(gè)場景案例和相應(yīng)的示例代碼2024-09-09

