---
title: "session 超时,sendredirct 问题 BeanNameUrlHandlerMapping"
author: "asdcls"
date: "2009-12-30T23:55:26Z"
tags: []
categories: ["企业架构"]
slug: "session-sendredirct-beannameurlhandlermapping"
draft: false
lastmod: "2009-12-31T00:03:03Z"
id: 560135
url: "https://asdcls.iteye.com/blog/560135"
---

出问题处：
response.sendRedirect(request.getContextPath()+loginUrl); 
在session 超时时　back/mgrLogin.do 
总是跳转到mgrLogin.do 
　４０４ 
不知道是什么原因 


对此：myworkfirst同学上次讨论 写道 
  看下后台日志，就知道了，路径不正确 
会话超时，怎么不用 过滤器呀? 

在session不超时的时候　 

response.sendRedirect(request.getContextPath()+loginUrl); 
可以正确的跳转到，配置的页面/back/mgrLogin.do 
<property name="loginUrl">   
      <value>/back/mgrLogin.do</value>   
     </property>  

但当session超时的时候，则莫名其妙的跳转到 mgrLogin.do 
不过后台没有报错。估计是session超时，对跳转有影响，我的项目中还用到了，urlrewrite. 再次到权限检查器的时候，已经是/mgrLogin.do 
解决方法么： 
到也没有什么不好的，将登录的路径改为根路径解决了 



```xml
<bean id="backAuthHandler" class="com.emar.framework.auth.BackAuthHandler"></bean> 

<bean id="backAuthorizeURLInterceptor" class="com.emar.framework.auth.BackAuthorizeURLInterceptor"> 
     <property name="authHandle" ref="backAuthHandler"></property> 
     <property name="loginUrl"> 
     <value>/back/mgrLogin.do</value> 
     </property> 
     <property name="checkAccessUrls"> 
            <list> 
                <value>/back/*</value> 
            </list> 
        </property> 
        <property name="noCheckAccessUrls"> 
            <list> 
           <value>/back/mgrLogin.do</value> 
            </list> 
        </property> 
    </bean> 
    <bean name="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" autowire="no"> 

<property name="interceptors"> 
            <list> 
            <ref local="backAuthorizeURLInterceptor"/> 
            </list> 
        </property> 
     
    </bean> 

```


Java代码 

```java
public class BackAuthorizeURLInterceptor extends HandlerInterceptorAdapter {   
private UrlPathHelper urlPathHelper = new UrlPathHelper();    
private String[] checkAccessUrls;   
private String[] noCheckAccessUrls;//不需要保护的url资源   
private PathMatcher pathMatcher = new AntPathMatcher();   
    
private String loginUrl;   
private AuthHandle authHandle;//权限检查处理器   
public void setPathMatcher(PathMatcher pathMatcher) {   
  Assert.notNull(pathMatcher, "PathMatcher must not be null");   
  this.pathMatcher = pathMatcher;   
}   
    
     
    
/**  
  * @param loginUrl the loginUrl to set  
  */  
public void setLoginUrl(String loginUrl) {   
     Assert.notNull(loginUrl, "loginUrl must not be null");   
  this.loginUrl = loginUrl;   
}   
  
    
  
/**  
  * @param checkAccessUrls the checkAccessUrls to set  
  */  
public void setCheckAccessUrls(String[] checkAccessUrls) {   
     Assert.notNull(checkAccessUrls, "checkAccessUrls must not be null");   
  this.checkAccessUrls = checkAccessUrls;   
}   
  
    
  
/**  
  * @param noCheckAccessUrls the noCheckAccessUrls to set  
  */  
public void setNoCheckAccessUrls(String[] noCheckAccessUrls) {   
  this.noCheckAccessUrls = noCheckAccessUrls;   
}   
  
/**  
  * @param authHandle the authHandle to set  
  */  
public void setAuthHandle(AuthHandle authHandle) {   
  this.authHandle = authHandle;   
}   
  
/**  
  *   
  */  
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)   
     throws Exception {   
  String url = urlPathHelper.getLookupPathForRequest(request);   
  if (!isProtected(url) ) {   
   return true;//所请求的资源不需要保护.   
  }else if(this.pathMatcher.match(url, loginUrl+"*")){   
   return true;   
  }   
     
  boolean b = authHandle.checkAuth(request, response);   
     
  if(!b){   
  
     response.sendRedirect(request.getContextPath()+loginUrl);      
  }   
  return b;   
}   
    
private boolean isProtected(String urlPath) {   
  if(noCheckAccessUrls != null){   
   for (int i = 0; i < this.noCheckAccessUrls.length; i++) {   
    String registeredPath = noCheckAccessUrls[i];   
    if (registeredPath == null) {   
     throw new IllegalArgumentException("Entry number " + i + " in allowAccessUrls array is null");   
    } else {   
     if (this.pathMatcher.match(registeredPath, urlPath)) {   
      return false;   
     }        
    }   
   }   
  }   
  if (this.checkAccessUrls != null) {      
   for (int i = 0; i < this.checkAccessUrls.length; i++) {   
    String registeredPath = checkAccessUrls[i];   
    if (registeredPath == null) {   
     throw new IllegalArgumentException("Entry number " + i + " in allowAccessUrls array is null");   
    } else {   
     if (this.pathMatcher.match(registeredPath, urlPath)) {   
      return true;   
     }        
    }   
   }   
  }   
  return false;   
}   
  
}  

```


```java
public class BackAuthorizeURLInterceptor extends HandlerInterceptorAdapter { 
private UrlPathHelper urlPathHelper = new UrlPathHelper(); 
private String[] checkAccessUrls; 
private String[] noCheckAccessUrls;//不需要保护的url资源 
private PathMatcher pathMatcher = new AntPathMatcher(); 

private String loginUrl; 
private AuthHandle authHandle;//权限检查处理器 
public void setPathMatcher(PathMatcher pathMatcher) { 
  Assert.notNull(pathMatcher, "PathMatcher must not be null"); 
  this.pathMatcher = pathMatcher; 
} 

  

/** 
  * @param loginUrl the loginUrl to set 
  */ 
public void setLoginUrl(String loginUrl) { 
Assert.notNull(loginUrl, "loginUrl must not be null"); 
  this.loginUrl = loginUrl; 
} 



/** 
  * @param checkAccessUrls the checkAccessUrls to set 
  */ 
public void setCheckAccessUrls(String[] checkAccessUrls) { 
Assert.notNull(checkAccessUrls, "checkAccessUrls must not be null"); 
  this.checkAccessUrls = checkAccessUrls; 
} 



/** 
  * @param noCheckAccessUrls the noCheckAccessUrls to set 
  */ 
public void setNoCheckAccessUrls(String[] noCheckAccessUrls) { 
  this.noCheckAccessUrls = noCheckAccessUrls; 
} 

/** 
  * @param authHandle the authHandle to set 
  */ 
public void setAuthHandle(AuthHandle authHandle) { 
  this.authHandle = authHandle; 
} 

/** 
  * 
  */ 
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 
     throws Exception { 
  String url = urlPathHelper.getLookupPathForRequest(request); 
  if (!isProtected(url) ) { 
   return true;//所请求的资源不需要保护. 
  }else if(this.pathMatcher.match(url, loginUrl+"*")){ 
   return true; 
  } 
  
  boolean b = authHandle.checkAuth(request, response); 
  
  if(!b){ 

  response.sendRedirect(request.getContextPath()+loginUrl);   
  } 
  return b; 
} 

private boolean isProtected(String urlPath) { 
  if(noCheckAccessUrls != null){ 
   for (int i = 0; i < this.noCheckAccessUrls.length; i++) { 
    String registeredPath = noCheckAccessUrls[i]; 
    if (registeredPath == null) { 
     throw new IllegalArgumentException("Entry number " + i + " in allowAccessUrls array is null"); 
    } else { 
     if (this.pathMatcher.match(registeredPath, urlPath)) { 
      return false; 
     }     
    } 
   } 
  } 
  if (this.checkAccessUrls != null) {   
   for (int i = 0; i < this.checkAccessUrls.length; i++) { 
    String registeredPath = checkAccessUrls[i]; 
    if (registeredPath == null) { 
     throw new IllegalArgumentException("Entry number " + i + " in allowAccessUrls array is null"); 
    } else { 
     if (this.pathMatcher.match(registeredPath, urlPath)) { 
      return true; 
     }     
    } 
   } 
  } 
  return false; 
} 

} 

```


response.sendRedirect(request.getContextPath()+loginUrl); 
在session 超时时　back/mgrLogin.do 
总是跳转到mgrLogin.do 
　４０４ 
不知道是什么原因 


对此：myworkfirst同学上次讨论 写道
  看下后台日志，就知道了，路径不正确 
会话超时，怎么不用 过滤器呀?

在session不超时的时候　 

response.sendRedirect(request.getContextPath()+loginUrl); 
可以正确的跳转到，配置的页面/back/mgrLogin.do 
<property name="loginUrl">   
      <value>/back/mgrLogin.do</value>   
     </property>  

但当session超时的时候，则莫名其妙的跳转到 mgrLogin.do 
不过后台没有报错。估计是session超时，对跳转有影响，我的项目中还用到了，urlrewrite. 再次到权限检查器的时候，已经是/mgrLogin.do 
解决方法么：
到也没有什么不好的，将登录的路径改为根路径解决了