2016-05-06 208 views
1

我们正在尝试做一个POC,我们将使用wso2身份服务器作为Oauth2身份验证服务器,并且我们使用ZUUL作为API网关Spring Boot Oauth2 SSO与wso2身份服务器使用zuul作为api-gateway与领事服务发现

zuul代理服务

@EnableZuulProxy 
@EnableOAuth2Sso 
@SpringBootApplication 
public class ApiGatewayApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(ApiGatewayApplication.class, args); 
    } 
} 

application.properties

security.basic.enabled=false 
security.oauth2.client.accessTokenUri=https://172.16.0.102:9443/oauth2/token 
security.oauth2.client.clientId=erX25bMjkEwIS7ZDxP5vYRM1r5Ya 
security.oauth2.client.clientSecret=A2kDWZ_WVProSgf2TuNE15jy8Oga 
security.oauth2.client.userAuthorizationUri=https://172.16.0.102:9443/oauth2/authorize 
security.oauth2.client.preEstablishedRedirectUri=https://172.16.0.42:8900/resource/greeting 
security.oauth2.client.useCurrentUri=false 
security.oauth2.resource.userInfoUri=https://172.16.0.102:9443/oauth2/userinfo 
security.oauth2.resource.preferTokenInfo=false 
server.port=8900 
spring.application.name=ZUUL 
zuul.ignoreLocalService=false 
server.ssl.key-store=wso2carbon.jks 
server.ssl.key-store-password=wso2carbon 
server.ssl.keyStoreType=jks 
security.oauth2.client.grantType=authorization_code 

zuul.routes.resource.path=/resource/** 
zuul.routes.resource.url=https://localhost:8090 

现在面临两个问题

  1. 我们是不是能够整合领事,ZUUL服务,我的服务能够与领事注册,但是当我添加@EnableZuulProxy我的服务无法连接到领事它 抛出一个异常

    com.ecwid.consul.transport.TransportException: java.net.ConnectException:连接被拒绝:连接

  2. 我们现在可以重定向到wso2 IS的登录页面,当我们尝试登录时,它会尝试将我重定向回到回拨URL ,但我的浏览器无法获得访问令牌,并且它说太多重定向。我无法理解,是因为我的配置还是其他原因。 我试图清除我的历史表明,当我GOOGLE了它,但它不工作

如果任何人有任何想法,这个请大家帮忙.....

回答

0

我认为可能会发生您的第二个问题是OAuth2授权/资源服务器在网关的重定向循环中被捕获。

您的登录成功将您重定向到您的/resource/greeting页面,我认为这是一个受保护的资源。然后,网关无法验证您的令牌并将您重定向回登录(其中会话已经过验证,并将您重定向回)。

您需要确定网关无法验证令牌的原因。有几件事你可以尝试,这可以揭示潜在的问题。

首先要尝试使用CURL命令验证令牌。从auth服务器检索到有效令牌后,在获取受保护资源(如https://172.16.0.42:8900/resource/greetinghttps://172.16.0.102:9443/oauth2/userinfo)时,将不记名令牌添加到请求头。

如果您可以使用CURL验证令牌,那么您知道事情已经正确设置,并且网关应该能够回拨资源服务器以获取经过验证的用户信息。如果是这种情况,那么您可以在网关应用程序中诊断问题。

此时,一种可能性可能与无效的CSRF令牌有关。我把一个例子放在一起,就像你想要做的一样,并且禁用了CSRF令牌。禁用网关中的CSRF仅用于诊断目的,不推荐作为长期解决方案。

https://github.com/kbastani/spring-cloud-event-sourcing-example/blob/master/edge-service/src/main/java/demo/EdgeApplication.java#L28

也有一些问题,你的Zuul网关配置。您似乎将/resources/**路由到https://localhost:8090。如果我假设正确,正因为如此,你不能从网关获得代理服务。建议您的后端服务指定其中哪些资源受到保护,以便网关可以同时提供受保护和未受保护的API资源。

见的配置在这个例子:

https://github.com/kbastani/spring-cloud-event-sourcing-example/blob/master/edge-service/src/main/resources/application.yml#L38

关于你的第一个问题,我没有一个很好的答案。我怀疑在添加@EnableZuulProxy的情况下,Consul的默认连接URL会发生更改。我会在Consul配置属性类中断开你的应用程序。在这里看到:

https://github.com/spring-cloud/spring-cloud-consul/blob/master/spring-cloud-consul-discovery/src/main/java/org/springframework/cloud/consul/discovery/ConsulDiscoveryProperties.java

+1

感谢您的回复,它确实有助于理解问题,但问题的实际原因是自签名证书,我会更新答案 –

0

不管肯尼曾建议是有效的,但在我们的例子中,我们所面临的,因为我们的时候,我们安装在信任存储证书,它的工作是使用自签名证书重定向循环问题!还需要有在性能做一些修改为WSO2,我们就安装证书后,就知道

security: 
    oauth2: 
    client: 
     clientId: fdrfsdgersdgdrf 
     clientSecret: dsgardgdfgadfgad 
     accessTokenUri: https://localhost:9443/oauth2/token 
     userAuthorizationUri: https://localhost:9443/oauth2/authorize 
     clientAuthenticationScheme: form 
     scope: openid 
     use-current-uri: true 
    resource: 
     userInfoUri: https://localhost:9443/oauth2/userinfo?schema=openid 
spring: 
    resources: 
    chain: 
     enabled: true 

server: 
    ssl: 
    key-store: localhost.pfx 
    key-password: localhost 
    key-store-type: PKCS12 

    trust-store: localhost.pfx 
    trust-store-password: localhost 
    trust-store-type: PKCS12 
    port: 8080 

我在POM解决了我的第一个问题,通过改变依赖

以前

<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-consul-all</artifactId> 
</dependency> 

<dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-consul-discovery</artifactId> 
</dependency>