1

我正嘗試使用Angular 2前端應用程序作爲將從Spring RESTful Web服務使用資源的客戶端。如何使用OAuth2和社交登錄來保護Spring Boot RESTful服務

因此想到使用OAuth 2身份驗證和社交登錄(Google和Facebook)來保護此Web服務。

成功登錄與社會登錄它不是重定向到URL(端口處角2在當地環境與3000端口上運行),從中我提出的要求,但其重定向到8080端口在本地環境後

本地主機:3000 - 前端 本地主機:8080 - OAuth的

我跟着這個教程https://spring.io/guides/tutorials/spring-boot-oauth2/對於上述方案,但他們使用的Java應用程序作爲他們的客戶端,它與註釋處理。

回答

1

我能夠通過以下兩個示例應用程序,以下實現與多個基於REST資源社會登錄的步驟是:

(1)結帳https://github.com/spring-guides/tut-spring-security-and-angular-js/tree/master/oauth2

(2)刪除「authserver」文件夾(我們將使用來自另一個項目auth服務器)

(3)從結賬社會演示auth服務器:https://github.com/spring-guides/tut-spring-boot-oauth2/tree/master/auth-server

(4) 「UI」 項目的開放application.yml,並做如下改變:

server.port: 9001 
server.context-path: /zuul 
debug: true 

spring: 
    aop: 
    proxy-target-class: true 


security: 
    oauth2: 
    client: 
     client-id: acme 
     client-secret: acmesecret 
     access-token-uri: http://localhost:8080/oauth/token 
     user-authorization-uri: http://localhost:8080/oauth/authorize 
     grant-type: implicit 
    resource: 
     user-info-uri: http://localhost:8080/me 

zuul: 
    routes: 
    resource: 
     path: /resource/** 
     url: http://localhost:9000/resource 
    user: 
     path: /user/** 
     url: http://localhost:8080/me 

logging: 
    level: 
    org.springframework.security: DEBUG 

(5)auth服務器的開放application.yml並添加谷歌屬性:

google: 
    client: 
    clientId: <your client id> 
    clientSecret: <your client secret> 
    accessTokenUri: https://accounts.google.com/o/oauth2/token 
    scope: profile,email 
    userAuthorizationUri: https://accounts.google.com/o/oauth2/auth 
    clientAuthenticationScheme: form 
    redirect-uri: http://localhost:8080 
    resource: 
    userInfoUri: https://www.googleapis.com/plus/v1/people/me 

(6)auth服務器的開放SocialApplication.java: 添加谷歌相關豆和過濾器(類似於facebook和github)。

(7)重命名application.properties到application.yml 「資源」 項目 以下是陽明的內容:

server: 
    port: 9000 
    context-path: /resource 
security: 
    oauth2: 
    resource: 
     user-info-uri: http://localhost:8080/me 

logging: 
    level: 
    org.springframework.security: DEBUG 
    org.springframework.web: DEBUG 

(8)現在運行auth服務器,資源和用戶界面項目和點擊端口9001和context/zuul。

+0

但我的UI項目是在Angular 2中,它沒有Spring代碼。所以我無法更改UI項目中的application.properties –

+0

在上述步驟中,UI項目意味着Zuul項目。你可以讓你的UI項目與Spring Boot Jar分開。 –

+0

我是Zuul新手,可以在GitHub中看到您的代碼嗎? @Hitesh shekhada –

相關問題