SpringBoot圖文并茂講解登錄攔截器
1.相關(guān)概念
1.實現(xiàn)效果
當沒有輸入正確的賬號密碼登錄成功時, 除了登錄頁,其他頁面都無法訪問(靜態(tài)資源要放行)
2.實現(xiàn)步驟
- 編寫一個攔截器實現(xiàn)HandlerInterceptor接口
- 攔截器注冊到容器中(實現(xiàn)WebMvcConfigurer的addInterceptors())
- 指定攔截規(guī)則(注意,如果是攔截所有,靜態(tài)資源也會被攔截)
2.代碼實現(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打進包內(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>請輸入賬號密碼進行登錄!</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.運行測試
1.直接通過網(wǎng)址訪問登錄成功頁面
被重定向到登錄頁
2.輸入錯誤賬號密碼
被重定向到登錄頁
3.輸入正確賬號密碼
到此這篇關(guān)于SpringBoot圖文并茂講解登錄攔截器的文章就介紹到這了,更多相關(guān)SpringBoot登錄攔截器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于PreparedStatement的setObject作用及說明
這篇文章主要介紹了關(guān)于PreparedStatement的setObject作用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03java中@JSONField和@JsonProperty注解的使用說明及對比
@JSONField與@JsonProperty隸屬兩個不同的包,前者是阿里系的fastjson包,后者是spring?boot官方使用的jackson包,本文主要介紹了java中@JSONField和@JsonProperty注解的使用說明及對比,感興趣的可以了解一下2023-11-11Java結(jié)構(gòu)型設(shè)計模式之組合模式詳解
組合模式,又叫部分整體模式,它創(chuàng)建了對象組的數(shù)據(jù)結(jié)構(gòu)組合模式使得用戶對單個對象和組合對象的訪問具有一致性。本文將通過示例為大家詳細介紹一下組合模式,需要的可以參考一下2022-09-09Spring MVC打印@RequestBody、@Response日志的方法
這篇文章主要介紹了Spring MVC打印@RequestBody、@Response日志的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02