gpt4 book ai didi

java - 如何解决用户 LOGOUT 的 FILTER 查询

转载 作者:行者123 更新时间:2023-12-01 15:11:19 24 4
gpt4 key购买 nike

我有这个用于过滤缓存页面的代码,但在编译时遇到一些问题:

package bean.log.filter;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import javax.servlet.annotation.WebFilter;

public class LoginFilter implements Filter
{
@Override

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
try
{


HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("userHash") == null)
{
response.sendRedirect("/scape/applicationservices/fileshare/vm/login/login.jsp"); // No logged-in user found, so redirect to login page.
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);
}
else
{
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

}

编译后出现以下错误:

D:\programs\MyPackage\bean\log\filter>javac LoginFilter.java
LoginFilter.java:14: bean.log.filter.LoginFilter is not abstract and does not ov
erride abstract method destroy() in javax.servlet.Filter
public class LoginFilter implements Filter
^
1 error

因此,我对代码进行了如下更改,然后进行了编译,但没有得到所需的结果。我所做的更改是:我将 @Overrideimplements Filter 放在注释中,并添加了 public void initpublic void destroy 方法

package bean.log.filter;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import javax.servlet.annotation.WebFilter;

public class LoginFilter // implements Filter
{
//@Override
public void init( )
{
}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{
try
{


HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute("userHash") == null)
{
response.sendRedirect("/timescape/applicationservices/fileshare/vm/login/login.jsp"); // No logged-in user found, so redirect to login page.
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);
}
else
{
chain.doFilter(req, res); // Logged-in user found, so just continue request.
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void destroy( )
{
}

}

如何在 web.xml 中配置过滤器

我的配置如下

<web-app>


<welcome-file-list>
<welcome-file>/WEB-INF/index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>noCacheFilter</filter-name>
<filter-class>bean.log.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>noCacheFilter</filter-name>
<url-pattern>/scape/applicationservices/fileshare/vm/apps/*</url-pattern>
</filter-mapping>



</web-app>

我希望此过滤器限制用户在注销后返回到我的 apps 目录中的先前缓存页面,因此我使用该url-pattern。如何实现这个过滤器的工作。

最佳答案

Java EE 过滤器必须实现您上面提到的接口(interface):javax.servlet.Filter 。您的问题不在于 Filter 本身,而在于您的 Java 使用情况。在这种语言中,当某些非抽象类实现接口(interface)时,它或其父类必须实现所有声明的方法。这意味着,当接口(interface)声明方法 init()doFilter()destroy() 时,您的类必须实现所有这些方法即使实现应该是空的。这意味着您必须结合两种解决方案:

  1. 取消注释实现过滤器
  2. let 未注释的方法 init()destroy()
  3. 如果您使用的是 Java 6 或更高版本,可以取消注释 @Override

之后,您的过滤器应该没问题,至少应该在 Web 容器处理某些与 web.xml 映射中指定的 URL 匹配的页面时执行。

无论如何,我猜想注销 URL 通常不会那么复杂,因此我希望映射 URL 类似于 /logout。正如我已经提到的,过滤器仅在与 URL 匹配的页面上执行。

关于java - 如何解决用户 LOGOUT 的 FILTER 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12312364/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com