• swagger2不能获取页面解决办法


    不会,我可以学;落后,我可以追赶;跌倒,我可以站起来;我一定行!

    问题场景: 在springboot项目中配置过滤器, 同时也集成了swagger2, 发现swagger2的地址不能访问, 没有页面弹出,参考以下:

    过滤器代码如下:

    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 (request.getServerName().equals("localhost")) {
                    filterChain.doFilter(request, response);
                }
            } else {
                if (isExceededUrl(request)) {
                    // 登录注册请求直接放行
                    filterChain.doFilter(request, response);
                } else {
                    // 获取请求头中的Authorization信息
                    String tokenHeader = request.getHeader(TokenConstants.TOKEN_HEADER);
    
                    // token不存在则返回
                    if (StringUtils.isBlank(tokenHeader) || !tokenHeader.startsWith(TokenConstants.TOKEN_PREFIX)) {
                        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:我们只对地址 /api 开头的api检查jwt. 不然的话登录/login也需要jwt 做url匹配,参考
         *                     https://www.cnblogs.com/zhangxiaoguang/p/5855113.html
         * @param request
         * @return boolean
         */
        private boolean isProtectedUrl(HttpServletRequest request) {
            return pathMatcher.match("/**", request.getServletPath());
        }
    
        /**
         * @Description:登录注册不做校验
         * @param request
         * @return boolean
         */
        private boolean isExceededUrl(HttpServletRequest request) {
            return pathMatcher.match("/user/login", request.getServletPath())
                    || pathMatcher.match("/user/register", request.getServletPath());
        }
    
    }

    解决不能访问的问题, 需要添加swagger2的静态资源映射:

    @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);
        }
    }

    以上内容仅代表个人观点,不喜勿喷...

     

  • 相关阅读:
    Oracle数据库编程
    使用JDBC处理MySQL大文本和大数据
    phpcms之带图片的登录信息(带cookie版)(由于cookie和PHPCMS的原因,这个暂时无法使用,看新的)
    phpcms之调用导航栏
    phpcms之修改默认显示文字
    phpcms之创建自己的路径
    dedecms 动态tab写法
    关于dedecms的操作
    12/23
    12/21
  • 原文地址:https://www.cnblogs.com/newbest/p/11494086.html
Copyright © 2020-2023  润新知