Swagger-UI与Spring Cloud整合与安全设置

swagger ui是一个API在线文档生成和测试的利器,在某种程度上可以媲美常用的postman,网上有很多它的整合教程,但是没有考虑到安全问题,下面是它的整合流程。

1、maven依赖:

    <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>

2、启动主类添加注解

@EnableWebMvc

3、创建Swagger2类放在与启动主类同一目录下

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicates;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * 
 * @Title: Swagger设置类
 * @Package com.lovnx.charge 
 * @author yezhiyuan   
 * @date 2017年5月10日 上午9:45:55 
 * @version V1.0
 */
@Configuration  
@ComponentScan(basePackages = { "com.lovnx.*.controller.*" })//配置controller路径
@EnableSwagger2
@SuppressWarnings({"unchecked","deprecation"})
public class Swagger2 {  

    @Bean  
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)  
                .apiInfo(apiInfo())  
                .select()
                .paths(Predicates.or(
                //这里添加你需要展示的接口
                        PathSelectors.ant("/account/**"),
                        PathSelectors.ant("/xxx/**"),
                        PathSelectors.ant("/qqq/**"),
                        PathSelectors.ant("/eee/**")
                                    )
                        )
                .build();  
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()  
                .title("API平台名字")
                .description("说明RESTful APIs")
                .contact("xxx@qq.com") 
                .version("1.0")
                .build();  
    }
}  

4、Swagger配置路径重定向(不加这个类会报错)

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * 
 * @Title: Swagger配置路径重定向
 * @Package com.config 
 * @author yezhiyuan   
 * @date 2017年5月10日 上午9:45:16 
 * @version V1.0
 */
@Configuration
public class SwaggerWebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

5、controller添加注解

类上添加:@Api(description="这个controller是干嘛的")
方法上添加:@ApiOperation(value="这个方法是干嘛的", notes="详细注释")

6、配置完成,访问API页面

http://localhost:服务端口/swagger-ui.html

7、整合Spring Security实现访问API页面输入用户名密码

maven依赖:
<dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-security</artifactId>  
</dependency>

配置文件添加:
security.basic.path=/swagger-ui.html
security.basic.enabled=true
security.user.name=lovnx
security.user.password=123456
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZhiYuanYe

您的鼓励将是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值