Java源码示例:org.springframework.social.connect.UserProfile
示例1
public static CalendarUser createCalendarUserFromProvider(Connection<?> connection){
// TODO: FIXME: Need to put this into a Utility:
UserProfile profile = connection.fetchUserProfile();
CalendarUser user = new CalendarUser();
if(profile.getEmail() != null){
user.setEmail(profile.getEmail());
}
else if(profile.getUsername() != null){
user.setEmail(profile.getUsername());
}
else {
user.setEmail(connection.getDisplayName());
}
user.setFirstName(profile.getFirstName());
user.setLastName(profile.getLastName());
user.setPassword(randomAlphabetic(32));
return user;
}
示例2
@RequestMapping(value="/signup", method = RequestMethod.GET)
public String getForm(NativeWebRequest request, @ModelAttribute User user) {
String view = successView;
// check if this is a new user signing in via Spring Social
Connection<?> connection = providerSignInUtils.getConnectionFromSession(request);
if (connection != null) {
// populate new User from social connection user profile
UserProfile userProfile = connection.fetchUserProfile();
user.setId(userProfile.getUsername());
// finish social signup/login
providerSignInUtils.doPostSignUp(user.getUsername(), request);
// sign the user in and send them to the user home page
signInAdapter.signIn(user.getUsername(), connection, request);
view += "?spi="+ user.getUsername();
}
return view;
}
示例3
@Override
public UserAccount createUserAccount(ConnectionData data, UserProfile profile) {
long userIdSequence = this.counterService.getNextUserIdSequence();
UserRoleType[] roles = (userIdSequence == 1l) ?
new UserRoleType[] { UserRoleType.ROLE_USER, UserRoleType.ROLE_AUTHOR, UserRoleType.ROLE_ADMIN } :
new UserRoleType[] { UserRoleType.ROLE_USER };
UserAccount account = new UserAccount(USER_ID_PREFIX + userIdSequence, roles);
account.setEmail(profile.getEmail());
account.setDisplayName(data.getDisplayName());
account.setImageUrl(data.getImageUrl());
if (userIdSequence == 1l) {
account.setTrustedAccount(true);
}
LOGGER.info(String.format("A new user is created (userId='%s') for '%s' with email '%s'.", account.getUserId(),
account.getDisplayName(), account.getEmail()));
return this.accountRepository.save(account);
}
示例4
@Override
public OAuth2Authentication loadAuthentication(String accessToken)
throws AuthenticationException, InvalidTokenException {
AccessGrant accessGrant = new AccessGrant(accessToken);
Connection<?> connection = this.connectionFactory.createConnection(accessGrant);
UserProfile user = connection.fetchUserProfile();
return extractAuthentication(user);
}
示例5
private OAuth2Authentication extractAuthentication(UserProfile user) {
String principal = user.getUsername();
List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER");
OAuth2Request request = new OAuth2Request(null, this.clientId, null, true, null, null, null, null, null);
return new OAuth2Authentication(request,
new UsernamePasswordAuthenticationToken(principal, "N/A", authorities));
}
示例6
public static CalendarUser createCalendarUserFromProvider(Connection<?> connection){
// TODO: There is a defect with Facebook:
// org.springframework.social.UncategorizedApiException: (#12) bio field is
// deprecated for versions v2.8 and higher:
//
// Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);
// Facebook facebook = connection.getApi();
// String [] fields = { "id", "email", "first_name", "last_name" };
// User userProfile = facebook.fetchObject("me", User.class, fields);
//
// Object api = connection.getApi();
// if(api instanceof FacebookTemplate){
// System.out.println("Facebook");
// }
// Does not work with spring-social-facebook:2.0.3.RELEASE
UserProfile profile = connection.fetchUserProfile();
CalendarUser user = new CalendarUser();
if(profile.getEmail() != null){
user.setEmail(profile.getEmail());
}
else if(profile.getUsername() != null){
user.setEmail(profile.getUsername());
}
else {
user.setEmail(connection.getDisplayName());
}
user.setFirstName(profile.getFirstName());
user.setLastName(profile.getLastName());
user.setPassword(randomAlphabetic(32));
return user;
}
示例7
public void createSocialUser(Connection<?> connection, String langKey) {
if (connection == null) {
log.error("Cannot create social user because connection is null");
throw new IllegalArgumentException("Connection cannot be null");
}
UserProfile userProfile = connection.fetchUserProfile();
String providerId = connection.getKey().getProviderId();
User user = createUserIfNotExist(userProfile, langKey, providerId);
createSocialConnection(user.getLogin(), connection);
mailService.sendSocialRegistrationValidationEmail(user, providerId);
}
示例8
private User createUserIfNotExist(UserProfile userProfile, String langKey, String providerId) {
String email = userProfile.getEmail();
String userName = userProfile.getUsername();
if (StringUtils.isBlank(email) && StringUtils.isBlank(userName)) {
log.error("Cannot create social user because email and login are null");
throw new IllegalArgumentException("Email and login cannot be null");
}
if (StringUtils.isBlank(email) && userRepository.findOneByLogin(userName).isPresent()) {
log.error("Cannot create social user because email is null and login already exist, login -> {}", userName);
throw new IllegalArgumentException("Email cannot be null with an existing login");
}
Optional<User> user = userRepository.findOneByEmail(email);
if (user.isPresent()) {
log.info("User already exist associate the connection to this account");
return user.get();
}
String login = getLoginDependingOnProviderId(userProfile, providerId);
String encryptedPassword = passwordEncoder.encode(RandomStringUtils.random(10));
Set<Authority> authorities = new HashSet<>(1);
authorities.add(authorityRepository.findOne("ROLE_USER"));
User newUser = new User();
newUser.setLogin(login);
newUser.setPassword(encryptedPassword);
newUser.setFirstName(userProfile.getFirstName());
newUser.setLastName(userProfile.getLastName());
newUser.setEmail(email);
newUser.setActivated(true);
newUser.setAuthorities(authorities);
newUser.setLangKey(langKey);
return userRepository.save(newUser);
}
示例9
/**
* @return login if provider manage a login like Twitter or Github otherwise email address.
* Because provider like Google or Facebook didn't provide login or login like "12099388847393"
*/
private String getLoginDependingOnProviderId(UserProfile userProfile, String providerId) {
switch (providerId) {
case "twitter":
return userProfile.getUsername().toLowerCase();
default:
return userProfile.getEmail();
}
}
示例10
@Override
public String execute(Connection<?> connection) {
UserProfile profile = connection.fetchUserProfile();
String username = profile.getUsername();
logger.info("Creating user with id: {}", username);
User user = new User(username, "", Collections.emptyList());
userDetailsManager.createUser(user);
return username;
}
示例11
@Override
public UserProfile fetchUserProfile(Yahoo2 yahoo) {
TinyUsercard tinyUserCard = yahoo.profilesOperations().getTinyUsercard();
return new YahooUserProfile(
tinyUserCard.getProfile().getNickname(),
tinyUserCard.getProfile().getGivenName(),
tinyUserCard.getProfile().getFamilyName(),
null,
tinyUserCard.getProfile().getGuid());
}
示例12
public UserProfile fetchUserProfile(Plus api) {
Person person = api.getPeopleOperations().get("me");
UserProfileBuilder builder = new UserProfileBuilder().setFirstName(person.getName().getGivenName()).setLastName(person.getName().getFamilyName());
builder.setEmail(person.getGoogleAccountEmail());
builder.setName(person.getName().getFormatted());
return builder.build();
}
示例13
@Override
public UserProfile fetchUserProfile(Wechat api) {
return null;
}
示例14
@Override
public UserProfile fetchUserProfile(QQ api) {
return null;
}
示例15
@Override
public UserProfile fetchUserProfile(WechatMp api) {
return null;
}
示例16
@Override
public UserProfile fetchUserProfile(Alipay api) {
return null;
}
示例17
@Override
public UserProfile fetchUserProfile(GitHub api) {
return null;
}
示例18
@Override
public UserProfile fetchUserProfile(Gitee api) {
return null;
}
示例19
@Override
public UserProfile fetchUserProfile(Weixin api) {
return null;
}
示例20
@Override
public UserProfile fetchUserProfile(QQ api) {
return null;
}
示例21
@Override
public UserProfile fetchUserProfile(WeiXin api) {
return null;
}
示例22
public static CalendarUser createCalendarUserFromProvider(Connection<?> connection){
// TODO: There is a defect with Facebook:
// Connection<Facebook> connection = facebookConnectionFactory.createConnection(accessGrant);
// Facebook facebook = connection.getApi();
// String [] fields = { "id", "email", "first_name", "last_name" };
// User userProfile = facebook.fetchObject("me", User.class, fields);
// Object api = connection.getApi();
// if(connection instanceof FacebookTemplate){
// System.out.println("HERE");
// }
/*
<form name='facebookSocialloginForm'
action="<c:url value='/auth/facebook' />" method='POST'>
<input type="hidden" name="scope"
value="public_profile,email,user_about_me,user_birthday,user_likes"/>
...
</form>
*/
// FIXME: Does not work with Facebook:
UserProfile profile = connection.fetchUserProfile();
CalendarUser user = new CalendarUser();
if(profile.getEmail() != null){
user.setEmail(profile.getEmail());
}
else if(profile.getUsername() != null){
user.setEmail(profile.getUsername());
}
else {
user.setEmail(connection.getDisplayName());
}
user.setFirstName(profile.getFirstName());
user.setLastName(profile.getLastName());
user.setPassword(randomAlphabetic(32));
return user;
}
示例23
/**
* 和上面的方法类似
* @param qq
* @return
*/
@Override
public UserProfile fetchUserProfile(QQ qq) {
return null;
}
示例24
/**
* Fetch user profile user profile.
*
* @param api the api
*
* @return the user profile
*/
@Override
public UserProfile fetchUserProfile(QQ api) {
return null;
}
示例25
/**
* Fetch user profile user profile.
*
* @param api the api
*
* @return user profile
*/
@Override
public UserProfile fetchUserProfile(Weixin api) {
return null;
}
示例26
/**
* Creates a new UserAccount with user social network account Connection Data and UserProfile.
* Default has ROLE_USER role.
*
* @param data
* @param profile
* @return
*/
UserAccount createUserAccount(ConnectionData data, UserProfile profile);