SpringBoot圖文并茂講解登錄攔截器
1.相關(guān)概念
1.實(shí)現(xiàn)效果
當(dāng)沒有輸入正確的賬號密碼登錄成功時, 除了登錄頁,其他頁面都無法訪問(靜態(tài)資源要放行)
2.實(shí)現(xiàn)步驟
- 編寫一個攔截器實(shí)現(xiàn)HandlerInterceptor接口
- 攔截器注冊到容器中(實(shí)現(xiàn)WebMvcConfigurer的addInterceptors())
- 指定攔截規(guī)則(注意,如果是攔截所有,靜態(tài)資源也會被攔截)
2.代碼實(shí)現(xiàn)

1.配置文件
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.limi</groupId>
<artifactId>springboot-test2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-test2</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- 下面插件作用是工程打包時,不將spring-boot-configuration-processor打進(jìn)包內(nèi),讓其只在編碼的時候有用 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>application.properties
server.port=8080
2.java代碼
SpringbootTest2Application
package com.limi.springboottest2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootTest2Application {
public static void main(String[] args) {
//1、返回我們IOC容器
ConfigurableApplicationContext run = SpringApplication.run(SpringbootTest2Application.class, args);
}
}LoginInterceptor
package com.limi.springboottest2.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("=========LoginInterceptor preHandle==========");
HttpSession session = request.getSession();
if(session.getAttribute("username")==null) //未登錄
{
//未登錄, 重定向到登錄頁
response.sendRedirect("/index"); //重定向到登錄controller
return false;//攔截
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("==========LoginInterceptor postHandle==========");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("==========LoginInterceptor afterCompletion==========");
}
}WebConfig
package com.limi.springboottest2.config;
import com.limi.springboottest2.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())//攔截器注冊到容器中
.addPathPatterns("/**") //所有請求都被攔截包括靜態(tài)資源
.excludePathPatterns("/index", "/login") //放行的網(wǎng)絡(luò)請求,
.excludePathPatterns("/view/index.html","/css/**","/images/**", "/js/**"); //放行的資源請求
}
}HelloController
package com.limi.springboottest2.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
@Controller
public class HelloController {
@GetMapping("/index")
public String index(){
return "/view/index.html"; //沒有使用模板引擎, 所以要帶上后綴
}
@PostMapping("/login")
public String login(HttpSession session, String username, String password){
System.out.println("username:"+username+" password:"+password);
if("andy".equals(username)&&"123456".equals(password)) { //賬號密碼匹配成功
session.setAttribute("username", username);
return "redirect:/success";
}
return "redirect:/index";
}
@GetMapping("/success")
public ModelAndView test1(HttpSession session){
System.out.println("======執(zhí)行控制器中方法success======");
String name = (String)session.getAttribute("username");
ModelAndView mv = new ModelAndView();
mv.addObject("name", name);
mv.setViewName("/view/success.html");
return mv;
}
}3.前端代碼
index.css
h1{
color: blueviolet;
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="./css/index.css" rel="external nofollow" >
</head>
<body>
<h1>請輸入賬號密碼進(jìn)行登錄!</h1>
<form action="login" method="post">
賬號<input type="text" name="username"><br>
密碼<input type="password" name="password"><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<h1>登錄成功!</h1>
</head>
<body>
</body>
</html>3.運(yùn)行測試
1.直接通過網(wǎng)址訪問登錄成功頁面

被重定向到登錄頁

2.輸入錯誤賬號密碼

被重定向到登錄頁

3.輸入正確賬號密碼


到此這篇關(guān)于SpringBoot圖文并茂講解登錄攔截器的文章就介紹到這了,更多相關(guān)SpringBoot登錄攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot項(xiàng)目中控制臺日志的保存配置操作
這篇文章主要介紹了SpringBoot項(xiàng)目中控制臺日志的保存配置操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Java中ConcurrentHashMap和Hashtable的區(qū)別
ConcurrentHashMap?和?Hashtable?都是用于在Java中實(shí)現(xiàn)線程安全的哈希表數(shù)據(jù)結(jié)構(gòu)的類,但它們有很多區(qū)別,本文就來詳細(xì)的介紹一下,感興趣的可以了解一下2023-10-10
MybatisPlus自動填充時間的配置類實(shí)現(xiàn)
本文介紹了如何在MyBatis-Plus中實(shí)現(xiàn)自動填充時間的功能,通過實(shí)現(xiàn)MetaObjectHandler接口,重寫insertFill()和updateFill()方法,分別在插入和更新時填充創(chuàng)建時間和更新時間,感興趣的可以了解一下2024-12-12
Java實(shí)現(xiàn)郵件發(fā)送的過程及代碼詳解
這篇文章主要介紹了Java實(shí)現(xiàn)郵件發(fā)送的過程及代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

