Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4049010
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:53:25+00:00 2026-05-20T13:53:25+00:00

I’m building a JSP application and I would like to use Facebook Connect as

  • 0

I’m building a JSP application and I would like to use Facebook Connect as one path for user registration and authentication, but I’m not finding much information about how to fetch and parse the FB cookie or even the right flow. I’m trying to merge the information found in the official documentation with a step by step guide like this one but for Java. I am not opposed to relying on libraries like Social Java but understanding the steps would be helpful. Here are the 3 use cases I’m trying to satisfy.

  1. Unauthenticated/unregistered user on my site clicks on “Facebook Connect” button to sign up (capturing email, name and profile ID) and and sign in.
  2. Unauthenticated user clicks on “”Facebook Connect” button to create a valid session on my domain.
  3. Authenticated and registered user without a connected Facebook profile clicks on “Facebook Connect” and associates a Facebook profile ID (and the option to update their email and name) with their existing profile.

For this project I have a Profile class that looks like this (I’m using the excellent Project Lombok with Hibernate)

@Entity
@Data
public class Profile implements java.io.Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  private String username;
  private String password;
  private String displayName;
  private String email;
  private String zipCode;
  private String mobileNumber;
  private String facebookId;

  @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
  private DateTime dateCreated;

  private int status;
  private int level;
}

Status and Level really should be enums, but I’m trying to keep the code tiny for this question.

Disclaimer:I’ve been reading a lot of blogs about how to setup Facebook Connect for user registration and authentication, but they are for the most part based on PHP and older versions of the Facebook API (even some SO questions point to the old wiki in their accepted answers). This seems like a perfect application of the SO community.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:53:26+00:00Added an answer on May 20, 2026 at 1:53 pm

    Here is servlet solution I use. With little tweaking you can meke it work in any JSP with simple username-password form. No javascript needed!!!
    As far as address and phone number go read this:
    http://developers.facebook.com/blog/post/447

    FBAuthServlet

    public class FBAuthServlet extends HttpServlet {
    
    private static final Logger log = Logger.getLogger(FBAuthServlet.class);
    
    private static final long serialVersionUID = 1L;
    
    private UserService userService = //here goes your user service implementation
    
    public FBAuthServlet() {
        super();
    }
    
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
    
        if ("y".equals(request.getParameter("FacebookLogin"))) {
            response.sendRedirect(FaceBookConfig.getLoginRedirectURL());
            return;
        }
        String code = req.getParameter("code");
        if (StringUtil.isNotBlankStr(code)) {
            String authURL = FaceBookConfig.getAuthURL(code);
            URL url = new URL(authURL);
            try {
                String result = readURL(url);
                String accessToken = null;
                Integer expires = null;
                String[] pairs = result.split("&");
                for (String pair : pairs) {
                    String[] kv = pair.split("=");
                    if (kv.length != 2) {
                        res.sendRedirect(FaceBookConfig.MAINURL);
                    } else {
                        if (kv[0].equals("access_token")) {
                            accessToken = kv[1];
                        }
                        if (kv[0].equals("expires")) {
                            expires = Integer.valueOf(kv[1]);
                        }
                    }
                }
    
                if (accessToken != null && expires != null) {
    
                    User user = authFacebookLogin(accessToken, request.getRemoteAddr());
                    if (user != null && user.getFacebookId() != null) {
                        //forward to spring security filter chain
                        res.sendRedirect(FaceBookConfig.MAINURL + "/j_spring_security_check?j_username=" + user.getEmail() + "&FaceBookId=" + user.getFacebookId());
                    } else if (user != null && StringUtil.isNullOrBlank(user.getFacebookId())) {
                        res.sendRedirect(FaceBookConfig.MAINURL + "/login.html?login_error=You are not Registered By Facebook Connect");
    
                    } else {
                        res.sendRedirect(FaceBookConfig.MAINURL);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                res.sendRedirect(FaceBookConfig.MAINURL);
            }
        }
    
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
    
    public void init() throws ServletException {
    }
    
    private String readURL(URL url) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = url.openStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        return new String(baos.toByteArray());
    }
    
    
    private User authFacebookLogin(String accessToken, String ip) {
        try {
            String content = IOUtil.urlToString(new URL("https://graph.facebook.com/me?access_token=" + accessToken));
    
            JSONObject resp = new JSONObject(content);
            String facebookid = resp.getString("id");
            String firstName = resp.getString("first_name");
            String lastName = resp.getString("last_name");
            String email = resp.getString("email");
    
            log.info("Facebook response: " + content);
    
            CreateUserRequestCommand comm = new CreateUserRequestCommand();
    
            comm.setEmail(email);
            comm.setFacebookId(facebookid);
            comm.setFirst(StringAndDateUtils.safeChar(firstName));
            comm.setLast(StringAndDateUtils.safeChar(lastName));
            //if success login
            if (userService.getUserByEmail(email) == null) {
                //if first time login
                User u = userService.createUser(comm, ip);
                return u;
            } else {//if existed
                User existedUser = userService.getUserByEmail(email);
                return existedUser;
    
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    
        return null;
    }
    }
    

    FBEnableServlet

    public class FBEnableServlet extends HttpServlet {
    
    private static final long serialVersionUID = 1L;
    
    private UserService userService = (UserService) ServiceLocator.getContext().getBean("userService");
    
    public FBEnableServlet() {
        super();
    }
    
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
    
        if ("y".equals(request.getParameter("EnableFacebookConnect"))) {
            response.sendRedirect(FaceBookConfig.getEnableRedirectURL());
            return;
        }
        String code = req.getParameter("code");
        if (StringUtil.isNotBlankStr(code)) {
            String authURL = FaceBookConfig.getEnableAuthURL(code);
            URL url = new URL(authURL);
            try {
                String result = readURL(url);
                String accessToken = null;
                Integer expires = null;
                String[] pairs = result.split("&");
                for (String pair : pairs) {
                    String[] kv = pair.split("=");
                    if (kv.length != 2) {
                        res.sendRedirect(FaceBookConfig.MAINURL);
                    } else {
                        if (kv[0].equals("access_token")) {
                            accessToken = kv[1];
                        }
                        if (kv[0].equals("expires")) {
                            expires = Integer.valueOf(kv[1]);
                        }
                    }
                }
    
                if (accessToken != null && expires != null) {
                    User user = authFacebookLogin(accessToken, request.getRemoteAddr());
                    String loginedEmail = "";
                    try {
                        loginedEmail = SecurityContextHolder.getContext().getAuthentication().getName();
                    } catch (Exception ex) {
    
                    }
                    System.out.println("Logined email = " + loginedEmail);
                    System.out.println("Facebook Login email = " + user.getEmail());
                    if (user != null && user.getFacebookId() != null && user.getEmail().equals(loginedEmail)) {
                        userService.setFaceBookid(user.getFacebookId());
                        //forward to spring security filter chain
                        res.sendRedirect(FaceBookConfig.MAINURL + "/j_spring_security_check?j_username=" + user.getEmail() + "&FaceBookId=" + user.getFacebookId());
                    } else {
                        res.sendRedirect(FaceBookConfig.MAINURL + "/secure/myAccount.html?message=Please login Facebook with same Email,you Login with " + user.getEmail());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                res.sendRedirect(FaceBookConfig.MAINURL);
            }
        }
    
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
    
    public void init() throws ServletException {
    }
    
    private String readURL(URL url) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = url.openStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        return new String(baos.toByteArray());
    }
    
    
    private User authFacebookLogin(String accessToken, String ip) {
        try {
            String content = IOUtil.urlToString(new URL("https://graph.facebook.com/me?access_token=" + accessToken));
    
            JSONObject resp = new JSONObject(content);
            String facebookid = resp.getString("id");
            String email = resp.getString("email");
    
            User existedUser = userService.getUserByEmail(email);
            if (existedUser == null) {
                return null;
            } else {
                existedUser.setFacebookId(facebookid);
                return existedUser;
            }
    
    
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
    
        return null;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.