I have implemented OncePerRequestFilter in my springboot application, but after adding the filter swagger is not opening it is giving 401 error (which is obvious). my problem is how to bypass swagger in my filter.
I have added only this dependency in my pom file
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${swagger.springdoc.openapi}</version>
</dependency>
Here is the code for my filter file
@Component
public class RequestResponseFilter extends OncePerRequestFilter{
@Inject
private AuthService authService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if(request.getServletPath().contains(ApiResource.PING)) {
response.setStatus(HttpServletResponse.SC_OK);
return;
}else if(request.getServletPath().contains("swagger-ui.html")){
//return;
}else if(request.getServletPath().contains("swagger-ui/index.html")){
//return;
}
else {
// Get the HTTP Authorization header from the request
final String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
final boolean authStatus = authService.authenticate(authorizationHeader);
if (!authStatus) {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
"REST signature failed validation.");
return;
}
}
filterChain.doFilter(request, response);
}
}
I tried to add my swagger url as you can see in else condition (as a workaround) but that is also not working..
Any help would be appreciated.
TIA