gpt4 book ai didi

java - Spring Social facebook + spring security 对电子邮件和用户名范围的权限

转载 作者:太空宇宙 更新时间:2023-11-04 14:32:07 25 4
gpt4 key购买 nike

我想使用 Spring Security 将 Spring Social facebook 集成到我的应用程序中(我使用注释基础配置)。我所需要的只是将 Facebook 帐户与我的应用程序帐户连接起来。在简单的例子中我发现了这个:

package eu.thinking4u.spring.social.simple.signinmvc.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.UserIdSource;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurer;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ConnectController;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.security.AuthenticationNameUserIdSource;


@Configuration
@EnableSocial
public class SocialContext implements SocialConfigurer {

@Autowired
private DataSource dataSource;

/**
* Configures the connection factories for Facebook and Twitter.
* @param cfConfig
* @param env
*/
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig, Environment env) {
/* cfConfig.addConnectionFactory(new TwitterConnectionFactory(
env.getProperty("twitter.consumer.key"),
env.getProperty("twitter.consumer.secret")
));*/
/*cfConfig.addConnectionFactory(new FacebookConnectionFactory(
env.getProperty("facebook.app.id"),
env.getProperty("facebook.app.secret")

));*/
final FacebookConnectionFactory fbcf = new FacebookConnectionFactory(env.getProperty("facebook.app.id"),
env.getProperty("facebook.app.secret"));
fbcf.setScope("email");
cfConfig.addConnectionFactory(fbcf);
}

/**
* The UserIdSource determines the account ID of the user. The example application
* uses the username as the account ID.
*/
public UserIdSource getUserIdSource() {
return new AuthenticationNameUserIdSource();
}

public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
return new JdbcUsersConnectionRepository(
dataSource,
connectionFactoryLocator,
/**
* The TextEncryptor object encrypts the authorization details of the connection. In
* our example, the authorization details are stored as plain text.
* DO NOT USE THIS IN PRODUCTION.
*/
Encryptors.noOpText()
);
}

/**
* This bean manages the connection flow between the account provider and
* the example application.
*/
@Bean
public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) {
return new ConnectController(connectionFactoryLocator, connectionRepository);
}

/* @Bean
public CanvasSignInController canvasSignInController(ConnectionFactoryLocator connectionFactoryLocator,
UsersConnectionRepository usersConnectionRepository, Environment env)
{
return new CanvasSignInController(connectionFactoryLocator, usersConnectionRepository,
new SimpleSignInAdapter(), env.getProperty("facebook.appKey"), env.getProperty("facebook.appSecret"),
env.getProperty("facebook.canvasPage"));
}*/
}

登录 Facebook 后,我们尝试获取用户个人资料,例如

WebRequest request
RegistrationForm dto = new RegistrationForm();
Connection<?> connection = ProviderSignInUtils.getConnection(request);

if (connection != null) {
UserProfile socialMediaProfile = connection.fetchUserProfile();
System.out.println("DTO EMAIL--->"+socialMediaProfile.getEmail());
dto.setEmail(socialMediaProfile.getEmail());
dto.setFirstName(socialMediaProfile.getFirstName());
dto.setLastName(socialMediaProfile.getLastName());
}

我尝试获取电子邮件和用户名,但不起作用。并且正确获取了名字和姓氏。

最佳答案

我相信这是当前版本的 Spring Social 中的一个错误。目前正在此处跟踪: https://jira.spring.io/browse/SOCIAL-436

与此同时,您可以通过页面上的隐藏字段设置范围来解决此问题,如下所示:

<form action="<c:url value="/connect/facebook" />" method="POST">
<input type="hidden" name="scope" value="email" />
<p>You haven't created any connections with Facebook yet. Click the button to create
a connection between your account and your Facebook profile.</p>
<p><button type="submit"><img src="<c:url value="/resources/social/facebook/signin.png" />"/>
</button></p>
</form>

关于java - Spring Social facebook + spring security 对电子邮件和用户名范围的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26011083/

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