英文:
Spring Boot Controller to handle all requests for preprocessing before forwarding to appropriate Controller
问题
我在我的Spring Boot应用程序中有一系列的Rest API控制器,它们具有与特定URL相匹配的请求映射。我需要更改我的实现,始终确保所有请求都有一个特定的自定义标头。如果标头不存在,我希望拒绝该请求。如果存在,我希望转发到适当的控制器,这与我的当前实现相同。
在Spring Boot中是否有一种方法可以在不修改现有控制器的情况下实现这一点?即使我的标头与安全无关,我是否可以尝试使用类似于Spring Security的东西?
谢谢。
英文:
I have a series of Rest API Controllers in my Spring boot application with Request Mappings that match certain URLs.
I need to change my implementation to always make sure that a specific custom header is in place for all requests. If header is not there I want to fail the request. If it is I want to forward to the appropriate controller which would be the same as my current implementation.
Is there a way to do this in Spring Boot without modifying my existing controllers at all? Could I try to use something like Spring Security, even though my header is not related to security at all?
Thank you.
答案1
得分: 2
Web MVC定义了一个名为“HandlerInterceptor”的抽象,以及其空操作实现HandlerInterceptorAdapter
。
因此,您可以注册类似以下内容的bean:
@Component
public class RequestProcessingTimeInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// 检查标头,从请求中提取它们,或进行其他操作
return true; // 如果希望继续传递到控制器
return false; // 否则 :)
}
}
这将指示Spring MVC在流程到达控制器之前调用该方法。
英文:
Web MVC defines an abstraction called "HandlerInterceptor" and its no-op implementation HandlerInterceptorAdapter
So you can register the bean that looks like this:
@Component
public class RequestProcessingTimeInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// check the headers, extract them from request, whatever
return true; // if you want to proceed to controller
return false;// otherwise :)
}
}
This will instruct spring mvc to call the method before the flow gets to the controller.
答案2
得分: 1
你可以将一个 Filter
配置为一个 @Service
。
@Service
@NoArgsConstructor @Log4j2
public class FilterImpl implements Filter {
@Override
public void init(FilterConfig config) throws ServletException { }
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request.getHeader("required-header-name") != null) {
chain.doFilter(request, response);
} else {
log.info("Rejected {}", request);
}
}
@Override
public void destroy() {
}
}
英文:
You can configure a Filter
as a @Service
.
@Service
@NoArgsConstructor @Log4j2
public class FilterImpl implements Filter {
@Override
public void init(FilterConfig config) throws ServletException { }
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request.getHeader("required-header-name") != null) {
chain.doFilter(request, response);
} else {
log.info("Rejected {}", request);
}
}
@Override
public void destroy() {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论