Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: How to read URL query parameters using Servlet Filter Liferay 7.2
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
}
}
@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
}
}
Hi,
Try using httpServletRequest.getParameter("code"); instead of ParamUtil.getString(httpServletRequest,"code");
Try using httpServletRequest.getParameter("code"); instead of ParamUtil.getString(httpServletRequest,"code");
Hi Mohammed, I tried this before and got null.
Mohammed Yasin:
Hi,
Try using httpServletRequest.getParameter("code"); instead of ParamUtil.getString(httpServletRequest,"code");
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
Yes Olaf you are indeed correct. I finally figured it out before I saw your post.
. 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);

String currentURL = (String)servletRequest.getAttribute(WebKeys.CURRENT_URL);
String completeURL = (String)servletRequest.getAttribute(WebKeys.CURRENT_COMPLETE_URL);
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.
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™