springboot项目,指定某些接口不被拦截方法

1、监听器(Interceptor)拦截处理

在 Spring Boot应用中,如果你希望某些请求地址不被监听器(Interceptor)拦截处理,可以通过配置拦截器的路径来实现。拦截器通常用于在请求前后进行处理,比如权限验证、日志记录等,但有时候你可能希望某些请求可以跳过这些处理。

以下是实现这一目标的一般步骤:
1)定义拦截器:

@Component
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在此处编写你的拦截逻辑
        // 返回 true 表示继续处理请求,返回 false 表示结束请求
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 在请求处理之后进行处理
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在请求完成之后进行处理
    }
}

2)配置拦截器:

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private MyInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/**") // 拦截所有路径
                .excludePathPatterns("/public/**"); // 跳过 /public 下的路径
    }
}

addPathPatterns("/**") 表示拦截所有路径,而 excludePathPatterns("/public/**")
表示跳过以 /public/ 开头的路径,即不对这些路径应用拦截器逻辑。

2、绕过Spring Security 认证处理

在 Spring Security 中,AuthenticationEntryPoint 主要用于处理未经认证的请求,例如需要登录但用户未提供凭证时的处理逻辑。如果你希望某些接口请求不经过 AuthenticationEntryPoint 的认证处理,通常可以通过配置 Spring Security 的 HttpSecurity 来实现。

1)配置类中定义 HTTP Security:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll() // 允许访问的接口路径
                .anyRequest().authenticated() // 其他接口路径需要认证
            .and()
            .httpBasic()
                .authenticationEntryPoint(new MyAuthenticationEntryPoint()); // 设置自定义的认证入口点
    }
}

说明:

  • antMatchers("/public/**").permitAll() 指定了 /public/** 路径下的接口不需要认证,可以直接访问。
  • .anyRequest().authenticated() 告诉 Spring Security 其他所有请求都需要认证。
  • .httpBasic().authenticationEntryPoint(new MyAuthenticationEntryPoint()) 指定了自定义的 AuthenticationEntryPoint,你可以根据需要进行自定义逻辑,例如返回特定的错误信息或跳转页面。

2)自定义 AuthenticationEntryPoint:

/**
 * 认证失败处理类 返回未授权
 *
 * @author dongxiajun
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
        String msg = StringUtils.format("请求访问:{},认证失败,无法访问系统资源", request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(401, msg)));
    }
}

相关推荐

  1. springboot项目指定某些接口拦截方法

    2024-07-13 14:06:02       15 阅读
  2. Android 在AMS中拦截某个指定Activity的启动

    2024-07-13 14:06:02       27 阅读
  3. springboot项目启动时打印全部接口方法

    2024-07-13 14:06:02       34 阅读
  4. springboot中使用aop实现方法拦截处理

    2024-07-13 14:06:02       55 阅读
  5. SpringBoot】测试Control接口方法

    2024-07-13 14:06:02       19 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 14:06:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 14:06:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 14:06:02       57 阅读
  4. Python语言-面向对象

    2024-07-13 14:06:02       68 阅读

热门阅读

  1. 无人机的工作原理

    2024-07-13 14:06:02       14 阅读
  2. js实现一键任意html元素生成截图功能

    2024-07-13 14:06:02       19 阅读
  3. 一、字符串/数组

    2024-07-13 14:06:02       20 阅读
  4. 2024年城市客运安全员考试题库及答案

    2024-07-13 14:06:02       17 阅读
  5. SwiftBrush算法与代码解读

    2024-07-13 14:06:02       20 阅读
  6. 005-基于Sklearn的机器学习入门:逻辑回归

    2024-07-13 14:06:02       28 阅读
  7. opencv—常用函数学习_“干货“_总

    2024-07-13 14:06:02       22 阅读
  8. Web组成架构

    2024-07-13 14:06:02       23 阅读