2

Gmail認証の要件を満たし、拡張可能なプロジェクトに取り組んでいます。 FacebookとGitHub認証の例があるこのチュートリアルhereに従っていました。だから私はGmailを試しましたが、私はこのエラーを解決して解決できず、解決しようとすると新しい例外が発生しています。コードが私の追加によって最も影響を受けないポイントだと私は信じています。この多くの設定とコードで、githubとfbでは動作しますが、Googleでは動作しません。Spring Boot OAuth2 - GoogleApi-トークンからユーザーの詳細を取得できませんでした

SocialApplication.java

@SpringBootApplication 
@RestController 
@EnableOAuth2Client 
@EnableAuthorizationServer 
@Order(6) 
public class SocialApplication extends WebSecurityConfigurerAdapter { 

    @Autowired 
    OAuth2ClientContext oauth2ClientContext; 

    @RequestMapping({ "/user", "/me" }) 
    public Map<String, String> user(Principal principal) { 
     Map<String, String> map = new LinkedHashMap<>(); 
     map.put("name", principal.getName()); 
     return map; 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http.antMatcher("/**").authorizeRequests().antMatchers("/", "/login**", "/webjars/**").permitAll().anyRequest() 
       .authenticated().and().exceptionHandling() 
       .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/")).and().logout() 
       .logoutSuccessUrl("/").permitAll().and().csrf() 
       .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and() 
       .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class); 
     // @formatter:on 
    } 

    @Configuration 
    @EnableResourceServer 
    protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter { 
     @Override 
     public void configure(HttpSecurity http) throws Exception { 
      // @formatter:off 
      http.antMatcher("/me").authorizeRequests().anyRequest().authenticated(); 
      // @formatter:on 
     } 
    } 

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

    @Bean 
    public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) { 
     FilterRegistrationBean registration = new FilterRegistrationBean(); 
     registration.setFilter(filter); 
     registration.setOrder(-100); 
     return registration; 
    } 

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

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

    **@Bean 
    @ConfigurationProperties("gmail") 
    ClientResources gmail(){return new ClientResources();}** 

    private Filter ssoFilter() { 
     CompositeFilter filter = new CompositeFilter(); 
     List<Filter> filters = new ArrayList<>(); 
     filters.add(ssoFilter(facebook(), "/login/facebook")); 
     filters.add(ssoFilter(github(), "/login/github")); 
     **filters.add(ssoFilter(gmail(), "/login/gmail"));** 
     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; 
    } 

} 

class ClientResources { 
    private OAuth2ProtectedResourceDetails client = new AuthorizationCodeResourceDetails(); 
    private ResourceServerProperties resource = new ResourceServerProperties(); 

    public OAuth2ProtectedResourceDetails getClient() { 
     return client; 
    } 

    public ResourceServerProperties getResource() { 
     return resource; 
    } 
} 

index.htmlを

<div> 
    With Facebook: <a href="/login/facebook">click here</a> 
</div> 
<div> 
    With Github: <a href="/login/github">click here</a> 
</div> 
**<div> 
    With Gmail: <a href="/login/gmail">click here</a> 
</div>** 

application.yml //はスペース

gmail: 
    client: 
    client_id: 7xxxxxxxx-1spjexxxxxxxc.apps.googleusercontent.com 
    scope: https://www.googleapis.com/auth/userinfo.profile 
    client_secret: Xxxxxxx-I*****zx 
    userAuthorizationUri: https://accounts.google.com/o/oauth2/auth 
    accessTokenUri: https://accounts.google.com/o/oauth2/token 
    auth_provider_x509_cert_url:https://www.googleapis.com/oauth2/v1/certs 
を保存するためにgithubのとfbのためのコンテンツをスキップ

LOG

org.springframework.security.authentication.BadCredentialsException: は org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter.attemptAuthenticationでトークンからユーザーの詳細情報を取得できませんでした(OAuth2ClientAuthenticationProcessingFilter.java :122) 〜[スプリングセキュリティoauth2-2.0.10.RELEASE.jar:NA] org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:212) [ばねセキュリティ - でweb-4.1.1.RELEASE.jar:4.1.1.RELEASE] org.springframework.web.filter.CompositeFilter $ VirtualFilterChain.doFilter(CompositeFilter.java:112) の[spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE] org.springframework.security.web .Filter(AbstractAuthenticationProcessingFilter.java:200) [spring-security-web-4.1.1.RELEASE.jar: (CompositeFilter.java:112) [spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE] にあるorg.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200) [spring-security-web-4.1.1.RELEASE.jar:4.1.1.RELEASE] at org.springf ramework.web.filter.CompositeFilter $ VirtualFilterChain.doFilter(CompositeFilter.java:112) [spring-web-4.3.2.RELEASE.jar:4.3.2.RELEASE] にあるorg.springframework.web.filter.CompositeFilterです。 doFilter(CompositeFilter.java:73) [ばねウェブ4.3.2.RELEASE.jar:4.3.2.RELEASE]: org.springframework.security.oauth2.common.exceptions

**によって引き起こされます。 InvalidTokenException: org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices.loadAuthentication(UserInfoTokenServices.java:91) 〜[春-boot-でdCCnRbsve3 ***********屋autoconfigure-1.4.0.RELEASE.jar:1.4.0.RELEASE] at org.spri ngframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter.attemptAuthentication(OAuth2ClientAuthenticationProcessingFilter.java:112) 〜[spring-security-oauth2-2.0.10.RELEASE.jar:na] ...一般的な66のフレーム が省略されました

Google APIコンソール。

リダイレクトURL私はとして与えている:はlocalhost:8080 /ログインごapplication.ymlのconfirgurationで/ Gmailの

答えて

4

、私は定義されたのUserInfo URLを見つけることができませんでしたか? は、私は私のために働いて次のGoogleの設定を持っている:

FYI
google: 
    client: 
    clientId: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
    clientSecret: YYYYYYYYYYYYYYYYYY 
    accessTokenUri: https://accounts.google.com/o/oauth2/token 
    userAuthorizationUri: https://accounts.google.com/o/oauth2/auth 
    clientAuthenticationScheme: form 
    scope: profile email 
    resource: 
    userInfoUri: https://www.googleapis.com/userinfo/v2/me 
+1

:あなたは、Googleのドキュメントのどこかに指定されているように、あなたがそれを行うと、任意の悪意のあるリソースプロバイダがユーザーの1人になりすますことができ、認証のためにuserInfoUriを使用しないでください。 oAuth2は認証用、OpenidConnectは認証用です。 – user1928596

関連する問題