Facebook graph api 将Spring安全性与Facebook登录集成

Facebook graph api 将Spring安全性与Facebook登录集成,facebook-graph-api,spring-mvc,spring-security,spring-social,spring-social-facebook,Facebook Graph Api,Spring Mvc,Spring Security,Spring Social,Spring Social Facebook,我在项目中使用Spring MVC和Spring安全性。。这将使用我自己的用户数据进行身份验证。但现在我正在尝试与Facebook整合。我在Facebook上创建了一个应用程序,这意味着我获得了客户ID和客户机密。。我也读了SO和一些文件中的一些问题,但仍然卡住了 我创建控制器以使用Facebook登录: import java.util.List; import org.springframework.stereotype.Controller; import org.springframe

我在项目中使用Spring MVC和Spring安全性。。这将使用我自己的用户数据进行身份验证。但现在我正在尝试与Facebook整合。我在Facebook上创建了一个应用程序,这意味着我获得了客户ID和客户机密。。我也读了SO和一些文件中的一些问题,但仍然卡住了

我创建控制器以使用Facebook登录:

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/login/")
public class LoginController {

    String fb_app_id="my_client_id";
    String redirect_url="localhost:8888";
    String state="public_profile,email,user_friends";
    String key="my_client_secret";

    @RequestMapping("/facebook")
    public String login( ModelMap model) {

        String url="https://www.facebook.com/dialog/oauth/?"
                   + "client_id=" + fb_app_id
                   + "&redirect_uri=" + redirect_url
                   + "&scope=email,publish_stream,user_about_me,friends_about_me"
                   + "&state=" + key
                   + "&display=page"
                   + "&response_type=code";
        return "redirect:"+url;

    }

}
我认为这是可行的,因为当我尝试连接时,我可以在下面用Javascript显示我的名字:

function testAPI() {
  console.log('Welcome!  Fetching your information.... ');
  FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
    document.getElementById('status').innerHTML = 'Good to see you, ' +
      response.name;
  });
}
但我仍然对如何将它与Spring安全性集成感到困惑。如果有人有任何例子,我将不胜感激

这是我的spring-security.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security
           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <http pattern="/common/*" security="none" />

    <http auto-config="true">
        <intercept-url pattern="/maintenance/*" access="ROLE_USER" />
        <intercept-url pattern="/_ah/*" access="ROLE_USER" />
        <intercept-url pattern="/predict/*" access="ROLE_USER" />

        <form-login login-page='/' default-target-url='/predict/list'
            login-processing-url="/login_check" authentication-failure-url="/index?login_error=2"
            always-use-default-target="true" />
        <logout logout-url="/logout" logout-success-url="/index" />
    <!--    <custom-filter ref="socialAuthenticationFilter" before="PRE_AUTH_FILTER" /> -->
    <!--    <custom-filter before="FORM_LOGIN_FILTER"
            ref="facebookAuthenticationFilter" /> -->
    </http>

    <authentication-manager>
        <authentication-provider ref="gaeAuthenticationProvider" />
    <!--    <authentication-provider ref="authenticationProviderFacebook"> 
        </authentication-provider>-->
    </authentication-manager>

    <beans:bean id="gaeAuthenticationProvider"
        class="com.games.predictor.security.AuthenticationProvider" />

</beans:beans>  

我有我自己的类通过JPA对userdata进行身份验证

package com.games.predictor.security;

import java.util.List;

import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import com.games.predictor.UserDAO;


public class AuthenticationProvider extends AbstractUserDetailsAuthenticationProvider
{
    //private SecurityDao securityDao; 

    @Override
    protected UserDetails retrieveUser(String username,
            UsernamePasswordAuthenticationToken authentication)
            throws AuthenticationException
    {
        final String password = authentication.getCredentials().toString();
        System.out.println(username+"===="+password);
        //This line for validating user with database
        boolean isValidUser = UserDAO.INSTANCE.isValidUser(username, password);
        if (isValidUser)
        {
            final List<SimpleGrantedAuthority> authorities = UserDAO.INSTANCE.getAuthoritiesByUser(username);
            //User u=new User(username,password,);
            return new User(username, password, true, true, true, true, authorities);
        }
        else
        {
            authentication.setAuthenticated(false);
            throw new BadCredentialsException("Username/Password does not match for " 
                + authentication.getPrincipal());
        }

    }

    @Override
    protected void additionalAuthenticationChecks(UserDetails arg0,
            UsernamePasswordAuthenticationToken arg1)
            throws AuthenticationException {
        // TODO Auto-generated method stub

    }
}
package com.games.predictor.security;
导入java.util.List;
导入org.springframework.security.authentication.BadCredentialsException;
导入org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
导入org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
导入org.springframework.security.core.AuthenticationException;
导入org.springframework.security.core.userdetails.userdetails;
导入org.springframework.security.core.userdetails.User;
导入org.springframework.security.core.authority.SimpleGrantedAuthority;
导入com.games.predictor.UserDAO;
公共类AuthenticationProvider扩展了AbstractUserDetailsAuthenticationProvider
{
//私人证券;
@凌驾
受保护的UserDetails retrieveUser(字符串用户名,
用户名密码身份验证(令牌身份验证)
抛出AuthenticationException
{
最终字符串密码=authentication.getCredentials().toString();
System.out.println(用户名+“=”+密码);
//此行用于使用数据库验证用户
布尔值isValidUser=UserDAO.INSTANCE.isValidUser(用户名、密码);
if(isValidUser)
{
最终列表权限=UserDAO.INSTANCE.getAuthoritiesByUser(用户名);
//用户u=新用户(用户名、密码,);
返回新用户(用户名、密码、true、true、true、true、authorities);
}
其他的
{
authentication.setAuthenticated(false);
抛出新的BadCredentialsException(“用户名/密码与”
+authentication.getPrincipal());
}
}
@凌驾
受保护的void additionalAuthenticationChecks(UserDetails arg0,
用户名密码身份验证令牌arg1)
抛出AuthenticationException{
//TODO自动生成的方法存根
}
}
在过去的几天里,我做了一些研究,但仍然坚持下去


我的项目堆栈:Java、SpringMVC、SpringSecurity、Google应用程序引擎、Google数据存储、JPA。(添加了Spring Social Core和Spring Social Facebook)

丹尼尔,你能详细说明一下你在哪里被卡住了吗。我使用相同的堆栈集成facebook登录。但我没有使用Spring社交网站。我相信这不是必须的

将facebook登录与现有登录集成的步骤

  • 在jsp中使用用于facebook登录的
  • 一旦用户批准你的应用程序。OAuth生成一个auth令牌,使用该令牌使用graph API获取客户端的电子邮件id
  • 使用Ajax将电子邮件id发送到服务器
  • 将发送到服务器端的电子邮件id与数据库中的电子邮件id进行比较
  • 如果电子邮件id相同,请登录用户
  • 身份验证令牌是临时的,所以我发现将其发送到服务器端或保存在db中是毫无意义的。它保存在客户端的本地。fb javascript插件会处理它。这样,您就省去了实施spring社交facebook的麻烦


    我看到您正在使用控制器登录facebook,这不是必需的,相信我,使用fb javascript插件非常简单。

    我认为您不应该手动处理facebook。您可以使用一个非常简单的库:对OAuth(Facebook、Twitter、Google…)、CAS、SAML、OpenID(Connect)和GAE进行身份验证。请参阅演示:

    您真的不应该自己做这些事情。。。你签出了吗:这里是项目链接:如果你想要一个示例实现,请检查这个。嗨,谢谢你的回答。。我的问题是将Facebook登录与Spring安全会话集成时。我看到一些人将它与spring social和spring security结合使用。但我在尝试实现其中一个示例时遇到了问题。Underdog忽略了这一点,您不应该获取在客户端生成的客户端的电子邮件id,因为这很容易被其他电子邮件修改,因此不应该有入口的人可以获得入口。一旦auth user token被返回,您应该将该token发送到服务器端,在服务器端,您需要检查该token是否仍然是生成的token,正如facebook推荐的那样。一旦你确认了这一点,你就可以将这个facebook用户作为你的一个用户登录,如果这个用户存在的话。