Table of Contents
Introduction
However, it does have lot of shortcomings too like
- Supporting only single user sign per application
- The Views are predefined so the user flow will be like
facebookConnect.html -> connect/facebookConnected.html and so on
While it is easy to change the initial view there is no clear documentation available which defines how to change the views.
To Replace facebookConnect.html
This bit is easy we just need to copy everything inside the form tag and place it in your login/home page.
All we are doing here is creating a form which posts to "/connect/facebook"
When we submit the form the code goes to ConnectController class and does the OAuth dance and routes back to "/connect/facebookConnected.html"
To Replace connect/facebookConnected.html
AS mentioned in the previous step the Connect controller class does the OAuth auth behind the scenes and redirects back to "/connect/facebookConnected.html"
org.springframework.social.connect.web.ConnectController Class is an UI controller for managing the account-to-service-provider connection flow.
In this class, all the GET and Post methods have defined to handle the authentication flow
- GET /connect/{providerId} – Get a web page showing connection status to {providerId}.
- POST /connect/{providerId} – Initiate an connection with {providerId}.
- GET /connect/{providerId}?oauth_verifier||code – Receive {providerId} authorization callback and establish the connection.
- DELETE /connect/{providerId} – Disconnect from {providerId}.
Among all the methods you should see a connectedView method which returns the "/connect/facebookConnected.html"
1 2 3 4 5 | protected String connectedView(String providerId) { return getViewPath() + providerId + "Connected"; } |
All we need to do is to override this method
Create a CustomController
Create a class and call it CustomController make sure that it extends org.springframework.social.connect.web.ConnectController
Add the @Controller annotation at class level
Add the Constructor
1 2 3 4 5 6 | public CustomController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) { super(connectionFactoryLocator, connectionRepository); } |
Override the connectedView method
- If you want web flow to be redirected to an another REST URL/Controller use something like
1 2 3 4 5 6 | @Override protected String connectedView(String providerId) { return "redirect:/facebook"; } |
The above code will redirect to another REST controller where say data can be saved to DB and things like authentication can be done, something like below
1 2 3 4 5 6 7 | @RequestMapping(value = "/facebook", method = RequestMethod.GET) public String loginToFacebook(Model model) { getFacebookUserData(model, new UserForm()); return "facebookPage" } |
- If you want to show custom login/home page use something like
1 2 3 4 5 6 | @Override protected String connectedView(String providerId) { return "home"; } |
Complete Custom controller class looks like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.nevado.controller; import org.springframework.social.connect.ConnectionFactoryLocator; import org.springframework.social.connect.ConnectionRepository; import org.springframework.social.connect.web.ConnectController; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/connect") public class CustomController extends ConnectController { public CustomController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) { super(connectionFactoryLocator, connectionRepository); } @Override protected String connectedView(String providerId) { return "redirect:/facebook"; } } |
Follow the video for reference