28.OAuth2.0-Spring Cloud Securit

作者: LANSHENGYANG | 来源:发表于2020-03-31 17:49 被阅读0次

    环境搭建

    父工程

    • 创建maven工程作为父工程,依赖如下:
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.2.2.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.stan.security</groupId>
        <artifactId>distributed-security</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>distributed-security</name>
        <packaging>pom</packaging>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    
                <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>javax.servlet-api</artifactId>
                    <version>3.1.0</version>
                    <scope>provided</scope>
                </dependency>
    
                <dependency>
                    <groupId>javax.interceptor</groupId>
                    <artifactId>javax.interceptor-api</artifactId>
                    <version>1.2</version>
                </dependency>
    
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>fastjson</artifactId>
                    <version>1.2.47</version>
                </dependency>
    
                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>1.18.0</version>
                </dependency>
    
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.1.47</version>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.security</groupId>
                    <artifactId>spring-security-jwt</artifactId>
                    <version>1.0.10.RELEASE</version>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.security.oauth.boot</groupId>
                    <artifactId>spring-security-oauth2-autoconfigure</artifactId>
                    <version>2.1.3.RELEASE</version>
                </dependency>
            </dependencies>
    
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    创建UAA授权服务工程

    • 1.创建distributed-security-uaa
    • 创建distributed-security-uaa作为授权服务工程,依赖如下:
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.stan.security</groupId>
            <artifactId>distributed-security</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <relativePath/>
        </parent>
        <groupId>com.stan.security</groupId>
        <artifactId>distributed-security-uaa</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>distributed-security-uaa</name>
        <description>Demo project for Spring Boot</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-freemarker</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-commons</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-openfeign</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.netflix.hystrix</groupId>
                <artifactId>hystrix-javanica</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-security</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-oauth2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.security</groupId>
                <artifactId>spring-security-jwt</artifactId>
            </dependency>
    
            <dependency>
                <groupId>javax.interceptor</groupId>
                <artifactId>javax.interceptor-api</artifactId>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    • 2.启动类
    • 本工程采用SpringBoot开发,每个工程编写一个启动类:
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableHystrix
    @EnableFeignClients(basePackages = {"com.stan.security"})
    public class DistributedSecurityUaaApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DistributedSecurityUaaApplication.class, args);
        }
    
    }
    
    • 3.配置文件
    • 在resources下创建application.yml
    spring:
      application:
        name: uaa-service
      main:
        allow-bean-definition-overriding: true
      http:
        encoding:
          enabled: true
          charset: UTF-8
          force: true
      freemarker:
        enabled: true
        suffix: .html
        request-context-attribute: rc
        content-type: text/html
        charset: UTF-8
      mvc:
        throw-exception-if-no-handler-found: true
      resources:
        add-mappings: false
      datasource:
        url: jdbc:mysql://localhost:3306/user_db?useUnicode=true
        username: root
        password: 123456
        driver-class-name: com.mysql.jdbc.Driver
    server:
      port: 53020
      tomcat:
        remote-ip-header: x-forwarded-for
        protocol-header: x-forwarded-proto
      use-forward-headers: true
      servlet:
        context-path: /uaa
    logging:
      level:
        root: debug
          org:
            springframework:
              web: info
    management:
      endpoints:
        web:
          exposure:
            include: refresh,health,info,env
    feign:
      hystrix:
        enabled: true
      compression:
        request:
          enabled: true
          mime-types[0]: text/html
          mime-types[1]: application/xml
          mime-types[2]: application/json
          min-request-size: 2048
        response:
          enabled: true
    

    创建Order资源服务

    • 本工程为Order订单服务工程,访问本工程的资源需要认证通过。
    • 本工程的目的主要是测试认证授权的功能,所以不涉及订单管理相关业务。
    • 1.创建Order工程
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.stan.security</groupId>
            <artifactId>distributed-security</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <relativePath/>
        </parent>
        <groupId>com.stan.security</groupId>
        <artifactId>distributed-security-order</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>distributed-security-order</name>
        <description>订单服务</description>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-security</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-oauth2</artifactId>
            </dependency>
    
            <dependency>
                <groupId>javax.interceptor</groupId>
                <artifactId>javax.interceptor-api</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
                <exclusions>
                    <exclusion>
                        <groupId>org.junit.vintage</groupId>
                        <artifactId>junit-vintage-engine</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    • 2.启动类
    @SpringBootApplication
    @EnableDiscoveryClient
    public class DistributedSecurityOrderApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DistributedSecurityOrderApplication.class, args);
        }
    
    }
    
    • 3.配置文件
    server:
      port: 53021
      tomcat:
        remote-ip-header: x-forwarded-for
        protocol-header: x-forwarded-proto
      use-forward-headers: true
      servlet:
        context-path: /order
    spring:
      application:
        name: order-service
      main:
        allow-bean-definition-overriding: true
      http:
        encoding:
          enabled: true
          charset: UTF-8
          force: true
      freemarker:
        enabled: true
        suffix: .html
        request-context-attribute: rc
        content-type: text/html
        charset: UTF-8
      mvc:
        throw-exception-if-no-handler-found: true
      resources:
        add-mappings: false
    logging:
      level:
        root: info
        org:
          springframework:
            web: info
    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:53000/eureka/
      instance:
        prefer-ip-address: true
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-adress}:${spring.application.instance_id:${server.port}}
    management:
      endpoints:
        web:
          exposure:
            include: refresh,health,info,env
    feign:
      hystrix:
        enabled: true
      compression:
        request:
          enabled: true
          mime-types[0]: text/html
          mime-types[1]: application/xml
          mime-types[2]: application/json
          min-request-size: 2048
        response:
          enabled: true
    

    相关文章

      网友评论

        本文标题:28.OAuth2.0-Spring Cloud Securit

        本文链接:https://www.haomeiwen.com/subject/gsjbuhtx.html