SpringBoot圖文并茂講解登錄攔截器
1.相關(guān)概念
1.實(shí)現(xiàn)效果
當(dāng)沒(méi)有輸入正確的賬號(hào)密碼登錄成功時(shí), 除了登錄頁(yè),其他頁(yè)面都無(wú)法訪問(wèn)(靜態(tài)資源要放行)
2.實(shí)現(xiàn)步驟
- 編寫(xiě)一個(gè)攔截器實(shí)現(xiàn)HandlerInterceptor接口
- 攔截器注冊(cè)到容器中(實(shí)現(xiàn)WebMvcConfigurer的addInterceptors())
- 指定攔截規(guī)則(注意,如果是攔截所有,靜態(tài)資源也會(huì)被攔截)
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>
<!-- 下面插件作用是工程打包時(shí),不將spring-boot-configuration-processor打進(jìn)包內(nèi),讓其只在編碼的時(shí)候有用 -->
<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) //未登錄
{
//未登錄, 重定向到登錄頁(yè)
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())//攔截器注冊(cè)到容器中
.addPathPatterns("/**") //所有請(qǐng)求都被攔截包括靜態(tài)資源
.excludePathPatterns("/index", "/login") //放行的網(wǎng)絡(luò)請(qǐng)求,
.excludePathPatterns("/view/index.html","/css/**","/images/**", "/js/**"); //放行的資源請(qǐng)求
}
}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"; //沒(méi)有使用模板引擎, 所以要帶上后綴
}
@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)) { //賬號(hào)密碼匹配成功
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>請(qǐng)輸入賬號(hào)密碼進(jìn)行登錄!</h1>
<form action="login" method="post">
賬號(hào)<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)行測(cè)試
1.直接通過(guò)網(wǎng)址訪問(wèn)登錄成功頁(yè)面

被重定向到登錄頁(yè)

2.輸入錯(cuò)誤賬號(hào)密碼

被重定向到登錄頁(yè)

3.輸入正確賬號(hào)密碼


到此這篇關(guān)于SpringBoot圖文并茂講解登錄攔截器的文章就介紹到這了,更多相關(guān)SpringBoot登錄攔截器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于PreparedStatement的setObject作用及說(shuō)明
這篇文章主要介紹了關(guān)于PreparedStatement的setObject作用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot使用注解進(jìn)行分頁(yè)的實(shí)現(xiàn)示例
分頁(yè)使用可以說(shuō)非常普遍了,有時(shí)候會(huì)需要非常靈活的方式去開(kāi)啟或關(guān)閉分頁(yè),嘗試使用一下注解的方式來(lái)進(jìn)行分頁(yè),本文主要介紹了SpringBoot使用注解進(jìn)行分頁(yè)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
java中@JSONField和@JsonProperty注解的使用說(shuō)明及對(duì)比
@JSONField與@JsonProperty隸屬兩個(gè)不同的包,前者是阿里系的fastjson包,后者是spring?boot官方使用的jackson包,本文主要介紹了java中@JSONField和@JsonProperty注解的使用說(shuō)明及對(duì)比,感興趣的可以了解一下2023-11-11
Java結(jié)構(gòu)型設(shè)計(jì)模式之組合模式詳解
組合模式,又叫部分整體模式,它創(chuàng)建了對(duì)象組的數(shù)據(jù)結(jié)構(gòu)組合模式使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的訪問(wèn)具有一致性。本文將通過(guò)示例為大家詳細(xì)介紹一下組合模式,需要的可以參考一下2022-09-09
java基礎(chǔ)之接口組成更新的實(shí)現(xiàn)
本文主要介紹了java基礎(chǔ)之接口組成更新的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Spring MVC打印@RequestBody、@Response日志的方法
這篇文章主要介紹了Spring MVC打印@RequestBody、@Response日志的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02

