I am using IceFaces 1.8.2. I want to create a Filter
so that people cannot access unauthorized URLs of my application.
I attempted to created a Filter
like this:
public class AuthenticationFilter implements Filter {
private FilterConfig config;
public void init(FilterConfig filterConfig) throws ServletException {
this.config=filterConfig;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse resp=(HttpServletResponse)response;
//HttpSession session=req.getSession(true);
String pageRequested=req.getRequestURL().toString();
System.out.println("Page Requested="+pageRequested);
HttpSession session = req.getSession(false);
if (!session.isNew()) {
if(ManageCustomerMbeans.AUTH_KEY!=null){
System.out.println(":::::::::::::::::::::::::Login Authenticate");
chain.doFilter(req, resp);
}
}
else {
System.out.println("::::::::::::::::::::::::::Not Authenticate");
//Session has expired - redirect to login.jsp
}
}
public void destroy() {
}
}
But the result is that the restricted page still shows if I type in its URL. If I block the other page like this:
if (!(pageRequested.contains("Login.jsp")&&session.getAttribute(ManageCustomerMbeans.AUTH_KEY)==null)) {
System.out.println("Filter Error!!!");
resp.sendRedirect("./error");
} else {
chain.doFilter(request, response);
}
nothing happend too.
How do I properly create a Filter
for an IceFaces based web application?
************UPDATE**************
After correction from Mr. BalusC, i do many correction in my Filter
class plus a little tweak because iam using icefaces 1.8.2..this is my correction and it works:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse resp=(HttpServletResponse)response;
HttpSession session = req.getSession(false);
String pageRequested=req.getRequestURL().toString();
String resources="resources/";
String xmlhttp="xmlhttp/";
String kaptcha="kaptcha.jpg";
String block="block/";
if(pageRequested.contains("login")||pageRequested.contains(resources)
||pageRequested.contains(block)||pageRequested.contains(xmlhttp)
||pageRequested.contains(kaptcha)||pageRequested.contains("error")
||pageRequested.contains("logout")||pageRequested.contains("lsuccess")){
chain.doFilter(req, resp);
}else{
if (session != null && session.getAttribute(ManageUsersMBeans.AUTH_KEY) != null) {
chain.doFilter(req, resp);
} else {
resp.sendRedirect(req.getContextPath() + "/error");
}
}
}
and in web.xml:
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.do.tools.util.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Thanks