我在集成knife4j的时候,接口文档页面报错

Blade 未结 1 2157
jiashaCocoa
jiashaCocoa 剑者 2021-09-08 16:38

一、该问题的重现步骤是什么?

  1. 按照正常的步骤集成knife4j,结果页面报错,但是后端控制台不报错

    报错信息如下:

    image.png不明白这是什么地方的错误



报错信息,贴在这里  --------------->>>>>>We're sorry but knife4j-vue doesn't work properly without JavaScript enabled. Please enable it to continue.

1条回答
  • 2021-09-08 17:59

    发现项目中有设置过滤器,只要把过滤器放开就可以了

    package com.xxxx.xxx.xxxxx.filter;
    
    import com.cetc.epms.inner.base.pojo.vo.LoginContextStore;
    import com.cetc.epms.inner.base.pojo.vo.inner.InnerLoginContext;
    import com.cetc.epms.inner.redis.service.TokenService;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import org.springframework.boot.configurationprocessor.json.JSONObject;
    import org.springframework.stereotype.Component;
    import org.springframework.util.StringUtils;
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.List;
    
    @Component
    @WebFilter(urlPatterns="/*")
    public class TokenValidationFilter implements Filter {
    
        @Autowired
        private CasClientConfigurationProperties configProps;
    
        @Autowired
        private TokenService tokenService;
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) res;
    
            String authorization = request.getHeader("Authorization");
            if (request.getMethod().equals("OPTIONS")) {
                response.setStatus(HttpServletResponse.SC_OK);
            }
            String requestURI = request.getRequestURI();
    
            boolean isWhite = isWhiteList(requestURI);
    
            if(!isWhite) {
                boolean exist = tokenService.exist(authorization);
                if (exist) {
                    InnerLoginContext loginContext = tokenService.getUserByToken(authorization);
                    LoginContextStore.setInnerLoginContext(loginContext);
    
                    chain.doFilter(req, res);
                } else {
                    responseError(response, "ticket无效或过期", 401);
                    return;
                }
            }else {
                chain.doFilter(req, res);
            }
        }
    
        @Override
        public void init(FilterConfig filterConfig) {
        }
    
        @Override
        public void destroy() {
        }
    
    
        private  boolean isWhiteList(String requestURI){
            boolean retVal =false ;
            List<String> whiteList = configProps.getWhiteList();
    
            if(!StringUtils.isEmpty(whiteList)){
                for(String configUri:whiteList){
                    if(requestURI.contains(configUri)){
                        retVal = true;
                    }
                }
            }
            return true;
        }
    
        public void  responseError(HttpServletResponse httpResponse,String msg,Integer code) throws IOException {
            ServletOutputStream outputStream = httpResponse.getOutputStream();
            httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
            httpResponse.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
            httpResponse.setHeader("Access-Control-Max-Age", "5000");
            httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With, Authorization, token");
    
            writeResult(outputStream, msg, code);
        }
    
        private static void writeResult(ServletOutputStream outputStream,String msg,int code){
            JSONObject json = new JSONObject();
            try {
                json.put("msg",msg);
                json.put("code",code);
                outputStream.write(json.toString().getBytes());
                outputStream.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                //writer.close();
            }
        }
    
    }


    1 讨论(0)
提交回复