java程序员微服务架构和实践

第十一节 资源服务器api-server集成zuul网关

2018-09-16  本文已影响6人  勃列日涅夫

zuul 集成spring security 作为边缘路由访问时的api权限控制策略

zuul的基本介绍已在第六节中有过基本介绍,可参考第六节 服务端负载均衡

关于资源服务器的api-server的配置使用如下:

  1. pom 添加依赖
 <dependency>
            <groupId>com.xzg</groupId>
            <artifactId>online-table-reservation-common</artifactId>
            <version>v1</version>
        </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-netflix-hystrix-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</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>
  1. 基本配置,启动类@EnableResourceServer标注该服务为资源服务器
@SpringBootApplication
@EnableEurekaClient
@EnableResourceServer
@Configuration
@ComponentScan({"com.xzg.api.service", "com.xzg.common"})
public class ApiApp {

    private static final Logger LOG = LoggerFactory.getLogger(ApiApp.class);
    static {
        // 本地测试
        LOG.warn("禁用ssl主机名检查,开发截断使用");
        HttpsURLConnection.setDefaultHostnameVerifier((hostname, sslSession) -> true);
    }
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        LOG.info("Register MDCHystrixConcurrencyStrategy");
        HystrixPlugins.getInstance().registerConcurrencyStrategy(new MDCHystrixConcurrencyStrategy());
        SpringApplication.run(ApiApp.class, args);
    }
}
  1. 配置文件中添加权限认证服务配置
#其他略
security:
  oauth2:
    resource:
      userInfoUri: https://localhost:9001/user
management:
  security:
    enabled: false

具体配置可参考源码

  1. 发送url获取code
https://localhost:9001/oauth/authorize?response_type=code&client_id=client&redirect_uri=http://localhost:7771/1&scope=apiAccess&state=553344
  1. 认证后取得code换取token
curl -X POST -k -H 'Content-Type: application/x-www-form-urlencoded' -i https://localhost:9001/oauth/token --data 'grant_type=authorization_code&client_id=client&redirect_uri=http://localhost:7771/1&code=OBbY4J'
  1. 获取token后使用token,就可以在请求资源的请求头添加Bearer token
curl -X GET -H 'Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ' -i http://localhost:7771/service

4 执行成功返回结果,Oauth2的基本也就实现了

请求:
https://localhost:9001/oauth/authorize?response_type=token&client_id=client&redirect_uri=http://localhost:7771&scope=apiAccess&state=553344
返回:
http://localhost:7771/#access_token=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2luZm8iOnsidXNlck5hbWUiOiJjbGllbnQiLCJwYXNzd29yOsTSQRjpKp2pE4ru2elm3uqFY_mduVtvwc92ZSPTNtTtBbijfNU86r7giIxsqaqaliu4pnvyXO2CWP7q74lOGWWDWDtI02u-a6jhpqauM-TjGHAMAxr-VUbyduw&token_type=bearer&state=553344&expires_in=7199&user_info=com.xzg.security.service.securityEtity.BaseUser@3e219620&jti=1521428f-5d94-4a72-befb-57531dab784a
curl -X POST -k -H 'Content-Type: application/x-www-form-urlencoded' -i https://localhost:9001/oauth/token --data 'grant_type=password&scope=apiAccess&client_id=client&username=client&password=password'

或者使用插件(火狐插件RESTClient)


图片.png

zuul 服务网关

zuul作为边缘路由,这里也属于资源服务,所以重点有两点配置,其一作为资源服务需要配置远程的权限服务器

security:
  oauth2:
    resource:
      userInfoUri: https://localhost:9001/user

同时作为边缘路由,需要配置路由链路

zuul:
    ignoredServices: "*"
    routes:
        restaurantapi:
            path: /api/**
            serviceId: api-service
            stripPrefix: true

其他配置具体可参考源码zuul服务源码
需要说明需要启动本zuul项目,需要依赖eureka server、security-server、rabbitmq、以及其他业务服务

上一篇下一篇

猜你喜欢

热点阅读