最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Swagger2配置Security授權(quán)認(rèn)證全過程

 更新時(shí)間:2023年03月29日 15:16:39   作者:一蓑煙雨?任平生  
這篇文章主要介紹了Swagger2配置Security授權(quán)認(rèn)證全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Swagger2配置Security授權(quán)認(rèn)證

package com.ytm.yeb.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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 java.util.ArrayList;
import java.util.List;

/**
 * @author TongMing Yang
 * @since 2021/1/12
 */

@EnableSwagger2
@Configuration
public class Swagger2Config {



    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                // 是否開啟
                .enable(true).select()
                // 掃描的路徑包
                .apis(RequestHandlerSelectors.basePackage("com.ytm.yeb.controller"))
                // 指定路徑處理PathSelectors.any()代表所有的路徑
                .paths(PathSelectors.any()).build()
                .pathMapping("/")
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> apiKeyList= new ArrayList();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> securityContexts=new ArrayList<>();
        securityContexts.add(
                SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.regex("^(?!auth).*$"))
                        .build());
        return securityContexts;
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> securityReferences=new ArrayList<>();
        securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
        return securityReferences;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("云E辦接口文檔")
                .description("云E辦接口文檔")
                .contact(new Contact("yeb", "http://localhost:8081/doc.html", "ytm5021@163.com"))
                .version("1.0")
                .build();
    }
}

Swagger2 3.0版本相關(guān)配置和坑

Swagger2 介紹

**網(wǎng)上介紹:**Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。

總體目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。文件的方法、參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許 API 來始終保持同步。Swagger 讓部署管理和使用功能強(qiáng)大的 API 從未如此簡(jiǎn)單。

整合使用完整過程

1.引入依賴

Swagger2 3.0由于新增了Starter 因此可以直接使用starter方式
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
在沒有starter前 一般都是引入以下依賴  兩個(gè)依賴的版本最好一致,避免出現(xiàn)沖突
<!--        &lt;!&ndash; https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>io.springfox</groupId>-->
<!--            <artifactId>springfox-swagger2</artifactId>-->
<!--            <version>3.0.0</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>io.springfox</groupId>-->
<!--            <artifactId>springfox-swagger-ui</artifactId>-->
<!--            <version>3.0.0</version>-->
<!--        </dependency>-->

tips:推薦使用idea的插件,方便查看依賴沖突:

2.攔截配置

package com.deer.primer3.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author lujy
 * @version 1.0
 * @date 2021/2/2 12:36
 */
@EnableWebMvc
@Configuration
@Slf4j
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/swagger-ui/")
                .setViewName("forward:/swagger-ui/index.html");
    }

    //這個(gè)是跨域配置 不需要的可以不配
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.info("跨域配置開啟");
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }
}

3.可選配置

/**
 * @author lujy
 * @version 1.0
 * @date 2021/2/7 10:04
 */
@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(api())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo api() {
        return new ApiInfoBuilder()
                .title("Swagger3接口文檔")
                .description("文檔描述")
                .contact(new Contact("lujy", "#", "18506239610@163.com"))
                .version("1.0")
                .build();
    }


}

new Docket(DocumentationType documentationType); 有參構(gòu)造 參數(shù) 對(duì)應(yīng)為 swagger版本

.apiInfo(api()) return —>>>Docket ApiInfoBuilder()

.select() —>>> return ApiSelectorBuilder

ApiSelectorBuilder .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

尋找Controller層請(qǐng)求處理的方法中有ApiOperation的注解(個(gè)人理解)

SpringSecurity 攔截放行

坑:

swagger 似乎 無法進(jìn)行文件上傳 測(cè)試多次 后臺(tái)都報(bào) 空指針,用postman測(cè)試則沒有影響

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java實(shí)現(xiàn)的xml格式化實(shí)現(xiàn)代碼

    java實(shí)現(xiàn)的xml格式化實(shí)現(xiàn)代碼

    這篇文章主要介紹了java實(shí)現(xiàn)的xml格式化實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2016-11-11
  • 詳解java中import的作用

    詳解java中import的作用

    這篇文章主要介紹了java中import作用,import與package機(jī)制相關(guān),這里先從package入手,再講述import以及static import的作用。
    2021-04-04
  • 6種SpringBoot解決跨域請(qǐng)求的方法整理

    6種SpringBoot解決跨域請(qǐng)求的方法整理

    跨域資源共享是一種標(biāo)準(zhǔn)機(jī)制,允許服務(wù)器聲明哪些源可以訪問其資源,在SpringBoot應(yīng)用中,有多種方式可以解決跨域問題,本文主要介紹了6種常見的解決方案,大家可以根據(jù)需求自行選擇
    2025-04-04
  • Java使用設(shè)計(jì)模式中的工廠方法模式實(shí)例解析

    Java使用設(shè)計(jì)模式中的工廠方法模式實(shí)例解析

    當(dāng)系統(tǒng)準(zhǔn)備為用戶提供某個(gè)類的子類的實(shí)例,又不想讓用戶代碼和該子類形成耦合時(shí),就可以使用工廠方法模式來設(shè)計(jì)系統(tǒng).工廠方法模式的關(guān)鍵是在一個(gè)接口或抽象類中定義一個(gè)抽象方法,下面我們會(huì)具體介紹Java使用設(shè)計(jì)模式中的工廠方法模式實(shí)例解析.
    2016-05-05
  • Java synchronized底層的實(shí)現(xiàn)原理

    Java synchronized底層的實(shí)現(xiàn)原理

    這篇文章主要介紹了Java synchronized底層的實(shí)現(xiàn)原理,文章基于Java來介紹 synchronized 是如何運(yùn)行的,內(nèi)容詳細(xì)具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-05-05
  • 詳解SpringBoot AOP 攔截器(Aspect注解方式)

    詳解SpringBoot AOP 攔截器(Aspect注解方式)

    這篇文章主要介紹了詳解SpringBoot AOP 攔截器 Aspect,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java+MySQL實(shí)現(xiàn)圖書管理系統(tǒng)(完整代碼)

    Java+MySQL實(shí)現(xiàn)圖書管理系統(tǒng)(完整代碼)

    這篇文章主要介紹了Java+MySQL實(shí)現(xiàn)圖書管理系統(tǒng)(完整代碼),本文給大家介紹的非常想詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • springboot實(shí)現(xiàn)的https單向認(rèn)證和雙向認(rèn)證(java生成證書)

    springboot實(shí)現(xiàn)的https單向認(rèn)證和雙向認(rèn)證(java生成證書)

    springboot https單向認(rèn)證和雙向認(rèn)證,本文主要介紹了springboot實(shí)現(xiàn)的https單向認(rèn)證和雙向認(rèn)證,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-04-04
  • 詳解Java中的時(shí)間處理與時(shí)間標(biāo)準(zhǔn)

    詳解Java中的時(shí)間處理與時(shí)間標(biāo)準(zhǔn)

    這篇文章主要為大家詳細(xì)介紹了三個(gè)時(shí)間標(biāo)準(zhǔn)GMT,CST,UTC的規(guī)定,以及Java進(jìn)行時(shí)間處理的相關(guān)知識(shí),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-09-09
  • 關(guān)于Spring Cloud 本地屬性覆蓋的問題

    關(guān)于Spring Cloud 本地屬性覆蓋的問題

    這篇文章主要介紹了關(guān)于Spring Cloud 本地屬性覆蓋的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論

东城区| 桐柏县| 临夏县| 视频| 理塘县| 子洲县| 博爱县| 五家渠市| 邵东县| 手机| 晋中市| 库伦旗| 上杭县| 富锦市| 突泉县| 温宿县| 临安市| 西和县| 荣成市| 青浦区| 江口县| 屏东县| 洛南县| 莱芜市| 东至县| 克拉玛依市| 巴中市| 肃宁县| 勐海县| 天津市| 麻城市| 睢宁县| 内乡县| 黄陵县| 冀州市| 安吉县| 维西| 新民市| 沂源县| 天气| 右玉县|