您能帮助检查为什么doFilter未被调用
首先,我首先创建webFilter并实现必要的方法,例如doFilter()和doInit()..
@WebFilter("/app/*")
public class NoCacheFilter implements Filter {
private FilterConfig filterConfig;
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
System.out.print("inDoFilter");
if (!request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
response.setDateHeader("Expires", 0); // Proxies.
}
HttpSession session = request.getSession(false);
String loginURI = request.getContextPath() + "/LoginPage.xhtml";
if (request.getRequestURI().indexOf(loginURI) >= 0
|| request.getRequestURI().indexOf("/Login") >= 0) {
chain.doFilter(request, response);
System.out.print("in2");
} else if (session == null || session.getAttribute("login") == null) {
System.out.print("in");
response.sendRedirect(loginURI);
} else {
System.out.print("out");
chain.doFilter(request, response);
}
}
}
web.xml
<web-app version="3.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-app_3_0.xsd">
<filter>
<filter-name>NoCacheFilter</filter-name>
<filter-class>src.NoCacheFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>NoCacheFilter</filter-name>
<url-pattern>/app/*</url-pattern>
</filter-mapping>
</web-app>
Login.java 此类表示负责验证用户在基础中是否存在的loginbean
public boolean checkuser(){
//UserBean bean=new UserBean();
//preparing some objects for connection
Statement stmt = null;
Sql sql = new Sql("localhost", "XE", "HR", "HR");
String query = "select * from users where login='"
+ login
+ "' AND password='"
+ password
+ "'";
System.out.println(query);
try{
sql.Open_Connexion();
Statement st = sql.Get_Connection().createStatement();
ResultSet rs = st.executeQuery(query);
boolean more = rs.next();
// if user does not exist set the isValid variable to false
if (!more)
{
System.out.println("Sorry, you are not a registered user! Please sign up first");
FacesContext.getCurrentInstance().addMessage("myform1:loginBtn", new FacesMessage("login ou mot de passe incorrect"));
setValid(false);
}
//if user exists set the isValid variable to true
else if (more)
{
String firstName = rs.getString("login");
//String lastName = rs.getString("LastName");
FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("login", firstName);
System.out.println("Welcome " + firstName);
setLogin(login);
//setLastName(lastName);
setValid(true);
sql.Fermer_Cnn();
return true;
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
即使我隐含了过滤器并在未调用的正确类路径中使用了过滤器,我还是尝试使用日志来查看输出是否对应,但System.out.println()仍然没有显示任何内容,因此我知道该方法不调用doFilter()。
最后一个方法代表logout()
public boolean logout() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
return true;
}
我们的目标是删除后退按钮,换句话说,在用户被分离后,他无法通过按浏览器的后退按钮返回到最后一页。 重定向由faces-config使用。