SpringBoot图文并茂讲解登录拦截器

1.相关概念

1.实现效果

当没有输入正确的账号密码登录成功时, 除了登录页,其他页面都无法访问(静态资源要放行)

2.实现步骤

  • 编写一个拦截器实现HandlerInterceptor接口
  • 拦截器注册到容器中(实现WebMvcConfigurer的addInterceptors())
  • 指定拦截规则(注意,如果是拦截所有,静态资源也会被拦截)

2.代码实现

SpringBoot图文并茂讲解登录拦截器_第1张图片

1.配置文件

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.0
         
    
    com.limi
    springboot-test2
    0.0.1-SNAPSHOT
    springboot-test2
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.springframework.boot
                            spring-boot-configuration-processor
                        
                    
                
            
        
    

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("/**")  //所有请求都被拦截包括静态资源
                .excludePathPatterns("/index", "/login") //放行的网络请求,
                .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("======执行控制器中方法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




    
    Title
    


    

请输入账号密码进行登录!

账号
密码

success.html




    
    Title
    

登录成功!

3.运行测试

1.直接通过网址访问登录成功页面

SpringBoot图文并茂讲解登录拦截器_第2张图片

被重定向到登录页

SpringBoot图文并茂讲解登录拦截器_第3张图片

2.输入错误账号密码

SpringBoot图文并茂讲解登录拦截器_第4张图片

被重定向到登录页

SpringBoot图文并茂讲解登录拦截器_第5张图片

3.输入正确账号密码

SpringBoot图文并茂讲解登录拦截器_第6张图片

SpringBoot图文并茂讲解登录拦截器_第7张图片

到此这篇关于SpringBoot图文并茂讲解登录拦截器的文章就介绍到这了,更多相关SpringBoot登录拦截器内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(SpringBoot图文并茂讲解登录拦截器)