spring boot 登录拦截器,spring 登录拦截器

  spring boot 登录拦截器,spring 登录拦截器

  

目录

1.相关概念1.实现效果2.实现步骤2.代码实现1.配置文件2.java代码3.前端代码3.运行测试

 

  

1.相关概念

 

  

1.实现效果

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

 

  

2.实现步骤

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

 

  

2.代码实现

 

  

1.配置文件

pom.xml

 

  ?可扩展标记语言版本=1.0 编码=UTF八号?项目xmlns= http://maven。阿帕奇。org/POM/4。0 .0 xmlns : xsi= http://www。w3。org/2001/XML schema-instance xsi :架构位置= http://maven。阿帕奇。org/POM/4。0 .0 https://maven.apache.org/xsd/maven-4.0.0.xsd模型版本4 .0 .0/模型版本父groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-parent/artifactId版本2.7-从存储库查找父级-/父级groupIdcom.limi/groupId artifactIdspringboot-test2/artifactId版本0。0 .1-快照/版本名称弹簧靴-测试2/名称描述跳羚的演示项目/描述属性Java。版本1.8/Java。版本/属性依赖关系groupIdorg.springframework.boot/groupId artifactId spring-boot-starter-web/artifactId/dependency依赖关系groupIdorg.springframework.boot/groupId artifact id spring-boot-dev tools/artifact id scope runtime/scope可选true/可选/依赖依赖关系groupIdorg.projectlombok/groupId artifactId lombok/artifactId可选true/可选/依赖依赖关系

  ;/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打进包内,让其只在编码的时候有用 --> <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;@SpringBootApplicationpublic 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;@Configurationpublic 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;@Controllerpublic 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

 

  

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

 

  

 

  被重定向到登录页

  

 

  2.输入错误账号密码

  

 

  被重定向到登录页

  

 

  3.输入正确账号密码

  

 

  

 

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

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

相关文章阅读

  • spring编程式事务处理,spring编程事务
  • spring编程式事务处理,spring编程事务,详解Spring学习之编程式事务管理
  • spring的核心功能模块有几个,列举一些重要的spring模块
  • spring的核心功能模块有几个,列举一些重要的spring模块,七个Spring核心模块详解
  • spring注解和springmvc的注解,SpringMVC常用注解
  • spring注解和springmvc的注解,SpringMVC常用注解,详解springmvc常用5种注解
  • spring实现ioc的四种方法,spring的ioc的三种实现方式
  • spring实现ioc的四种方法,spring的ioc的三种实现方式,简单实现Spring的IOC原理详解
  • spring事务失效问题分析及解决方案怎么做,spring 事务失效情况
  • spring事务失效问题分析及解决方案怎么做,spring 事务失效情况,Spring事务失效问题分析及解决方案
  • spring5.0新特性,spring4新特性
  • spring5.0新特性,spring4新特性,spring5新特性全面介绍
  • spring ioc以及aop原理,springmvc aop原理
  • spring ioc以及aop原理,springmvc aop原理,深入浅析Spring 的aop实现原理
  • Spring cloud网关,spring cloud zuul作用
  • 留言与评论(共有 条评论)
       
    验证码: