swagger2 can not get the page Solution

No, I can learn; behind, I can catch up; fall, I can stand up; I'll do!

 

Problem Scenario: Configuring filters in springboot project also integrates swagger2, found swagger2 address can not be accessed, no pop-up pages, refer to the following:

The filter code is as follows:

public class JwtAuthenticationFilter extends OncePerRequestFilter {

    private static final PathMatcher pathMatcher = new AntPathMatcher();

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        if (StringUtils.contains(request.getServletPath(), "swagger")
                || StringUtils.contains(request.getServletPath(), "webjars")
                || StringUtils.contains(request.getServletPath(), "v2")) {
            IF (to request.getServerName () the equals ( "localhost." )) { 
                filterChain.doFilter (request, Response); 
            } 
        } the else {
             IF (isExceededUrl (Request)) {
                 // log registration request direct release 
                filterChain.doFilter (request, Response); 
            } the else {
                 // Get Authorization request header 
                String tokenHeader = request.getHeader (TokenConstants.TOKEN_HEADER); 

                // token does not exist is returned 
                IF (StringUtils.isBlank (tokenHeader) ||!  tokenHeader.startsWith (TokenConstants .TOKEN_PREFIX)) {
                    the ServletOutputStream OUT = response.getOutputStream();
                    out.print(ResponseConstants.TOKEN_IS_INVALID);
                    out.close();
                    return;
                }

                // token过期返回
                if (JwtUtil.isExpiration(JwtUtil.getTokenByHeader(tokenHeader))) {
                    ServletOutputStream out = response.getOutputStream();
                    out.print(ResponseConstants.TOKEN_IS_INVALID);
                    out.close();
                    return;
                }

                String uid = "";
                try {
                    uid = JwtUtil.getUidByTokenHeader(request.getHeader(TokenConstants.TOKEN_HEADER));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                request.setAttribute("uid", uid);

                HttpServletRequest req = (HttpServletRequest) request;
                MutableHttpServletRequest mutableRequest = new MutableHttpServletRequest(req);
                mutableRequest.putHeader("uid", uid);
                filterChain.doFilter(request, response);
            }
        }
    } 

    / ** 
     * @Description:. Otherwise we only login / login jwt need to do to match the api url address check jwt / api beginning of reference 
     *                      https://www.cnblogs.com/zhangxiaoguang/p/5855113. HTML 
     * @param Request 
     * @return boolean
      * / 
    Private  boolean isProtectedUrl (the HttpServletRequest Request) {
         return pathMatcher.match ( "/ **" , request.getServletPath ()); 
    } 

    / ** 
     * @Description: Login Register do not do school test 
     * @param Request 
     * @return boolean
      * / 
    Private  boolean isExceededUrl(HttpServletRequest request) {
        return pathMatcher.match("/user/login", request.getServletPath())
                || pathMatcher.match("/user/register", request.getServletPath());
    }

}

 

To solve the problem can not be accessed, you need to add a static resource mapping swagger2 of:

 

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }
}

 

 

The above content represents only personal point of view, do not like do not spray ...

 

Guess you like

Origin www.cnblogs.com/newbest/p/11494086.html