Message Boards

How to read URL query parameters using Servlet Filter Liferay 7.2

Kevin Matthews, modified 3 Years ago.

How to read URL query parameters using Servlet Filter Liferay 7.2

Expert Posts: 253 Join Date: 1/25/16 Recent Posts
Hello, I would like to know how can I ready query parameters from URL using liferay filter?I am working on integrating my liferay application to Authorization server.  I created a filter and redirect to Auth server. I  was presented the Auth server login page for username and password. Once my log username and password successful an redirect of the URL was displayed with the authorization code as query parameter http://localhost:8081/?code=xF0ea_MIwT4cQk0lFThTYQtRfopJAW78V_ZxNz_q. I would like to get this authorization code from the URL so I can make POST call to Authorization server to receive a JSON token and then once I get the token display the dashboard.  I tried to use liferay portal utility class ParmUtil but that didn't  work. Here is my code snippet below:

@Component(
    immediate = true,
    property = {
               "servlet-context-name=",
               "servlet-filter-name=Oauth2 Login Filter",
               "url-pattern=/c/portal/login"
    },
    service = Filter.class
)
public class AuthenticationSSOFilter implements Filter {
    
    private static final Log _log = LogFactoryUtil.getLog(AuthenticationSSOFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
        
    }    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
    
        
    try {    
          HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
          HttpServletResponse response = (HttpServletResponse) servletResponse;

 Response clientResponse =   ClientBuilder.newClient().target(OauthBaseDEVUrl)
 if (clientResponse.getStatus() == 302){
           response.sendRedirect(OauthBaseDEVUrl);
 String actuallRedirectValue = ParamUtil.getString(httpServletRequest,"code");
             _log.info("Actual Redirect Param Value:" + actuallRedirectValue);
     }
        
    } catch (Exception e) {
          _log.error("Error in LoginFilter: " + e.getMessage(), e);
          } finally {
             filterChain.doFilter(servletRequest, servletResponse);
          }
}    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }
    
}
thumbnail
Mohammed Yasin, modified 3 Years ago.

RE: How to read URL query parameters using Servlet Filter Liferay 7.2

Liferay Master Posts: 591 Join Date: 8/8/14 Recent Posts
Hi,
Try using  httpServletRequest.getParameter("code");  instead of  ParamUtil.getString(httpServletRequest,"code");
Kevin Matthews, modified 3 Years ago.

RE: How to read URL query parameters using Servlet Filter Liferay 7.2

Expert Posts: 253 Join Date: 1/25/16 Recent Posts
Hi Mohammed, I tried this before and got null.
Mohammed Yasin:

Hi,
Try using  httpServletRequest.getParameter("code");  instead of  ParamUtil.getString(httpServletRequest,"code");
thumbnail
Olaf Kock, modified 3 Years ago.

RE: How to read URL query parameters using Servlet Filter Liferay 7.2

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts
Kevin Matthews:

Hi Mohammed, I tried this before and got null.


Notice the difference between the URL you post and the declaration in the servlet filter:

URL: http://localhost:8081/?code=xF0ea_MI...

property = { ...
"url-pattern=/c/portal/login"

Maybe that's the key? With that URL, you wouldn't get any code parameter in the servlet filter that reacts only to /c/portal/login
Kevin Matthews, modified 3 Years ago.

RE: How to read URL query parameters using Servlet Filter Liferay 7.2

Expert Posts: 253 Join Date: 1/25/16 Recent Posts
Yes Olaf you are indeed correct. I finally figured it out before I saw your post. emoticon.  Just to see where I was in the flow I use the floowing liferay Web Keys. I print out using the 
String currentURL = (String)servletRequest.getAttribute(WebKeys.CURRENT_URL);
String completeURL = (String)servletRequest.getAttribute(WebKeys.CURRENT_COMPLETE_URL);
Kevin Matthews, modified 3 Years ago.

RE: How to read URL query parameters using Servlet Filter Liferay 7.2

Expert Posts: 253 Join Date: 1/25/16 Recent Posts
Ok I finally figured out the problem. I had to change my redirect url to localhost:8080/c/portal/login  it seem servlet looks at the current path url-pattern=/c/portal/login.