Filtre login ne fonctionne pas correctement
bonjour,
J'ai implémenté un filtre pour sécuriser mon application web (jsf).lorsque je click run sur mon fichier login.xhtml ça fonctionne à merveille et ça me permet de rejoindre mon application.mais par contre si je lance directement une autre page *.html, le système fait une redirection vers la page login.xhtml mais la page n'est pas affiché correctement( toute la partie primefaces et jsf ne s'affiche pas).je n'arrive pas à determiner l'erreur puisque j'ai pas d'erreurs dans mes logs apache tomcat
voici mes fichiers:
web.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- ############################################# -->
<!-- # File upload # -->
<!-- ############################################# -->
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>
org.primefaces.webapp.filter.FileUploadFilter
</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<!-- Optional
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/var/bcom/temp</param-value>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>login.xhtml</welcome-file>
</welcome-file-list>
</web-app> |
login.xhtml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="Reflect Template" />
<title> Login Page</title>
<script type='text/javascript' src='js/jquery.js'></script>
<script type='text/javascript' src='js/jquery-ui.js'></script>
<script type='text/javascript' src='js/jquery.wysiwyg.js'></script>
<script type='text/javascript' src='js/custom.js'></script>
</h:head>
<h:body class="nobackground">
<!-- <div id="login">-->
<center>
<h1 class="logo">
<a href="">le texte fonctionne masi pas le primefaces et jsf</a>
</h1>
</center>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<center>
<h:form id="loginForm" >
<p:growl id="msg" showDetail="true" life="3000" />
<p:panel header="Login" style="width: 360px;alignment-adjust: true ">
<h:panelGrid id="loginPanel" columns="2" style="border-color: 0xffgg; margin: 200,200,50,100">
<h:outputText value="Username" />
<p:inputText id="username" value="#{loginBean.uname}" ></p:inputText>
<!-- <p:spacer></p:spacer> -->
<p:message for="username" ></p:message>
<h:outputText value="Password" />
<p:password id="password" value="#{loginBean.password}" feedback="false"></p:password>
<!-- <p:spacer></p:spacer> -->
<p:message for="password"></p:message>
<!-- <p:spacer></p:spacer> -->
<p:commandButton action="#{loginBean.login}" value="Login" update="loginForm" ajax="true"></p:commandButton>
</h:panelGrid>
</p:panel>
</h:form>
</center>
<!-- </div> -->
</h:body>
</html> |
filter.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebFilter(filterName = "AuthFilter", urlPatterns = {"*.xhtml"})
public class AuthFilter implements Filter {
public AuthFilter() {
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
try {
// check whether session variable is set
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession ses = req.getSession(false);
// allow user to proccede if url is login.xhtml or user logged in or user is accessing any page in //public folder
String reqURI = req.getRequestURI();
if ( reqURI.indexOf("/login.xhtml") >= 0 || (ses != null && ses.getAttribute("username") != null)
|| reqURI.indexOf("/public/") >= 0 || reqURI.contains("javax.faces.resource") )
chain.doFilter(request, response);
else // user didn't log in but asking for a page that is not allowed so take user to login page
res.sendRedirect(req.getContextPath() + "/login.xhtml"); // Anonymous user. Redirect to login page
}
catch(Throwable t) {
System.out.println( t.getMessage());
}
} //doFilter
@Override
public void destroy() {
}
} |
faces-config
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
<navigation-rule>
<from-view-id>/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>home</from-outcome>
<to-view-id>/home.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config> |
je travaille sous netbeans 7.3, jdk 1.6 et tomcat 7
merci d'avance