Swagger UI 配置,以及整合spring security OAuth使用配置

3 篇文章 0 订阅
1 篇文章 0 订阅

需要的Maven 依赖;


        <!-- swagger ui 相关依赖-->
        <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>

 

效果图:

 废话不多说,直接上代码,详情请看注释:

package top.test.blogs.config;

import com.google.common.base.Predicates;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.OAuthBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import top.copying.blogs.properties.IgnoreUrlProperties;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author copying
 * @date 2020-08-29 10:45:36
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    /** 过滤的URL */
    @Resource
    private IgnoreUrlProperties ignoreUrlProperties;
    /**
     * apis扫描com路径下的api文档.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
     *  .apis(RequestHandlerSelectors.basePackage("top.copying.blogs"))
     * paths路径判断
     * @return Docket
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build()
                .securitySchemes(Collections.singletonList(securityScheme()))
                .securityContexts(Collections.singletonList(securityContext()));
    }

    /**
     * title标题
     * description 描述
     * termsOfServiceUrl(不可见)条款地址
     * version版本号
     * @return ApiInfo
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Copying Blogs Service")
                .description("个人博客功能接口")
                .termsOfServiceUrl("http://www.baidu.com")
                .version("1.0")
                .build();
    }

    /**
     * 2020-08-29 09:48:35 copying add
     * 添加在Swagger ui中实现登录功能
     * securitySchemes()方法配置里增加需要token的配置。
     * 配置完成后,swagger-ui.html里右上角会有一个Authorize的按钮,录入该token即能成功调用相关接口
     * //.grantTypes(Collections.singletonList(grantType))
     * @return SecurityScheme
     */
    private SecurityScheme securityScheme(){
        return new OAuthBuilder()
                .name("spring_oauth")
                .grantTypes(grantTypes())
                .scopes(Arrays.asList(scopes()))
                .build();

    }

    /**
     * 设置使用什么模式
     * 当前为密码模式
     * @return List<GrantType>
     */
    private List<GrantType> grantTypes(){
        List<GrantType> grantTypes = new ArrayList<>();
        GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("/blogs/oauth/token");
        grantTypes.add(grantType);
        return grantTypes;
    }

    /**
     * 设置 swagger2 认证的安全上下文
     * //.forPaths(PathSelectors.any()) 所有接口加锁
     * //.forPaths(Predicates.not(Predicates.in(ignoreUrlProperties.getUrls()))) 加锁排除指定的接口地址
     * ignoreUrlProperties为获取yml中配置的接口地址
     * @return SecurityContext
     */
    private SecurityContext securityContext() {
        return SecurityContext.builder()
                .securityReferences(Collections.singletonList(new SecurityReference("oauth2", scopes())))
                .forPaths(Predicates.not(Predicates.in(ignoreUrlProperties.getUrls())))
                .build();
    }


    /**
     * 设置认证的作用域scope
     * @return AuthorizationScope[]
     */
    private AuthorizationScope[] scopes() {
        return new AuthorizationScope[]{
                new AuthorizationScope("all", "All scope is trusted!")
        };
    }


}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值