1 访问swagger首页 http://localhost:8080/swagger-ui.html出错
错误信息如下:
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
解决办法
在启动类上加上注解@EnableSwagger2 如果还不行,那就是因为你系统有shiro鉴权导致,解决方案:
1.放开对该页面的鉴权
先把shiro配置类的 @Configuration 给注释掉,再打开 http://localhost:8080/swagger-ui.html 右键 选择检查 或 按F12键,查看该页面的网络请求,在network这项里,通过查看发现访问该页面,需要访问哪些资源,把相应的文件及文件夹在shiro中放开权限,如下:
filterChainDefinitionMap.put("/swagger-ui.html", "anon");
filterChainDefinitionMap.put("/swagger-resources", "anon");
filterChainDefinitionMap.put("/swagger-resources/**", "anon");
filterChainDefinitionMap.put("/webjars", "anon");
filterChainDefinitionMap.put("/webjars/**", "anon");
filterChainDefinitionMap.put("/v2", "anon");
filterChainDefinitionMap.put("/v2/**", "anon");
加上这些配置后,再把 shiro配置类的 @Configuration 给注释去掉,再打开 http://localhost:8080/swagger-ui.html 访问正常了
2.去掉shiro的启动注解,不启动权限验证,也可以解决