質問

Using Spring Social and Spring MVC, I tried to use the RedirectView when connecting to social provider and must be missing something and does not manage to get a successfull connection process to LinkedIn.

Here are the 2 controller methods : the first one acts as expected : based on a service provider choice from the user, it detects that it is not connected to Linked in thru the sp.isconnected() call and when not redirect to a view that is controlled by the 2nd method to initiate the linked in connection.

The second method setsup the appropriate right parameters and redirect to appropriate spring social connection URL (URIs.SPRINGLISIGNIN = "/connect/linkedin")but the Spring Social ConnectController would expect a POST method at this stage and I do not manage to figure out how to pass it thru the RedirectView. I was expecting the initial POST method from the first controller method would be carried out to the second one but it is not the case. In debug mode in the lisgnin method, request.getMethod() shows a GET value.

   @RequestMapping(value = Uris.SPCHOICE, method = RequestMethod.POST)
public RedirectView Spchoice (@RequestParam("sp") String sp, Model model) {

    ServiceProviders spasenum = ServiceProviders.valueOf(sp);
    RedirectView toReturn;
    SPConnectionRetriever spResolver=null;
    switch (spasenum) {
    case FACEBOOK :
        spResolver = FBConnectionRetriever;   
        break;
    case LINKEDIN :
        spResolver = LIConnectionRetriever;     
        break;
    }
    SecurityContext.setCurrentSpResolver(spResolver);
    if (spResolver.isconnected())
        toReturn  = new RedirectView(Uris.MAIN, true);
    else
        toReturn = new RedirectView(spResolver.getConnectUrl(), true);

    return toReturn;
}

Here is the 2nd method :

@RequestMapping(value = Uris.SIGNINLI)
public RedirectView liSignin(HttpServletRequest request, HttpServletResponse response) {
    request.setAttribute("scope", "r_fullprofile,r_network");
    return new RedirectView(Uris.SPRINGLISIGNIN, true);
役に立ちましたか?

解決

As per Roman C comment, no way to achieve what I wanted to do without coming back to the browser so that it can issue a POST request. So I have achieved what I wanted to do by rearchitecturing the code and focusing on the JSP.

    <h3>
    Showing available Service Providers :
    <c:out value="${serviceProvider}" />
    </h3>
    <ul>
    <c:forEach items="${serviceProviders}" var="sp">
        <li><c:out value="${sp.name}" /> <c:if test="${sp.connected}"> : Connected with Permissions : <c:out
                    value="${sp.permissions}" />
                <form action="<c:url value="<%=Uris.DISCONNECT%>" />" method="POST">
                    <button type="submit">Disconnect</button>
                    <input type="hidden" name="sp" value="${sp.name}" />
                </form>
            </c:if> 
            <c:if test="${!sp.connected}"> : Disconnected  
            <form action="<c:url value="${sp.URL }" />" method="POST">
                    <button type="submit">Connect</button>
                    <input type="hidden" name="scope" value="${sp.permissions}" />
                </form></c:if></li>
    </c:forEach>
</ul>

The controller then takes care of populating the service provider information depending of its connection status. If there is no connection to the service provider, the 2nd <c:if> clause will build a connection button taking care of doing the post method with the right scope/permissions.

Here is the controller code (URIs.WORK points to the jsp shown above):

   @RequestMapping(value = Uris.MAIN)
    public String home(Model model) {
    List <SPInfo> SPStatusList = new ArrayList<SPInfo>();
    for (ServiceProviders sp : ServiceProviders.values()) {
        SPConnectionRetriever spAccess = SPBank.getSPConnection(sp);
        SPStatusList.add(new SPInfo(sp.toString(), spAccess.isconnected(), spAccess.getPermissions(),spAccess.getConnectUrl()));
    }

    model.addAttribute("nom", SecurityContext.getCurrentUser().getId());
       model.addAttribute("serviceProviders", SPStatusList);

    return Uris.WORK;
}

with SPInfo class looking as follows :

   public class SPInfo {
    private String name;
    private boolean connected;
    private String permissions;
    private String URL;

    public SPInfo(String name, boolean isConnected, String permissions, String URL) {
        super();
        this.name = name;
        this.connected = isConnected;
        this.permissions = permissions;
        this.URL = URL;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public boolean isConnected() {
        return connected;
    }
    public void setConnected(boolean isConnected) {
        this.connected = isConnected;
    }
    public String getPermissions() {
        return permissions;
    }
   public void setPermissions(String permissions) {
        this.permissions = permissions;
    }
    public String getURL() {
        return URL;
    }
    public void setURL(String uRL) {
        URL = uRL;
    }   
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top