上一篇文章《实战Spring Cloud Oauth2系列(十二)在Spring cloud gateway里面添加oauth拦截》我们介绍了把oauth的拦截放在gateway里面,然后通过spring cloud gateway拦截token访问后端的服务。这篇文章我们把oauth服务业纳入到gateway里面,通过gateway访问oauth服务。
把oauth服务放到gateway里面主要是需要做两件事件:
第一件事是添加oauth服务的路由,这里比较简单,在配置文件里面添加即可,示例如下:
- id: shop-oauth-server uri: lb://shop-oauth-server predicates: - Path=/oauth/** filters: - StripPrefix=1
然后就是放开以oauth开头的请求路径,把oauth开头的路径放入白名单里面,不进行accesstoken的校验,示例如下:
permitAll.add("/oauth/**");
做了这两步之后,我们就可以通过gateway正常访问了。
登录的地址是:
http://127.0.0.1:8080/oauth/oauth/token?grant_type=password&scope=all&client_id=book&client_secret=book&username=wang&password=wang&device_type=102
可以看到能访问
刷新token的请求地址如下:
http://127.0.0.1:8080/oauth/oauth/token?grant_type=refresh_token&client_id=book&client_secret=book&refresh_token=ccc17ffe-eeca-4d6c-96ea-7d8be5b852c7
可以看到也是没有问题的
最后按照惯例,附上本案例的源码,登录后即可下载。
还没有评论,来说两句吧...