'swagger is not opening after adding OncePerRequestFilter is spring boot application
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
Solution 1:[1]
In order to ignore specific URL we need to @Override shouldNotFilter method of OncePerRequestFilter.
I updated my configuration with below code.
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
@Component
public class AuthenticationFilter extends OncePerRequestFilter {
private List<String> excludeUrlPatterns = new ArrayList<String>(Arrays.asList("/swagger-ui.html",
"/swagger-uui.html", "/webjars/springfox-swagger-ui/springfox.css",
"/webjars/springfox-swagger-ui/swagger-ui-bundle.js", "/webjars/springfox-swagger-ui/swagger-ui.css",
"/webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js",
"/webjars/springfox-swagger-ui/springfox.js", "/swagger-resources/configuration/ui",
"/webjars/springfox-swagger-ui/favicon-32x32.png", "/swagger-resources/configuration/security",
"/swagger-resources", "/v2/api-docs",
"/webjars/springfox-swagger-ui/fonts/titillium-web-v6-latin-700.woff2",
"/webjars/springfox-swagger-ui/fonts/open-sans-v15-latin-regular.woff2",
"/webjars/springfox-swagger-ui/fonts/open-sans-v15-latin-700.woff2",
"/webjars/springfox-swagger-ui/favicon-16x16.png"));
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
System.out.println("Once per request filter is executed.");
}
@Override
protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException {
String path = request.getRequestURI();
if (excludeUrlPatterns.contains(path)) {
return true;
} else {
return false;
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | I AM GROOT |
