Java 使用具有特定url模式的筛选器时的无限循环

Java 使用具有特定url模式的筛选器时的无限循环,java,jsp,servlets,Java,Jsp,Servlets,我的过滤器有一个无限循环。url模式不是通用的。我似乎不明白为什么会这样。这是我的过滤器的映射 <filter> <filter-name>AdminAuthentication</filter-name> <filter-class>my.filters.AdminAuthFilter</filter-class> </filter> <filter-mapping> <filte

我的过滤器有一个无限循环。url模式不是通用的。我似乎不明白为什么会这样。这是我的过滤器的映射

<filter>
    <filter-name>AdminAuthentication</filter-name>
    <filter-class>my.filters.AdminAuthFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>AdminAuthentication</filter-name>
    <url-pattern>/admin/addLocation</url-pattern>
    <url-pattern>/admin/deleteLocation</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>
我的问题是,当我在没有登录的情况下转到admin/addLocation时,我会得到一个无限的重定向,就像这样
http://localhost:8080/PROJ/admin/admin/admin/admin...
否则,当我登录时,它可以正常工作。login.jsp也不在admin文件夹中。请帮忙

您的入口点需要在筛选器之外。你的重定向有问题。由于用户为空,正在与chain.doFilter发生冲突

下面是一个简单的登录过滤器,我使用它来检查用户是否已登录以及是否在定义的url模式内的会话中

过滤器描述符

<filter>
    <filter-name>AdminFilter</filter-name>
    <filter-class>com.AdminLoginFilter</filter-class>
    <description>Admin Login Filter</description>
    <init-param>
        <param-name>Admin_login_form</param-name>
        <param-value>/administration/login</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>AdminFilter</filter-name>
    <url-pattern>/administration/controlpanel/*</url-pattern>
</filter-mapping>

管理员过滤器
com.AdminLoginFilter
管理员登录筛选器
管理员登录表单
/管理/登录
管理员过滤器
/行政/控制小组/*
Servlet过滤器

public class AdminLoginFilter implements Filter {

private FilterConfig filterConfig;
private String loginForm; 

public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    loginForm = this.filterConfig.getInitParameter("Admin_login_form");
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpSession session = httpRequest.getSession();

    ControlPanelUser adminUser = (ControlPanelUser) session.getAttribute(PageConstants.CONTROL_PANEL_USER); 

    if ((adminUser == null || adminUser.getBoId() < 1)) { //Send user to login form
        filterConfig.getServletContext().getRequestDispatcher(loginForm).forward(request, response); 
    } else {// Send user to requested page
        chain.doFilter(request,response); 
    }

}

public void destroy() {
    this.filterConfig = null;
}
}
公共类AdminLoginFilter实现过滤器{
私有过滤器配置过滤器配置;
私有字符串loginForm;
public void init(FilterConfig FilterConfig)抛出ServletException{
this.filterConfig=filterConfig;
loginForm=this.filterConfig.getInitParameter(“Admin\u login\u form”);
}
public void doFilter(ServletRequest请求、ServletResponse响应、FilterChain链)抛出IOException、ServletException{
HttpServletRequest httpRequest=(HttpServletRequest)请求;
HttpSession session=httpRequest.getSession();
ControlPanelUser adminUser=(ControlPanelUser)session.getAttribute(PageConstants.CONTROL\u PANEL\u USER);
如果((adminUser==null | | adminUser.getBoId()<1)){//将用户发送到登录表单
filterConfig.getServletContext().getRequestDispatcher(loginForm).forward(请求,响应);
}else{//将用户发送到请求的页面
链式过滤器(请求、响应);
}
}
公共空间销毁(){
this.filterConfig=null;
}
}
凭证检查

public class CheckUserCredentialsCommand implements Command {
public void execute(CommandContext commandContext) throws Exception {

    ILoginForm loginForm = new LoginForm();
    loginForm.populateFromForm(commandContext);

    List<ValidationMessage> messages = loginForm.validate();

    if(messages != null && messages.size() > 0){
        commandContext.setScopedVariable(PageConstants.LOGIN_MESSAGES, messages, ScopedContext.REQUEST);
    } else {        
        ControlPanelUser customer = ControlPanelUserDAO.selectControlPanelUser(loginForm.getEmailAddress(), loginForm.getPasswrd());
        if(customer != null){
            commandContext.setScopedVariable(PageConstants.CONTROL_PANEL_USER, customer, ScopedContext.SESSION);
        } else {
            commandContext.setScopedVariable(PageConstants.LOGIN_MESSAGES, messages, ScopedContext.REQUEST);
        }
    }
    String referer = commandContext.getRequest().getHeader("referer");
    if(referer != null){
        referer = referer.substring(referer.lastIndexOf("/") + 1, referer.length());
        if("login".equals(referer)){
            commandContext.redirect(commandContext.getServletContext().getContextPath()+"/administration/controlpanel/dashboard");
        } else {
            commandContext.redirect(commandContext.getRequest().getHeader("referer"));
        }
    } else {
        commandContext.redirect(commandContext.getServletContext().getContextPath()+"/administration/controlpanel/dashboard");
    }
}

}
公共类CheckUserCredentialsCommand实现该命令{
public void execute(CommandContext CommandContext)引发异常{
ILoginForm loginForm=新的loginForm();
loginForm.populateFromForm(commandContext);
List messages=loginForm.validate();
if(messages!=null&&messages.size()>0){
commandContext.setScopedVariable(PageConstants.LOGIN_消息、消息、ScopedContext.REQUEST);
}否则{
ControlPanelUser customer=ControlPanelUserDAO.selectControlPanelUser(loginForm.getEmailAddress(),loginForm.getPassword());
如果(客户!=null){
commandContext.setScopedVariable(PageConstants.CONTROL_PANEL_用户、客户、ScopedContext.SESSION);
}否则{
commandContext.setScopedVariable(PageConstants.LOGIN_消息、消息、ScopedContext.REQUEST);
}
}
字符串referer=commandContext.getRequest().getHeader(“referer”);
if(referer!=null){
referer=referer.substring(referer.lastIndexOf(“/”)+1,referer.length());
如果(“登录”。等于(引用)){
重定向(commandContext.getServletContext().getContextPath()+“/administration/controlpanel/dashboard”);
}否则{
重定向(commandContext.getRequest().getHeader(“referer”);
}
}否则{
重定向(commandContext.getServletContext().getContextPath()+“/administration/controlpanel/dashboard”);
}
}
}

我的登录条目是,当我登录到该页面时,它将提交给CheckUserCredentialsCommand,这只是一个简单的servlet。然后,该servlet尝试将页面重定向到过滤器后面的某个页面。在过滤器中,它检查用户,如果用户为空,它将转发回登录页面,如果存在有效用户,它将通过过滤器链,该过滤器链是您从CheckUserCredentials命令重定向的,现在您的ur l看起来像,仪表板页面在过滤器后面,如果没有用户,您将永远无法访问该页面。

您的入口点必须位于筛选器之外。你的重定向有问题。由于用户为空,正在与chain.doFilter发生冲突

下面是一个简单的登录过滤器,我使用它来检查用户是否已登录以及是否在定义的url模式内的会话中

过滤器描述符

<filter>
    <filter-name>AdminFilter</filter-name>
    <filter-class>com.AdminLoginFilter</filter-class>
    <description>Admin Login Filter</description>
    <init-param>
        <param-name>Admin_login_form</param-name>
        <param-value>/administration/login</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>AdminFilter</filter-name>
    <url-pattern>/administration/controlpanel/*</url-pattern>
</filter-mapping>

管理员过滤器
com.AdminLoginFilter
管理员登录筛选器
管理员登录表单
/管理/登录
管理员过滤器
/行政/控制小组/*
Servlet过滤器

public class AdminLoginFilter implements Filter {

private FilterConfig filterConfig;
private String loginForm; 

public void init(FilterConfig filterConfig) throws ServletException {
    this.filterConfig = filterConfig;
    loginForm = this.filterConfig.getInitParameter("Admin_login_form");
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpSession session = httpRequest.getSession();

    ControlPanelUser adminUser = (ControlPanelUser) session.getAttribute(PageConstants.CONTROL_PANEL_USER); 

    if ((adminUser == null || adminUser.getBoId() < 1)) { //Send user to login form
        filterConfig.getServletContext().getRequestDispatcher(loginForm).forward(request, response); 
    } else {// Send user to requested page
        chain.doFilter(request,response); 
    }

}

public void destroy() {
    this.filterConfig = null;
}
}
公共类AdminLoginFilter实现过滤器{
私有过滤器配置过滤器配置;
私有字符串loginForm;
public void init(FilterConfig FilterConfig)抛出ServletException{
this.filterConfig=filterConfig;
loginForm=this.filterConfig.getInitParameter(“Admin\u login\u form”);
}
public void doFilter(ServletRequest请求、ServletResponse响应、FilterChain链)抛出IOException、ServletException{
HttpServletRequest httpRequest=(HttpServletRequest)请求;
HttpSession session=httpRequest.getSession();
ControlPanelUser adminUser=(ControlPanelUser)session.getAttribute(PageConstants.CONTROL\u PANEL\u USER);
如果((adminUser==null | | adminUser.getBoId()<1)){//将用户发送到登录表单
filterConfig.getServletContext().getRequestDispatcher(loginForm).forward(请求,响应);
}else{//将用户发送到请求的页面
链式过滤器(请求、响应);
}
}
公共空间销毁(){
this.filterConfig=null;
}
}
凭证检查

public class CheckUserCredentialsCommand implements Command {
public void execute(CommandContext commandContext) throws Exception {

    ILoginForm loginForm = new LoginForm();
    loginForm.populateFromForm(commandContext);

    List<ValidationMessage> messages = loginForm.validate();

    if(messages != null && messages.size() > 0){
        commandContext.setScopedVariable(PageConstants.LOGIN_MESSAGES, messages, ScopedContext.REQUEST);
    } else {        
        ControlPanelUser customer = ControlPanelUserDAO.selectControlPanelUser(loginForm.getEmailAddress(), loginForm.getPasswrd());
        if(customer != null){
            commandContext.setScopedVariable(PageConstants.CONTROL_PANEL_USER, customer, ScopedContext.SESSION);
        } else {
            commandContext.setScopedVariable(PageConstants.LOGIN_MESSAGES, messages, ScopedContext.REQUEST);
        }
    }
    String referer = commandContext.getRequest().getHeader("referer");
    if(referer != null){
        referer = referer.substring(referer.lastIndexOf("/") + 1, referer.length());
        if("login".equals(referer)){
            commandContext.redirect(commandContext.getServletContext().getContextPath()+"/administration/controlpanel/dashboard");
        } else {
            commandContext.redirect(commandContext.getRequest().getHeader("referer"));
        }
    } else {
        commandContext.redirect(commandContext.getServletContext().getContextPath()+"/administration/controlpanel/dashboard");
    }
}

}
公共类CheckUserCredentialsCommand实现该命令{
public void execute(CommandContext CommandContext)引发异常{
ILoginForm loginForm=新的loginForm();
loginForm.populateFromForm(commandContext);
List messages=loginForm.validate();
if(messages!=null&&messages.size()>0){
commandContext.setScopedVariable(第页