2014-03-12 44 views
2
public class ValidateSession extends HandlerInterceptorAdapter { 

    //before the actual handler will be executed 
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {  
     HttpSession session = request.getSession(); 
     if(session.getAttribute("user")==null){ 
      /*ModelAndView mav = new ModelAndView("/login/index"); 
      throw new ModelAndViewDefiningException(mav);*/ 
      ModelAndView mav = new ModelAndView(); 
      mav.setViewName("redirect:/login/index.mars"); 
      throw new ModelAndViewDefiningException(mav);   
     } 

     return true; 
    } 
} 

在我的情況下,如果會話過期,則用戶無法訪問我的應用程序,但我堅持重定向循環。雖然我已經嘗試了許多可能的方式,但沒有運氣:(如何避免在春季web重定向循環mvc

回答

0

我知道這是一個老的文章,但認爲它可能會有所幫助,因爲沒有以上爲我工作

在類實現的HandlerInterceptor:

response.sendRedirect("login?logout=true"); 

然後控制器:

@RequestMapping(value = "login", method = RequestMethod.GET) 
public String login(ModelMap modelmap, HttpServletRequest request, HttpSession httpSession, @RequestParam(required = false) String logout, @RequestParam(required = false) String reason, @RequestParam(required = false) String message) { 
    if (reason != null && MvcStatics.ERROR_MAP.get(reason) != null) { 
     ArrayList<String> errors = new ArrayList<>(); 
     errors.add(MvcStatics.ERROR_MAP.get(reason)); 
     modelmap.addAttribute("errors", errors); 
    } 

    if (logout != null && logout.equalsIgnoreCase("true")) { 
     httpSession.removeAttribute("loggedIn"); 
     httpSession.removeAttribute("user"); 
     httpSession.removeAttribute("token"); 
     modelmap.addAttribute("message", "You have successfully been logged out."); 
    }} 
2

不要ValidateSession處理器與login/index.mars要求Use mvc interceptor排除可能的,因爲3.2關聯。我想。

<mvc:interceptors> 
    <mvc:interceptor> 
     <mvc:mapping path="/yourpath/*"/> 
     <exclude-mapping path="/login/index.mars"/> 
     <bean class="org...ValidateSession " /> 
    </mvc:interceptor> 
</mvc:interceptors> 
+0

感謝稔,但攔截器沒有與路徑工作=追加「/ *」,任何想法我怎麼可以啓用整個應用程序的攔截器。 –

+0

它通過添加

0

我經歷了同樣的問題。只是刪除了「重定向:」之前「/login/index.mars」

ModelAndView mav = new ModelAndView(); 
    mav.setViewName("redirect:/login/index.mars"); 
    //to this and you redirect ll works fine 
    mav.setViewName("/login/index.mars"); 
    throw new ModelAndViewDefiningException(mav);