解决策略:通过网关分发时,对swagger需要的各种资源放行即可
@Component
public class ManagerFilter extends ZuulFilter {
@Override
public String filterType ( ) {
return "pre" ;
}
@Override
public int filterOrder ( ) {
return 0 ;
}
@Override
public boolean shouldFilter ( ) {
return true ;
}
@Override
public Object run ( ) throws ZuulException {
System. out. println ( "经过后台过滤器了" ) ;
RequestContext requestContext = RequestContext. getCurrentContext ( ) ;
HttpServletRequest request = requestContext. getRequest ( ) ;
if ( request. getMethod ( ) . equals ( "OPTIONS" ) ) {
return null;
}
String url = request. getRequestURL ( ) . toString ( ) ;
if ( url. indexOf ( "swagger" ) > 0 || url. indexOf ( "webjars" ) > 0 || url. indexOf ( "v2" ) > 0 ) {
System. out. println ( "swaggerAPI" + url) ;
return null;
}
requestContext. setResponseStatusCode ( 403 ) ;
requestContext. setResponseBody ( "兄弟,你权限不足" ) ;
requestContext. getResponse ( ) . setContentType ( "text/html;charset=utf-8" ) ;
return null;
}
}