Skip to content

Commit

Permalink
IVIS-46: - Update SDK login chapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanPopenko committed Oct 31, 2016
1 parent a021a72 commit 1922684
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
49 changes: 49 additions & 0 deletions docs/sdk/routines/code/LoginController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.imcode.imcms.addon.ivisclient.controllers;

import imcode.services.utils.IvisOAuth2Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;

@Controller
public class LoginController {

@Value("#{'${client-address}' + '${redirect-relate-uri}'}")
private String redirectUri;

private final AuthorizationCodeResourceDetails client;

@Autowired
public LoginController(AuthorizationCodeResourceDetails client) {
this.client = client;
}

@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(ModelAndView view) throws URISyntaxException {
String oAuth2AuthirizationUrl = IvisOAuth2Utils.getOAuth2AuthirizationUrl(client, redirectUri, false);
view.setViewName("redirect:" + oAuth2AuthirizationUrl);
return view;
}

@RequestMapping(value = "${redirect-relate-uri}", method = RequestMethod.GET)
public ModelAndView authorizationClientProcess(ModelAndView view,
HttpServletRequest request,
@RequestParam("code") String code) throws UnsupportedEncodingException {
//send post request and receive token
OAuth2AccessToken accessToken = IvisOAuth2Utils.getAccessToken(client, code, redirectUri);
IvisOAuth2Utils.setAccessToken(request, accessToken);
view.setViewName("start_page_view");//view name of start page
return view;
}

}
29 changes: 25 additions & 4 deletions docs/sdk/routines/login.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,33 @@ To login you need:
#. send POST request with client credentials with code.
#. receive access token.

Talking is a good but let's coding.
Let's see how it looks like.

.. literalinclude:: /sdk/routines/code/LoginController.java
:language: java
:linenos:

To know if user login on JSP you can invoke special tag <ivis:authorized> with optional parameter role.

.. code-block:: jsp
<%@taglib prefix="ivis" uri="ivis.sdk" %>
<ivis:authorized>
Information for authorized persons
</ivis:authorized>
...
<ivis:authorized role="ROLE_ADMIN">
Information for user in admin role
</ivis:authorized>
.. important::

You can use this tag if you have permission to use method getCurrent user.

Prerequisites
-------------

.. code-block:: java



Expand Down

0 comments on commit 1922684

Please sign in to comment.