2016-04-26 123 views
4

我使用Spring Boot,Spring MVC和spring-security-oauth2創建網站,並試圖通過Google,Facebook,GitHub ...驗證本地存儲的用戶... OAuth2服務。我也有標準的用戶名 - 密碼。Spring Boot OAuth2 - 使用本地用戶數據庫與Facebook進行身份驗證

我的想法是將每個用戶的[OAuth2_provider_type,OAuth2_subjectId]對從第一次成功oauth登錄存儲到本地數據庫,然後在用戶通過Google(或FB ...)進行身份驗證時使用此對來查找正確的用戶。再次。我已經正確地使用Google/Facebook進行身份驗證,但是我不知道如何將本地存儲的用戶連接到Spring OAuth2安全性並將它們傳遞給SecurityContext。

有人能指點我一些例子或集成測試,我可以看到類似的東西嗎?

在這裏,我發現偉大的教程Spring Boot and OAuth2與提示如何添加本地用戶數據庫。試圖從重定向到谷歌授權的重新導向URI這就好比.antMatchers("/user").authenticated()定義http://localhost:10001/user終點並獲得拒絕訪問

o.s.s.w.a.i.FilterSecurityInterceptor - Secure object: FilterInvocation: URL: /user?state=Wj7RVk&code=4/wa2AFtJr0K3cKTxDAYo8rTOu2p41km5o3YCPnimx4wU; Attributes: [authenticated] o.s.s.w.a.i.FilterSecurityInterceptor - Previously Authenticated: org.sprin[email protected]6fa8940c: Principal: anonymousUser; Credentials: [PROTECTED]; Authenticated: true; Details: org.sprin[email protected]fffde5d4: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 5888363FA9E329992073DCE4B21E8B5C; Granted Authorities: ROLE_ANONYMOUS o.s.s.access.vote.AffirmativeBased - Voter: org.sp[email protected]29802df4, returned: -1 o.s.s.w.a.ExceptionTranslationFilter - Access is denied (user is anonymous); redirecting to authentication entry point org.springframework.security.access.AccessDeniedException: Access is denied

非常感謝您的幫助!

回答

0

試圖從谷歌重定向到重定向授權URI http://localhost:10001/user

o.s.s.w.a.i.FilterSecurityInterceptor - 安全對象: FilterInvocation:URL: /用戶狀態= Wj7RVk &代碼= 4/wa2AFtJr0K3cKTxDAYo8rTOu2p41km5o3YCPnimx4wU; 屬性:[認證]

看起來你是在一個錯誤的方式做這件事,你不應該去/usercodestate PARAMS!流程是:

  1. 您註冊一個SSO端點像 localhost:8080/login/facebook
  2. 的網址一旦你點擊到這會導致你 重定向到Facebook的授權頁面
  3. 一旦被授權,你應該端點鏈接重定向回 localhost:8080/login/facebook與你的國家和代碼PARAMS: localhost:8080/login/facebook?state=Wj7RVk&code=4/wa2AFtJr0K3cKTxDAYo8rTOu2p41km5o3YCPnimx4wU
  4. 當SSO過濾得到的代碼再轉到Facebook來 excha NGE access_code到ACCESS_TOKEN並提出將令牌會議
  5. 在這個過程中AuthenticationSuccessHandler結束被稱爲根據其具體imlementation你重定向 (通常是 SavedRequestAwareAuthenticationSuccessHandler

如果這些步驟已成功完成您可以前往/user端點獲取一些用戶信息。

相關問題