Redis缓存url访问次数(SpringBoot拦截器)

作者:不染心
更新时间:2021/7/22
🍃项目地址:https://download.csdn.net/download/qq_38234785/85560684?spm=1001.2014.3001.5503

一、项目目录

🍃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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不染心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值