使用Nginx+SSL 访问地址Swagger 2.0

水滴石穿,点滴记忆。

location /dataAPI {
            root   html;
            index  index.html index.htm;
            proxy_pass http://127.0.0.1:8080/api;
            proxy_set_header Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;

        }
        location /api {
            return 403;
        }



            proxy_set_header Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;

不然接口用例中的地址为错误地址

链接: Swagger 2.0 使用Nginx+SSL 访问地址.

上面的代码是很久之前使用Swagger 遇到的,当时也没多进行整理,潦草的记录了一下,现在完善一下当时的事。

  • 当时我在一个小项目中,使用了Swagger 2.0这个版本,这个项目很小帮一个朋友的小忙,然后为了这次举例子,我把里面的一些敏感信息改掉了,之前想着打码之类的,但是会影响直接的可读性,就把一些类名、方法名改掉了,见谅。
  • 说一下版本信息:

    • Java:JDK1.8;
    • SpringBoot: 2.0.5.RELEASE;
    • Swagger2: 2.9.2;
    • maven:POM如下
    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>org.example</groupId>
      <artifactId>bigbird</artifactId>
      <version>1.0-SNAPSHOT</version>
    
      <name>bigbird</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <spring.version>2.0.5.RELEASE</spring.version>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <version>${spring.version}</version>
          <scope>test</scope>
          <exclusions>
            <exclusion>
              <artifactId>spring-boot-starter-logging</artifactId>
              <groupId>org.springframework.boot</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-log4j2</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
          <version>${spring.version}</version>
          <exclusions>
            <exclusion>
              <artifactId>spring-boot-starter-logging</artifactId>
              <groupId>org.springframework.boot</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger2</artifactId>
          <version>2.9.2</version>
        </dependency>
        <dependency>
          <groupId>io.springfox</groupId>
          <artifactId>springfox-swagger-ui</artifactId>
          <version>2.9.2</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>bigbird</finalName>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
              <encoding>UTF-8</encoding>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20.1</version>
            <configuration>
              <skip>true</skip>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>${spring.version}</version>
            <executions>
              <execution>
                <goals>
                  <goal>repackage</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <mainClass>org.example.App</mainClass>
              <fork>true</fork>
              <addResources>true</addResources>
              <outputDirectory>${project.build.directory}/jar</outputDirectory>
              <executable>true</executable>
            </configuration>
          </plugin>
          <!-- 打JAR包 -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
              <!-- 不打包资源文件(配置文件和依赖包分开) -->
              <excludes>
                <exclude>static/**</exclude>
                <exclude>templates/**</exclude>
                <exclude>*.yml</exclude>
                <exclude>config.properties</exclude>
                <exclude>application.properties</exclude>
                <exclude>log4j2-spring.xml</exclude>
                <exclude>data/**</exclude>
                <!--<exclude>*.**</exclude>
                <exclude>*/*.xml</exclude>-->
              </excludes>
              <archive>
                <manifest>
                  <addClasspath>true</addClasspath>
                  <!-- MANIFEST.MF 中 Class-Path 加入前缀 -->
                  <classpathPrefix>lib/</classpathPrefix>
                  <!-- jar包不包含唯一版本标识 -->
                  <useUniqueVersions>false</useUniqueVersions>
                  <!--指定入口类 -->
                  <mainClass>org.example.App</mainClass>
                </manifest>
                <manifestEntries>
                  <!--MANIFEST.MF 中 Class-Path 加入资源文件目录 -->
                  <Class-Path>./config/</Class-Path>
                </manifestEntries>
              </archive>
              <outputDirectory>${project.build.directory}</outputDirectory>
            </configuration>
          </plugin>
          <!-- 该插件的作用是用于复制依赖的jar包到指定的文件夹里 -->
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
    
          <!-- 该插件的作用是用于复制指定的文件 -->
          <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <executions>
              <execution> <!-- 复制配置文件 -->
                <id>copy-resources</id>
                <phase>package</phase>
                <goals>
                  <goal>copy-resources</goal>
                </goals>
                <configuration>
                  <resources>
                    <resource>
                      <directory>src/main/resources</directory>
                      <includes>
                        <include>*.yml</include>
                        <include>static/**</include>
                        <include>templates/**</include>
                        <include>config.properties</include>
                        <include>application.properties</include>
                        <include>log4j2-spring.xml</include>
                        <include>data/**</include>
                      </includes>
                    </resource>
                  </resources>
                  <outputDirectory>${project.build.directory}/config</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </project>
    
  • Swagger2正式开始

    • SpringBoot 启动类增加Swagger2的注解,这里很简单了,SpringBoot现在太方便了,增加什么功能组件都简单。
    @Configuration
    @EnableSwagger2
    @SpringBootApplication
    public class App
    {
        private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
        public static void main( String[] args )
        {
            SpringApplication app = new SpringApplication(App.class);
            app.run(args);
        }
        @Bean
        public Docket createRestApi(){
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                    .select().apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                    .paths(PathSelectors.any()).build();
        }
    
        private ApiInfo apiInfo(){
            return new ApiInfoBuilder().title("API 接口文档").description("API 接口文档").version("1.0").build();
        }
    }
    

    这里简单描述一下,第一件事就是在启动类上增加@EnableSwagger2,紧接着注册Bean对象,融入Spring的怀抱。我这里用的很简单,没有使用Swagger2的group功能,所以遇到的异常情况不多。

    • Swagger2 的接口类
    @Api
    @RestController
    public class ApiController {
        @ApiOperation(value = "可以写一下这个接口是做什么的", notes = "")
        @PostMapping(value="/api/TestRecord/add")
        public Object hello(@RequestBody @ApiParam(value = "json/application", required = true) @Validated(TestRecord.class) Testrecord testrecord, BindingResult results) throws ValidationException {
            int i = 0;
        }
    

    这里基本上没什么好详细说明的了,Swagger2的接口可以进行数据格式校验,我这里使用的是@Validated

    import javax.validation.ValidationException;
    

    好的,上面的配置过程很多博主哪里都写的很消息,我这里也在跟其他博主学习,怎么能表达的更加清晰。

    说一下标题中的Nginx和SSL的事。

    ​ 本身Swagger的接口在上面就已经可以使用了,碰巧如果程序是运行在HTTPS下呢,怎么办?加Nginx去代理一个HTTPS,对吧,好这里也没什么问题,那问题会出现在什么地方呢?

    https地址下的跳转,因为Swagger生成了页面的接口,在上面可以对接口进行测试,查看说明一些列操作,不可避免的需要跳转,那么问题就出现了,Https的地址,默认是带不过去的。这个事到这里跟Swagger其实也没有什么关系,主要是这种ssl特性导致的,需要我们在Nginx内把地址跳转的一些参数补齐。

    location /dataAPI {
                root   html;
                index  index.html index.htm;
                proxy_pass http://127.0.0.1:8080/api;
                proxy_set_header Host $host;
                proxy_set_header  X-Real-IP  $remote_addr;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Port $server_port;
            }
    

    增加的几句话主要是告诉Nginx的,你在代理的时候,要把真是IP和端口给我保持住,不要半路走丢了。

学习写博客的过程中,希望能帮到您。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值