将Facebook身份验证添加到我的Spring MVC应用程序中

时间:2016-10-17 12:31:20

标签: java spring-security spring-boot spring-social spring-social-facebook

我正在尝试将OAuth2身份验证配置到当前配置为使用简单FORM身份验证的现有Spring MVC应用程序中。我想使用Facebook作为身份验证提供程序,但我还需要维护表单身份验证并在两种身份验证方法中以相同的方式在应用程序数据库中注册用户数据(显然,对于社交身份验证,某些字段将不存在,例如{。{{ 1}})。

我正在使用Spring Boot 1.4.1以及这些额外的依赖项:

password

但我找不到教程或一些明确的文档来解释如何解决我的案例(FORM + OAuth2)。

我试图关注this tutorial,但它基于Spring Boot + AngularJS,而我使用纯HTML + Thymeleaf。

这是我目前的Spring Security配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security.oauth</groupId>
    <artifactId>spring-security-oauth2</artifactId>
</dependency>

我应该添加什么来配置OAuth2并在验证后获取用户信息?

UPDATE 经过一些googlin后,我发现可以采用这种依赖方式:

http.authorizeRequests() //
     .antMatchers("/css/**").permitAll() // static resources
     .antMatchers("/webjars/**").permitAll() // static resources
     .antMatchers("/images/**").permitAll() // static resources
     .antMatchers("/login").permitAll().anyRequest().authenticated();
http.formLogin()
     .failureUrl("/login?error").defaultSuccessUrl("/").loginPage("/login").permitAll()//
     .and().logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
     .logoutSuccessUrl("/login?logout").permitAll();

取代普通<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-social-facebook</artifactId> </dependency> ,但我仍然找不到一种简单的方法来实现我想要的目标。

我找到的最好的教程是this,但有点过时并且不是基于Spring Boot,所以我无法理解是否存在某些实用程序和自动配置。

1 个答案:

答案 0 :(得分:0)

我尝试将此教程用于其他社交网络,但我得到了您的应用程序,因为客户端必须在 application.yml 中提供下一个配置:

facebook:
  client:
    clientId: 233668646673605
    clientSecret: 33b17e044ee6a4fa383f46ec6e28ea1d
    accessTokenUri: https://graph.facebook.com/oauth/access_token
    userAuthorizationUri: https://www.facebook.com/dialog/oauth
    tokenName: oauth_token
    authenticationScheme: query
    clientAuthenticationScheme: form
  resource:
    userInfoUri: https://graph.facebook.com/me

此信息将在outh2过程中用于获取授权令牌

我通过我的授权服务器使用Spring配置简单的OAuth2来解决我的问题,如果你还没找到怎么做,你可以在这里查看我的answer