SpringBoot * 的使用
作者:久曲键 时间:2023-04-27 23:27:27
一、 * 简介
* 通常通过 * 的方式来执行。
* 的生命周期由IoC
容器管理,可以通过注入等方式来获取其他Bean
的实例,使用更方便。
二、 * 配置使用方式
1、过滤器 * 作用范围
2、 * 的使用
示例代码如下:
package com.rongrong.wiki.interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* * :Spring框架特有的,常用于登录校验,权限校验,请求日志打印 /login
*/
@Component
public class LogInterceptor implements HandlerInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(LogInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 打印请求信息
LOG.info("------------- LogInterceptor 开始 -------------");
LOG.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
LOG.info("远程地址: {}", request.getRemoteAddr());
long startTime = System.currentTimeMillis();
request.setAttribute("requestStartTime", startTime);
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
long startTime = (Long) request.getAttribute("requestStartTime");
LOG.info("------------- LogInterceptor 结束 耗时:{} ms -------------", System.currentTimeMillis() - startTime);
}
}
将 * 加入到配置中,示例代码如下:
package com.rongrong.wiki.config;
import com.rongrong.wiki.interceptor.LogInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.annotation.Resource;
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {
@Resource
LogInterceptor loginInterceptor;
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor)
.addPathPatterns("/**")
.excludePathPatterns("/login");
}
}
重新编译启动,查看结果如下:
三、知识点总结
1、 * 的使用
返回
true
会往后执行返回
false
会结束,可以利用这点来做权限拦截addPathPatterns()
,要拦截请求excludePathPatterns()
,排除请求,不拦截
2、 * 和过滤器的相同与不同
都可以用来统一处理请求,比如:打印日志、权限控制
过滤器依赖于
servlet
容器, * 依赖Spring
框架过滤器不用注入其它类, * 可注入其它类,基于这一点,建议能用 * 的都用 *
来源:https://www.cnblogs.com/longronglang/p/15470625.html
标签:SpringBoot, ,
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
轻量级声明式的Http库——Feign的独立使用
2022-06-05 04:36:19
C++ socket实现miniFTP
2022-05-01 11:45:16
![](https://img.aspxhome.com/file/2023/2/112302_0s.jpg)
深入C#中get与set的详解
2022-05-29 21:04:10
Nginx启用压缩及开启gzip 压缩的方法
2021-09-20 21:19:25
![](https://img.aspxhome.com/file/2023/8/66628_0s.png)
浅谈java的byte数组的不同写法
2023-03-10 07:53:12
java volatile关键字的含义详细介绍
2021-11-01 16:52:17
![](https://img.aspxhome.com/file/2023/9/132029_0s.jpg)
SpringBoot实现监控Actuator,关闭redis监测
2021-08-01 04:01:33
![](https://img.aspxhome.com/file/2023/6/130006_0s.jpg)
Android本地存储SharedPreferences详解
2023-06-24 17:01:51
JAVA 根据设置的概率生成随机数的方法
2023-07-15 00:13:37
手动编译C#代码的方法
2021-05-27 05:42:40
![](https://img.aspxhome.com/file/2023/1/91381_0s.png)
关于@Scheduled不执行的原因分析
2021-06-27 17:13:20
![](https://img.aspxhome.com/file/2023/9/67619_0s.png)
Kotlin HttpURLConnection与服务器交互实现方法详解
2022-11-22 02:12:00
C#实现DataSet内数据转化为Excel和Word文件的通用类完整实例
2023-05-29 21:50:02
Android中的windowSoftInputMode属性详解
2022-11-14 06:17:40
![](https://img.aspxhome.com/file/2023/4/138184_0s.png)
C#实现绘制鼠标的示例代码
2023-06-11 04:40:54
![](https://img.aspxhome.com/file/2023/4/77234_0s.png)
详解MyBatis-Puls中saveBatch批量添加慢的问题
2021-10-14 02:36:27
![](https://img.aspxhome.com/file/2023/9/64369_0s.png)
Android控件PopupWindow模仿ios底部弹窗
2023-03-09 20:42:10
![](https://img.aspxhome.com/file/2023/0/137990_0s.gif)
SpringBoot实现redis缓存菜单列表
2023-11-24 07:39:20
WinForm中comboBox控件数据绑定实现方法
2023-04-23 07:37:28
Android中自定义标题栏样式的两种方法
2022-05-03 09:06:01