gpt4 book ai didi

java - 将 Facebook 身份验证集成到 FacebookApp 中的 Spring Security

转载 作者:搜寻专家 更新时间:2023-11-01 01:11:25 24 4
gpt4 key购买 nike

我有一个使用用户名/密码 SpringSecurity 配置的 Web 应用程序。现在我想把它移植到一个简单的 Facebook 应用程序中。出于某种原因,我想通过使用返回的 facebook 访问 token 进行身份验证,并保留用户名-密码验证程序。

详细来说,我会检查用户 facebook 访问 token 以进行身份​​验证,由以下人员返回:

https://graph.facebook.com/oauth/access_token?client_id=[my_api_key]&redirect_uri=[my_redirect_uri]&client_secret=[my_api_secret]&code=[code]

用户不需要提供任何用户名/密码,因为他们已经使用 facebook 登录。但我想保留(用户名/密码)spring 安全配置,以便用户可以登录我原来的网站。

SpringSecurity是否支持这种认证方式?如果答案是肯定的,我想知道如何做到这一点?我是否需要编写自定义身份验证提供程序来执行此操作?

更新:最后,我们自定义了SpringSecurity的认证方式,使其通过扩展UsernamePasswordAuthenticationFilter(声明为)接受access_token作为认证参数表单登录过滤器)

最佳答案

还有另一个来自 Spring 的项目:Spring Social这非常有用。

它支持多种社交网络。我成功地使用它对 Facebook 进行了身份验证。然后我编写了一个小函数来将 Facebook 用户登录到我的 Spring Security 上下文中:

protected void authenticate(UserDTO user){
SecurityContextHolder.getContext().getAuthentication();
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());
token.setDetails(new WebAuthenticationDetails(getRequest()));
Authentication authentication = authenticationManager.authenticate(token);
SecurityContextHolder.getContext().setAuthentication(authentication);
}

UserDTO 需要有用户名和(生成的)密码属性,并且需要保存在数据库中,以便您的 user-service(来自 Spring security)可以检索它。

关于java - 将 Facebook 身份验证集成到 FacebookApp 中的 Spring Security,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6408931/

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