如何在Springboot中将拦截器添加到一个特定的URL中?

发布于 2025-02-07 10:44:18 字数 964 浏览 0 评论 0原文

我有一个如下:

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/create")
    public Map<String, Object> createAccount(User user) {
        return userService.createAccount(user);
    }

    @PostMapping("/login")
    public Map<String, Object> accountLogin(User user) {
        return userService.accountLogin(user);
    }

    @GetMapping("/activate")
    public Map<String, Object> activateAccount(String confirmationCode) {
        return userService.activateAccount(confirmationCode);
    }

    @PostMapping("/roleChange")
    public Map<String, Object> setUserRole(String uuid, String email, String roleId){
        return userService.setUserRole(uuid, email, roleId);
    }
}

现在,我想仅将interceptor添加到/user/rolechange。我知道您可以使用.excludepathpatterns()来排除所有其他路径,但是我很好奇是否有办法为Interpector指定一条路径。谢谢你!

I have a UserController class as below:

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/create")
    public Map<String, Object> createAccount(User user) {
        return userService.createAccount(user);
    }

    @PostMapping("/login")
    public Map<String, Object> accountLogin(User user) {
        return userService.accountLogin(user);
    }

    @GetMapping("/activate")
    public Map<String, Object> activateAccount(String confirmationCode) {
        return userService.activateAccount(confirmationCode);
    }

    @PostMapping("/roleChange")
    public Map<String, Object> setUserRole(String uuid, String email, String roleId){
        return userService.setUserRole(uuid, email, roleId);
    }
}

Now I wanna add an interceptor to /user/roleChange only. I know you can exclude all the other paths with .excludePathPatterns() but I'm just curious if there's a way to specify one path for the interceptor. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彻夜缠绵 2025-02-14 10:44:18

以下是Interceptor的示例代码,

@Configuration
public class AppConfig implements WebMvcConfigurer {

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomInterceptor()).addPathPatterns("/user");
    }
}

public class CustomInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("Hello..");
        return true;
    }
}

在此中,我有“拦截器”注册“/用户”。

registry.addinterceptor(new customInterceptor())。addathpatterns(“/user”);

Below is the sample code for interceptor

@Configuration
public class AppConfig implements WebMvcConfigurer {

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new CustomInterceptor()).addPathPatterns("/user");
    }
}

public class CustomInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("Hello..");
        return true;
    }
}

In this I have register "/user" for interceptor.

registry.addInterceptor(new CustomInterceptor()).addPathPatterns("/user");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文