gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:52:21 24 4
gpt4 key购买 nike

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

我正在使用带有这些附加依赖项的 Spring Boot 1.4.1:

<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>

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

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

这是我当前的 Spring Security 配置:

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();

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

<小时/>

更新经过一番谷歌搜索后,我发现可行的方法可能是使用此依赖项:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-social-facebook</artifactId>
</dependency>

代替普通的spring-security-oauth2,但我仍然找不到一个简单的方法来实现我想要的。

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

最佳答案

我尝试将本教程用于其他社交网络,但我发现您作为客户端的应用程序必须在 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 过程中用于获取授权 token

我通过我的授权服务器使用Spring配置简单的OAuth2解决了我的问题,你可以检查我的answer如果您还没有找到如何操作,请点击这里

关于java - 将 Facebook 身份验证添加到我的 Spring MVC 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40086461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com