最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot圖文并茂講解登錄攔截器

 更新時(shí)間:2022年06月27日 09:08:43   作者:鳴鼓ming  
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了,下面這篇文章主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)登陸攔截器功能的相關(guān)資料,需要的朋友可以參考下

1.相關(guān)概念

1.實(shí)現(xiàn)效果

當(dāng)沒有輸入正確的賬號(hào)密碼登錄成功時(shí), 除了登錄頁(yè),其他頁(yè)面都無(wú)法訪問(靜態(tài)資源要放行)

2.實(shí)現(xiàn)步驟

  • 編寫一個(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";  //沒有使用模板引擎, 所以要帶上后綴
    }
    @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.直接通過網(wǎng)址訪問登錄成功頁(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)文章

  • SpringBoot項(xiàng)目中控制臺(tái)日志的保存配置操作

    SpringBoot項(xiàng)目中控制臺(tái)日志的保存配置操作

    這篇文章主要介紹了SpringBoot項(xiàng)目中控制臺(tái)日志的保存配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 老生常談java路徑中的反斜杠和斜杠的區(qū)別

    老生常談java路徑中的反斜杠和斜杠的區(qū)別

    下面小編就為大家?guī)?lái)一篇老生常談java路徑中的反斜杠和斜杠的區(qū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2017-04-04
  • Spring Shell打Jar包時(shí)常用小技巧

    Spring Shell打Jar包時(shí)常用小技巧

    這篇文章主要介紹了Spring Shell打Jar包時(shí)常用小技巧,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Java設(shè)計(jì)模式之單例模式簡(jiǎn)析

    Java設(shè)計(jì)模式之單例模式簡(jiǎn)析

    這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式簡(jiǎn)析,單例模式是常用的設(shè)計(jì)模式,在我們的系統(tǒng)乃至在框架中都普遍被用到,單例模式就是使一個(gè)類有且只有一個(gè)實(shí)例用于外部訪問,這樣大大的節(jié)省了系統(tǒng)的資源,需要的朋友可以參考下
    2023-12-12
  • Spring Boot 集成 Kafka的詳細(xì)步驟

    Spring Boot 集成 Kafka的詳細(xì)步驟

    Spring Boot與Kafka的集成使得消息隊(duì)列的使用變得更加簡(jiǎn)單和高效,可以配置 Kafka、實(shí)現(xiàn)生產(chǎn)者和消費(fèi)者,并利用 Spring Boot 提供的功能處理消息流,以下是 Spring Boot 集成 Kafka 的詳細(xì)步驟,包括配置、生產(chǎn)者和消費(fèi)者的實(shí)現(xiàn)以及一些高級(jí)特性,感興趣的朋友一起看看吧
    2024-07-07
  • Java中ConcurrentHashMap和Hashtable的區(qū)別

    Java中ConcurrentHashMap和Hashtable的區(qū)別

    ConcurrentHashMap?和?Hashtable?都是用于在Java中實(shí)現(xiàn)線程安全的哈希表數(shù)據(jù)結(jié)構(gòu)的類,但它們有很多區(qū)別,本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下
    2023-10-10
  • Java靜態(tài)泛型使用方法實(shí)例解析

    Java靜態(tài)泛型使用方法實(shí)例解析

    這篇文章主要介紹了Java靜態(tài)泛型使用方法實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • MybatisPlus自動(dòng)填充時(shí)間的配置類實(shí)現(xiàn)

    MybatisPlus自動(dòng)填充時(shí)間的配置類實(shí)現(xiàn)

    本文介紹了如何在MyBatis-Plus中實(shí)現(xiàn)自動(dòng)填充時(shí)間的功能,通過實(shí)現(xiàn)MetaObjectHandler接口,重寫insertFill()和updateFill()方法,分別在插入和更新時(shí)填充創(chuàng)建時(shí)間和更新時(shí)間,感興趣的可以了解一下
    2024-12-12
  • Java實(shí)現(xiàn)限流接口的示例詳解

    Java實(shí)現(xiàn)限流接口的示例詳解

    限流是對(duì)某一時(shí)間窗口內(nèi)的請(qǐng)求數(shù)進(jìn)行限制,保持系統(tǒng)的可用性和穩(wěn)定性,防止因流量暴增而導(dǎo)致的系統(tǒng)運(yùn)行緩慢或宕機(jī),本文主要來(lái)和大家聊聊如何使用java實(shí)現(xiàn)限流接口,感興趣的可以了解下
    2023-12-12
  • Java實(shí)現(xiàn)郵件發(fā)送的過程及代碼詳解

    Java實(shí)現(xiàn)郵件發(fā)送的過程及代碼詳解

    這篇文章主要介紹了Java實(shí)現(xiàn)郵件發(fā)送的過程及代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論

贵定县| 贡觉县| 凭祥市| 西青区| 普定县| 南涧| 民县| 新田县| 罗源县| 玛曲县| 洛浦县| 长武县| 凤凰县| 华容县| 成武县| 河曲县| 拉孜县| 特克斯县| 富民县| 罗城| 博白县| 四会市| 花莲市| 泰和县| 延安市| 宁海县| 东乡族自治县| 荆州市| 龙川县| 六枝特区| 木兰县| 韩城市| 林西县| 砚山县| 芦溪县| 凌海市| 建平县| 丹棱县| 乌兰浩特市| 石城县| 竹溪县|