28.OAuth2.0-Spring Cloud Security OAuth2-环境搭建

环境搭建

父工程

  • 创建maven工程作为父工程,依赖如下:


    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.2.RELEASE
         
    
    com.stan.security
    distributed-security
    0.0.1-SNAPSHOT
    distributed-security
    pom
    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        Hoxton.RELEASE
        1.8
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            

            
                javax.servlet
                javax.servlet-api
                3.1.0
                provided
            

            
                javax.interceptor
                javax.interceptor-api
                1.2
            

            
                com.alibaba
                fastjson
                1.2.47
            

            
                org.projectlombok
                lombok
                1.18.0
            

            
                mysql
                mysql-connector-java
                5.1.47
            

            
                org.springframework.security
                spring-security-jwt
                1.0.10.RELEASE
            

            
                org.springframework.security.oauth.boot
                spring-security-oauth2-autoconfigure
                2.1.3.RELEASE
            
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


创建UAA授权服务工程

  • 1.创建distributed-security-uaa
  • 创建distributed-security-uaa作为授权服务工程,依赖如下:


    4.0.0
    
        com.stan.security
        distributed-security
        0.0.1-SNAPSHOT
        
    
    com.stan.security
    distributed-security-uaa
    0.0.1-SNAPSHOT
    distributed-security-uaa
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
            org.springframework.data
            spring-data-commons
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            com.netflix.hystrix
            hystrix-javanica
        

        
            org.springframework.retry
            spring-retry
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.cloud
            spring-cloud-starter-security
        

        
            org.springframework.cloud
            spring-cloud-starter-oauth2
        

        
            org.springframework.security
            spring-security-jwt
        

        
            javax.interceptor
            javax.interceptor-api
        

        
            mysql
            mysql-connector-java
        

        
            org.springframework.boot
            spring-boot-starter-jdbc
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


  • 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工程


    4.0.0
    
        com.stan.security
        distributed-security
        0.0.1-SNAPSHOT
        
    
    com.stan.security
    distributed-security-order
    0.0.1-SNAPSHOT
    distributed-security-order
    订单服务

    
        1.8
    

    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.cloud
            spring-cloud-starter-security
        

        
            org.springframework.cloud
            spring-cloud-starter-oauth2
        

        
            javax.interceptor
            javax.interceptor-api
        

        
            com.alibaba
            fastjson
        

        
            org.projectlombok
            lombok
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


  • 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 Security OAuth2-环境搭建)