栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

13-SpringBoot 登录拦截器

Java 更新时间:发布时间: 百科书网 趣学号
1.相关概念 1.实现效果

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

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

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.直接通过网址访问登录成功页面

被重定向到登录页

2.输入错误账号密码

被重定向到登录页

3.输入正确账号密码

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/986494.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号