当前位置: 首页 > news >正文

Spring Boot Vue 毕设系统讲解 3

目录

项目配置类

项目中配置的相关代码

spring Boot 拦截器相关知识

一、基于URL实现的拦截器:

二、基于注解的拦截器

三、把拦截器添加到配置中,相当于SpringMVC时的配置文件干的事儿:

项目配置类

项目中配置的相关代码

首先定义项目认证授权拦截器   AuthorizationInterceptor 把这个类注册为 bean  使用的是 @Bean注解

其次是重写 addInterceptors方法 将然后将注册的认证授权bean 添加到拦截器的链条当中,设置是所有请求都要过拦截器,出了static下面的静态资源不拦截

然后是重写 addResourceHandlers  这里是对项目的静态资源做定向解析,addResourceHandlers是请求路径.addResourceLocations 是资源的路径

spring Boot 拦截器相关知识

其实spring boot拦截器的配置方式和springMVC差不多,只有一些小的改变需要注意下就ok了。

下面主要介绍两种常用的拦截器:

一、基于URL实现的拦截器:
public class LoginInterceptor extends HandlerInterceptorAdapter{  @Override  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  String path = request.getServletPath();  if (path.matches(Const.NO_INTERCEPTOR_PATH)) {  //不需要的拦截直接过  return true;  } else {  // 这写你拦截需要干的事儿,比如取缓存,SESSION,权限判断等  System.out.println("====================================");  return true;  }  }  
} 


关键代码:path.matches(Const.NO_INTERCEPTOR_PATH 就是基于正则匹配的url。

public class Const {  public static final String SUCCESS = "SUCCESS";  public static final String ERROR = "ERROR";  public static final String FIALL = "FIALL";  /**********************对象和个体****************************/  public static final String SESSION_USER = "loginedAgent"; // 用户对象  public static final String SESSION_LOGINID = "sessionLoginID"; // 登录ID  public static final String SESSION_USERID = "sessionUserID"; // 当前用户对象ID编号  public static final String SESSION_USERNAME = "sessionUserName"; // 当前用户对象ID编号  public static final Integer PAGE = 10; // 默认分页数  public static final String SESSION_URL = "sessionUrl"; // 被记录的url  public static final String SESSION_SECURITY_CODE = "sessionVerifyCode"; // 登录页验证码 // 时间 缓存时间  public static final int TIMEOUT = 1800;// 秒  public static final String ON_LOGIN = "/logout.htm";  public static final String LOGIN_OUT = "/toLogout";  // 不验证URL anon:不验证/authc:受控制的  public static final String NO_INTERCEPTOR_PATH =".*/((.css)|(.js)|(images)|(login)|(anon)).*";  
} 

二、基于注解的拦截器

①创建注解:

/**  * 在需要登录验证的Controller的方法上使用此注解  */  
@Target({ElementType.METHOD})// 可用在方法名上  
@Retention(RetentionPolicy.RUNTIME)// 运行时有效  
public @interface LoginRequired {   
} 


②创建拦截器:

public class AuthorityInterceptor extends HandlerInterceptorAdapter{  @Override  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  // 如果不是映射到方法直接通过  if (!(handler instanceof HandlerMethod)) {  return true;  }  // ①:START 方法注解级拦截器  HandlerMethod handlerMethod = (HandlerMethod) handler;  Method method = handlerMethod.getMethod();  // 判断接口是否需要登录  LoginRequired methodmethodAnnotation = method.getAnnotation(LoginRequired.class);  // 有 @LoginRequired 注解,需要认证  if (methodAnnotation != null) {  // 这写你拦截需要干的事儿,比如取缓存,SESSION,权限判断等  System.out.println("====================================");  return true;  }  return true;  }  
} 
三、把拦截器添加到配置中,相当于SpringMVC时的配置文件干的事儿:
/**  * 和springmvc的webmvc拦截配置一样  * @author BIANP  */  
@Configuration  
public class WebConfigurer implements WebMvcConfigurer {  @Override  public void addInterceptors(InterceptorRegistry registry) {  // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录  registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**");  registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**");  }  @Bean  public LoginInterceptor LoginInterceptor() {  return new LoginInterceptor();  }  @Bean  public AuthorityInterceptor AuthorityInterceptor() {  return new AuthorityInterceptor();  }  
} 

1、一定要加@Configuration 这个注解,在启动的时候在会被加载。

2、有一些教程是用的“WebMvcConfigurerAdapter”,不过在spring5.0版本后这个类被丢弃了 WebMvcConfigurerAdapter ,虽然还可以用,但是看起来不好。

3、也有一些教程使用的WebMvcConfigurationSupport,我使用后发现,classpath:/META/resources/,classpath:/resources/,classpath:/static/,classpath:/public/不生效。具体可以原因,大家可以看下源码因为:WebMvcAutoConfiguration上有个条件注解:

@ConditionalOnMissingBean(WebMvcConfigurationSupport.class) 
所以还是建议使用WebMvcConfigurer, 其实springMVC很多东西,都可以搬到springboot中来使用,只需要把配置文件的模式,改成 对应@Configuration 类就好了。

http://www.lryc.cn/news/395338.html

相关文章:

  • Spring Boot对接大模型:实战价值与技巧
  • 完美解决NameError: name ‘file‘ is not defined的正确解决方法,亲测有效!!!
  • Witness Table 的由来
  • Python 3 AI 编程助手
  • 【nginx】nginx的配置文件到底是什么结构,到底怎么写?
  • 基于React 实现井字棋
  • 文件的换行符,Windows 的 CRLF 和 Linux 的 LF
  • 怎样优化 PostgreSQL 中对日期时间范围的模糊查询?
  • B端设计:任何不顾及用户体验的设计,都是在装样子,花架子
  • React@16.x(51)路由v5.x(16)- 手动实现文件目录参考
  • 从零开始读RocketMq源码(二)Message的发送详解
  • 怎样优化 PostgreSQL 中对布尔类型数据的查询?
  • mysql在linux系统下重置root密码
  • 设计模式探索:观察者模式
  • Perl语言入门到高级学习
  • DOM 基本操作 - 获取元素
  • Google 搜索引擎:便捷高效、精准查询,带来无与伦比的搜索体验
  • tomcat的介绍与优化
  • Python 插入、替换、提取、或删除Excel中的图片
  • 紧凑型建模的veriloga语句要怎么看?
  • 大语言模型系列-Transformer介绍
  • JavaDS —— 顺序表ArrayList
  • Sphinx 搜索配置
  • 如何在不关闭防火墙的情况下,让两台设备ping通
  • windows USB 设备驱动开发-USB 等时传输
  • 【文件共享 windows和linux】Windows Server 2016上开启文件夹共享,并在CentOS 7.4上访问和下载文件
  • 【知网CNKI-注册安全分析报告】
  • 【Python_GUI】tkinter常用组件——文本类组件
  • zdppy+onlyoffice+vue3解决文档加载和文档强制保存时弹出警告的问题
  • C语言从头学31——与字符串变量相关的几个函数