2017-08-18 112 views
0

我正在致力於Spring引導web應用程序。我現在正在使用具有自定義userDetailService的Spring Security來開發註冊和登錄系統。在我的Spring Boot + Security + web應用程序中添加可選的Google登錄

現在我想添加一個註冊登錄系統,使用Google帳號。我創建了我的Google API密鑰,並將它們添加到application.properties。我想是不是.yml propertie文件在這裏有必要使用:

# =============================== 
# = OAUTH2 
# =============================== 
security.oauth2.client.client-id=clientId Here 
security.oauth2.client.client-secret=clientSecret here 
security.oauth2.client.access-token-uri=https://www.googleapis.com/oauth2/v3/token 
security.oauth2.client.user-authorization-uri=https://accounts.google.com/o/oauth2/auth 
security.oauth2.client.token-name=oauth_token 
security.oauth2.client.authentication-scheme=query 
security.oauth2.client.client-authentication-scheme=form 
security.oauth2.client.scope=profile 
security.oauth2.resource.user-info-uri=https://www.googleapis.com/userinfo/v2/me 
security.oauth2.resource.prefer-token-info=false 

我加的OAuth2支持我春季啓動應用程序在這條路上:

@SpringBootApplication 
@EnableOAuth2Sso 
public class WebApplication { 

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

現在我想保持posibility到使用Google登錄或使用網站帳戶登錄,但我只找到關於唯一登錄或多個提供商登錄(Facebook,Google,Twitter ..)的手冊

在我的SpringSec我有這個完整的配置類。我想,我要創建谷歌的的AuthenticationProvider並將其鏈接到我的應用程序的谷歌訪問的URL,但我很困惑又一下:

@Autowired 
     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 

      /** 
      * Obtenemos información de persistencia 
      */ 
      // @formatter:off 
      auth 
       //.authenticationProvider(googleOauth2AuthProvider()) 
       .userDetailsService(userDetailsService) 
       .passwordEncoder(bCryptPasswordEncoder); 
      // @formatter:on 
    } 
    ... 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     String[] anonymousRequest = { urls}; 

     http 
     .authorizeRequests() 
     //..other rules 

回答

1

你必須使用在您配置所需的身份驗證提供者的複合過濾器,例如:

private Filter ssoFilter() { 
    CompositeFilter filter = new CompositeFilter(); 
    List<Filter> filters = new ArrayList<>(); 
    filters.add(ssoFilter(facebook(), "/login/facebook")); 
    filters.add(ssoFilter(google(), "/login/google")); 
    filter.setFilters(filters); 
    return filter; 
} 

private Filter ssoFilter(ClientResources client, String path) { 
    OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter = new OAuth2ClientAuthenticationProcessingFilter(
      path); 
    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext); 

    oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate); 
    UserInfoTokenServices tokenServices = new UserInfoTokenServices(client.getResource().getUserInfoUri(), 
      client.getClient().getClientId()); 

    tokenServices.setRestTemplate(oAuth2RestTemplate); 
    oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices); 
    return oAuth2ClientAuthenticationFilter; 
} 

其中:

@Bean 
@ConfigurationProperties("google") 
public ClientResources google() { 
    return new ClientResources(); 
} 

@Bean 
@ConfigurationProperties("facebook") 
public ClientResources facebook() { 
    return new ClientResources(); 
} 

和:

class ClientResources { 

    @NestedConfigurationProperty 
    private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails(); 


    @NestedConfigurationProperty 
    private ResourceServerProperties resource = new ResourceServerProperties(); 

    public AuthorizationCodeResourceDetails getClient() { 
     return client; 
    } 

    public ResourceServerProperties getResource() { 
     return resource; 
    } 
} 

最後,BasicAuthenticationFilter一樣前添加過濾器在你的HTTP安全性配置:

@Override 
    protected void configure(HttpSecurity http) throws Exception { 
     String[] anonymousRequest = { urls}; 

     http 
     .authorizeRequests() 
     //..other rules 
     addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class); 

PS:你的配置屬性已開始在@ConfigurationProperties("facebook")指定的值:

facebook: 
    client: 
    clientId: yourCliendId 
    clientSecret: yourClientSecret 
    accessTokenUri: https://graph.facebook.com/oauth/access_token 
    userAuthorizationUri: https://www.facebook.com/dialog/oauth 
    tokenName: oauth_token 
    authenticationScheme: query 
    registeredRedirectUri: http://localhost:8083/app.html 
    preEstablishedRedirectUri: http://localhost:8083/app.html 
    clientAuthenticationScheme: form 
    resource: 
    userInfoUri: https://graph.facebook.com/me 

這是靈感從這裏介紹的例子:https://github.com/spring-guides/tut-spring-boot-oauth2/tree/master/github

+0

謝謝,它正在處理這個信息:) – Genaut

1

您可以使用Spring社會或的oauth2

實現這一目標

如果你想使用春季社交,請注意,在春季啓動社交中默認不支持谷歌,所以你必須做一些額外的步驟。

  1. 添加Maven依賴

    <dependency> 
        <groupId>org.springframework.social</groupId> 
        <artifactId>spring-social-google</artifactId> 
        <version>1.0.0.RELEASE</version> 
    </dependency> 
    
  2. 添加GoogleAutoConfiguration類

請按Ctrl + Shift + T在你的IDE(eclipse)並尋找FacebookAutoConfiguration類,你應該能夠可以在spring-autoconfigure.jar的org.springframework.boot.autoconfigure.social包中找到它。複製此文件並用Google替換Facebook。

3.添加GoogleProperties

在同一個包添加下面的類

@ConfigurationProperties(prefix = "spring.social.google") 

public class GoogleProperties extends SocialProperties{ 

更新application.properties與谷歌API密鑰

Follow this link for complete description and step by step instruction

希望它能幫助! !

如果你想用做的oauth2 here is a working example

相關問題