在Linux系统中安全使用Swagger主要涉及以下几个方面:
sudo apt update sudo apt install openjdk-11-jdk sudo apt install maven wget https://repo1.maven.org/maven2/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.html unzip springfox-swagger-ui-2.9.2.html sudo mv springfox-swagger-ui-2.9.2 /var/www/html/swagger-ui /var/www/html/swagger-ui/index.html 文件,找到以下行并进行修改:script window.onload function () { // Begin Swagger UI call region const ui = SwaggerUIBundle({ url: "http://petstore.swagger.io/v2/api-docs", dom_id: '#swagger-ui', deepLinking: true, presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ], plugins: [ SwaggerUIBundle.plugins.DownloadUrl ], layout: "StandaloneLayout" }); // End Swagger UI call region window.ui = ui; }; /etc/apache2/sites-available/swagger.conf:<VirtualHost *:80> ServerName localhost DocumentRoot /var/www/html/swagger-ui Directory /var/www/html/swagger-ui Options Indexes FollowSymLinks AllowOverride All Require all granted </VirtualHost> 然后启用该虚拟主机:
sudo a2ensite swagger.conf sudo systemctl reload apache2 如果你使用的是Nginx,可以创建一个服务器块配置文件,例如 /etc/nginx/sites-available/swagger:
server { listen 80; server_name localhost; root /var/www/html; index index.html index.htm; location / { try_files $uri $uri/ /index.html; } } 然后启用该服务器块:
sudo ln -s /etc/nginx/sites-available/swagger /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx 现在,你应该能够通过浏览器访问 http://localhost/swagger-ui 来查看和使用Swagger UI。