亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • Springboot文件上傳功能的實(shí)現(xiàn)

    Springboot文件上傳功能的實(shí)現(xiàn)

    這篇文章主要介紹了SpringBoot文件上傳功能的實(shí)現(xiàn),文中通過代碼示例介紹的非常詳細(xì),具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們可以參考閱讀
    2023-04-04
  • Spring IOC和aop的原理及實(shí)例詳解

    Spring IOC和aop的原理及實(shí)例詳解

    這篇文章主要介紹了Spring IOC和aop的原理及實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • 詳細(xì)聊一聊java語言中的package和import機(jī)制

    詳細(xì)聊一聊java語言中的package和import機(jī)制

    這篇文章主要給大家介紹了關(guān)于java語言中package和import機(jī)制的相關(guān)資料,Java中的package是指將相關(guān)的類組織在一起的一種機(jī)制,它可以用來避免命名沖突,也可以方便地管理和維護(hù)代碼,需要的朋友可以參考下
    2024-01-01
  • 用java實(shí)現(xiàn)猜數(shù)字游戲

    用java實(shí)現(xiàn)猜數(shù)字游戲

    這篇文章主要為大家詳細(xì)介紹了用java實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 基于html5+java實(shí)現(xiàn)大文件上傳實(shí)例代碼

    基于html5+java實(shí)現(xiàn)大文件上傳實(shí)例代碼

    本文通過一段實(shí)例代碼給大家介紹基于html5+java實(shí)現(xiàn)大文件上傳,涉及到html5 java 文件上傳相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • SpringBoot讀取Resource目錄下文件的四種方式總結(jié)

    SpringBoot讀取Resource目錄下文件的四種方式總結(jié)

    在Spring?Boot項(xiàng)目中,經(jīng)常需要獲取resources目錄下的文件,這些文件可以包括配置文件、模板文件、靜態(tài)資源等,本文將介紹四種常用的方法來獲取resources目錄下的文件,需要的朋友可以參考下
    2023-08-08
  • java后臺(tái)處理前端傳的json串方法

    java后臺(tái)處理前端傳的json串方法

    今天小編就為大家分享一篇java后臺(tái)處理前端傳的json串方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Mybatis使用MySQL模糊查詢時(shí)輸入中文檢索不到結(jié)果怎么辦

    Mybatis使用MySQL模糊查詢時(shí)輸入中文檢索不到結(jié)果怎么辦

    這篇文章主要介紹了Mybatis使用MySQL模糊查詢時(shí)輸入中文檢索不到結(jié)果的解決辦法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • SpringBoot+BootStrap多文件上傳到本地實(shí)例

    SpringBoot+BootStrap多文件上傳到本地實(shí)例

    這篇文章主要介紹了SpringBoot+BootStrap多文件上傳到本地實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(15)

    Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(15)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-07-07

最新評(píng)論