我在Netbeans 7.4中創建了一個java web-app MySQL作爲數據庫。我已經使用過濾器來控制未經授權的訪問,但它無法正常工作。 我有我的index.jsp(作爲登錄頁面),home.jsp和Add_Details.jsp文件。我希望當會話爲空或用戶嘗試訪問其他頁面(index.jsp除外)時,它們將重定向到index.jsp。但它不起作用。這裏是我的代碼: -Servlet過濾器無法正常工作
AuthenticationFilter.java
package Filters;
//all mandatory files are imported.
public class AuthenticationFilter implements Filter {
private ServletContext context;
@Override
public void init(FilterConfig fConfig) throws ServletException {
this.context = fConfig.getServletContext();
this.context.log("AuthenticationFilter initialized");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
String uri = req.getRequestURI();
this.context.log("Requested Resource::" + uri);
HttpSession session = req.getSession(false);
if (session == null && !(uri.endsWith("index.jsp") || !uri.endsWith("Bucket/") || !uri.endsWith("LoginServlet")))
{
this.context.log("Unauthorized access request");
res.sendRedirect("index.jsp");
} else {
// pass the request along the filter chain
chain.doFilter(request, response);
}
}
@Override
public void destroy() {
// close any resources here
}
}
LogingRequestFilter.java
public class LoggingRequestFilter implements Filter {
private ServletContext context;
@Override
public void init(FilterConfig fConfig) throws ServletException {
this.context = fConfig.getServletContext();
this.context.log("RequestLoggingFilter initialized");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
Enumeration<String> params = req.getParameterNames();
while (params.hasMoreElements()) {
String name = params.nextElement();
String value = request.getParameter(name);
this.context.log(req.getRemoteAddr() + "::Request Params::{" + name
+ "=" + value + "}");
}
Cookie[] cookies = req.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
this.context.log(req.getRemoteAddr() + "::Cookie::{"
+ cookie.getName() + "," + cookie.getValue() + "}");
}
}
// pass the request along the filter chain
chain.doFilter(request, response);
}
@Override
public void destroy() {
// we can close resources here
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>LoggingRequestFilter</filter-name>
<filter-class>com.org.king.Filters.LoggingRequestFilter</filter-class>
</filter>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.org.king.Filters.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggingRequestFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>