文章目录
  1. 1. pring-security oauth2 集成 swagger 2.9.2

pring-security oauth2 集成 swagger 2.9.2

pom.xml:

<?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\>com.xxx.xxx\</groupId\>
<artifactId\>bzy-auth\</artifactId\>
<version\>1.0\</version\>
<packaging\>jar\</packaging\>

<name\>xxx-auth\</name\>
<description\>xxx-auth project for sign,auth\</description\>

<parent\>
<groupId\>com.xxxl.xxx\</groupId\>
<artifactId\>xxx-parent\</artifactId\>
<version\>1.0.0-SNAPSHOT\</version\>
</parent\>

<dependencies\>

<dependency\>
<groupId\>com.hhotel.bzy\</groupId\>
<artifactId\>bzy-common-swagger\</artifactId\>
<version\>${common.swagger.version}\</version\>
<type\>jar\</type\>
</dependency\>

<dependency\>
<groupId\>org.springframework.cloud\</groupId\>
<artifactId\>spring-cloud-starter-netflix-eureka-client\</artifactId\>
</dependency\>


<dependency\>
<groupId\>org.springframework.cloud\</groupId\>
<artifactId\>spring-cloud-starter-security\</artifactId\>
</dependency\>
<dependency\>
<groupId\>org.springframework.security.oauth.boot\</groupId\>
<artifactId\>spring-security-oauth2-autoconfigure\</artifactId\>
<version\>2.1.1.RELEASE\</version\>
</dependency\>

</dependencies\>




</project\>

SwaggerConfig:

package com.xxx.xxx.auth.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.AuthorizationCodeGrantBuilder;
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.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.Arrays;

/\*\*  \* Created by sam on 2019/3/11.
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerAuthConfig {

    @Value("${bzy.swagger-enable}")
    private Boolean swaggerEnable;

    @Value("${bzy.swagger-auth-url}")
    private String authServerUrl;

    @Value("${bzy.security-auth2.clientId}")
    private String clientId;

    @Value("${bzy.security-auth2.clientId}")
    private String secretKey;

    public SwaggerAuthConfig() {

    }
    @Bean
    public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
    .enable(this.swaggerEnable.booleanValue())
    .select()
    .apis(RequestHandlerSelectors.any())
    .paths(PathSelectors.any())
    .build()
    .securitySchemes(Arrays.asList(securityScheme()))
    .securityContexts(Arrays.asList(securityContext()));
    }

    @Bean
    public SecurityConfiguration security() {
    return SecurityConfigurationBuilder.builder()
    .clientId(clientId)
    .clientSecret(secretKey)
    .scopeSeparator(" ")
    .useBasicAuthenticationWithAccessCodeGrant(true)
    .build();
    }


    private SecurityScheme securityScheme() {
    GrantType grantType = new AuthorizationCodeGrantBuilder()
    .tokenEndpoint(new TokenEndpoint(authServerUrl + "/token", "oauthtoken"))
    .tokenRequestEndpoint(
    new TokenRequestEndpoint(authServerUrl + "/authorize", clientId, secretKey))
    .build();

    SecurityScheme oauth = new OAuthBuilder().name("spring_oauth")
    .grantTypes(Arrays.asList(grantType))
    .scopes(Arrays.asList(scopes()))
    .build();
    return oauth;
    }

    private AuthorizationScope[]() scopes() {
    AuthorizationScope[]() scopes = {
    new AuthorizationScope("read", "for read operations"),
    new AuthorizationScope("write", "for write operations"),
    new AuthorizationScope("all", "for all operation API") };
    return scopes;
    }

    private SecurityContext securityContext() {
    return SecurityContext.builder()
    .securityReferences(
    Arrays.asList(new SecurityReference("spring_oauth", scopes())))
    .forPaths(PathSelectors.any())
    .build();
    }
    }

[参考文章][1]

[优秀博客][2]

[优秀博客][3]

[1]: https://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
[2]: https://www.xncoding.com/2017/07/08/spring/sb-swagger2.html
[3]: https://www.cnblogs.com/bndong/p/10037197.html

文章目录
  1. 1. pring-security oauth2 集成 swagger 2.9.2