Spring社交Facebook模板总是返回相同的用户

时间:2016-04-27 14:49:08

标签: spring-boot spring-social-facebook

我使用Spring Social 2.0.2.RELEASE为Facebook提供社交登录。我的问题是当我使用FacebookTemplate时,Spring Social总是返回相同的第一个用户。这里的例子是:

```

@Autowired
private Facebook facebook;

@RequestMapping(value = "/facebook/login", method = RequestMethod.GET)
public ModelAndView handleFacebookLogin(HttpServletResponse response) {
//always the same user
User profile = facebook.fetchObject("me", User.class, "id", "name",   "link", "email");


return new ModelAndView("redirect:/dashboard");
}

```

我还有一个Custom ConnectController:

```

@Controller
@RequestMapping("/connect")
public class CustomConnectController extends ConnectController {

    @Autowired
    public CustomConnectController(ConnectionFactoryLocator connectionFactoryLocator,
            ConnectionRepository connectionRepository) {
        super(connectionFactoryLocator, connectionRepository);
    }

    @Override
    protected RedirectView connectionStatusRedirect(String providerId, NativeWebRequest request) {
        return new RedirectView("/facebook/login");
    }


}

```

如果打开两个浏览器并尝试使用不同的用户登录,则始终返回第一个。我目前的解决方案是将整个ConnectController复制到我的应用程序并更改行为。这很可怕,我希望我犯了一个大错。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题并通过创建这个类来解决问题:

@Configuration
public class UniqueSessionUserID extends SocialConfigurerAdapter {

    @Override
    public UserIdSource getUserIdSource() {
    return new UserIdSource() {

        @Override
        public String getUserId() {
        RequestAttributes request = RequestContextHolder.getRequestAttributes();

        String uuid = (String) request.getAttribute("_socialUserUUID", RequestAttributes.SCOPE_SESSION);

        if (uuid == null) {
            uuid = UUID.randomUUID().toString();
        }

        request.setAttribute("_socialUserUUID", uuid, RequestAttributes.SCOPE_SESSION);

        return uuid;
        }
    };
    }
}

这是一个链接,更详细地解释了为什么这是必要的: Spring Social Facebook more than one user