Question

hi i am using jsf2.0, prettyfaces,primesfaces

i have created a filter which monitor user session

@WebFilter(urlPatterns= {"*.xhtml"} , dispatcherTypes = {DispatcherType.REQUEST})
public class Authentication implements Filter {

@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("[Authentication Filter] : init Method");
}
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
PrettyContext context = PrettyContext.getCurrentInstance(request);      
if (!(context.getCurrentMapping().getId().equals("login")) && (session == null || session.getAttribute("username") == null)) {
{
response.sendRedirect(request.getContextPath()+"/login");
}
else {
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
@Override
public void destroy() {}
}

web.xml

<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

pretty-config.xml

<url-mapping id="home"> 
<pattern value="/" /> 
<view-id value="index.xhtml" />
</url-mapping> 

when i use dispatcherTypes = {DispatcherType.REQUEST} i get null pointer exception on context.getCurrentMapping().getId() this statement which is using in if statement see above but when i dispatcherTypes = {DispatcherType.FORWARD} its working fine for me what's happening can any body guide me ? thanks and i want to know different between DispatcherType.REQUEST and DispatcherType.FORWARD , an other Question is when i use FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); for destoring session and if i press BACK button from browser it takes me on previous page why browser shows me privous page?, i want login page on BAck button thanks.

Was it helpful?

Solution

PrettyFaces implements URL rewriting by intercepting incoming requests in a Servlet filter and then forwarding it to the real URL.

The DispatchType of a filter configures for which kind of request the filter should be applied. The default is REQUEST which is the usual case. If you set it to FORWARD it is only applied to requests that are internally forwarded (which is what PrettyFaces does).

In case of REQUEST you are getting the NPE because your filter executes before the PrettyFaces filter and so you cannot access the PrettyContext.

It is absolutely fine for you to use FORWARD in your case. The only thing that you have to remember is that HttpServletRequest.getRequestURI() will return the real URL instead of the pretty one for forwarded requests.

Your back button problem is probably related to caching. If you don't set correct caching heads, the browser will cache the last page and so pressing BACK will simply redisplay the last page without sending a request to the server.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top