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)));
}
}
到此这篇关于springboot中如何指定某些接口不被拦截的文章就介绍到这了,更多相关springboot指定接口不被拦截内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论