作者:不染心
更新时间:2021/7/22
🍃项目地址:https://download.csdn.net/download/qq_38234785/85560684?spm=1001.2014.3001.5503
一、项目目录
![](https://i-blog.csdnimg.cn/blog_migrate/ca8d5c4acbf2fac0151eb99eb84309fc.png)
🍃pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 引入redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
二、代码
🍃ReisUrlCountInterceptor
自定义一个url拦截器
package com.example.boot05web01.interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class ReisUrlCountInterceptor implements HandlerInterceptor {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String url = request.getRequestURI();
System.out.println("ReisUrlCountInterceptor 当前获取的url为:" + url);
System.out.println(stringRedisTemplate.opsForValue().increment(url));
return true;
}
}
🍃AdminWebConfig
将拦截器加入到拦截链中
package com.example.boot05web01.config;
import com.example.boot05web01.interceptor.ReisUrlCountInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
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;
import javax.xml.ws.Action;
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Resource
ReisUrlCountInterceptor reisUrlCountInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(reisUrlCountInterceptor)
// 对所有路径进行拦截
.addPathPatterns("/**")
// 对登陆url放行
.excludePathPatterns("/login", "/loginapi");
}
}
🍃HelloController
package com.example.boot05web01.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
@RestController
public class HelloController {
@GetMapping("/hello")
public Object hello(){
return "hello";
}
@GetMapping("/hello2")
public Object hello2(){
return "hello2";
}
/**
* /login路径在AdminWebConfig的拦截器中放行,不进行拦截
* @return
*/
@GetMapping("/login")
public String login(){
return "/login";
}
/**
* /loginapi路径在AdminWebConfig的拦截器中放行,不进行拦截
* @return
*/
@GetMapping("/loginapi")
public String loginapi(HttpSession session){
session.setAttribute("loginUser", "slh");
return "redirect:/";
}
}
三、测试
访问/hello
和/hello2
路径,拦截器都会进行拦截。
访问/login
和/loginapi
路径,拦截器都会进行放行。
访问:localhost:8080/hello
2021-07-22 20:30:12.514 INFO 5296 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
ReisUrlCountInterceptor 当前获取的url为:/hello
15
![](https://i-blog.csdnimg.cn/blog_migrate/3209e682e62192227abe2df086ead791.png)