`
asdcls
  • 浏览: 17171 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

session 超时,sendredirct 问题 BeanNameUrlHandlerMapping

 
阅读更多
出问题处:
response.sendRedirect(request.getContextPath()+loginUrl);
在session 超时时 back/mgrLogin.do
总是跳转到mgrLogin.do
 404
不知道是什么原因


对此: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
解决方法么:
到也没有什么不好的,将登录的路径改为根路径解决了


<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代码
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;   
}   
  
}  

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
 404
不知道是什么原因


对此: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
解决方法么:
到也没有什么不好的,将登录的路径改为根路径解决了
1
0
分享到:
评论

相关推荐

    第二课:通过MVC原理,自定义MVC框架实现1

    分别是:RequestMappingHandlerMapping与BeanNameUrlHandlerMapping实现组成结构:RequestMappingH

    Spring-Reference_zh_CN(Spring中文参考手册)

    13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器 13.5.2. 视图解析链 13.5.3. 重定向(Rediret)到另一个视图 ...

    Spring 2.0 开发参考手册

    18.2.2. 访问本地的无状态Session Bean(SLSB) 18.2.3. 访问远程SLSB 18.3. 使用Spring提供的辅助类实现EJB组件 19. JMS 19.1. 简介 19.2. 使用Spring JMS 19.2.1. JmsTemplate 19.2.2. 连接工厂 19.2.3. ...

    spring chm文档

    Spring Framework 开发参考手册 Rod Johnson ...18.2.2. 访问本地的无状态Session Bean(SLSB) 18.2.3. 访问远程SLSB 18.3. 使用Spring提供的辅助类实现EJB组件 19. JMS 19.1. 简介 19.2. 使用Spring JMS ...

    Spring中文帮助文档

    13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器(ViewResolver) 13.5.2. 视图解析链 13.5.3. 重定向...

    Spring API

    13.4.1. BeanNameUrlHandlerMapping 13.4.2. SimpleUrlHandlerMapping 13.4.3. 拦截器(HandlerInterceptor) 13.5. 视图与视图解析 13.5.1. 视图解析器(ViewResolver) 13.5.2. 视图解析链 13.5.3. 重定向...

    SPRING API 2.0.CHM

    BeanNameUrlHandlerMapping BeanNameViewResolver BeanNotOfRequiredTypeException BeanPostProcessor BeanPropertyBindingResult BeanPropertySqlParameterSource BeanReference BeansDtdResolver ...

Global site tag (gtag.js) - Google Analytics