2017-05-01 58 views
0

目前我正在使用Spring Boot 1.4.0版本進行持續開發,使用Spring安全性進行身份驗證。要求是用戶首次登錄時需要重定向到密碼重置頁面,否則應重定向到主頁面。無論在成功處理程序中配置的URL如何,應用程序總是重定向home.jsp。春季開機安全始終重定向到home.jsp

下面是我的配置,我失去了什麼這裏

WebSecurityConfiguration

  http.authorizeRequests() 
     .antMatchers("/resources/**","/rest/**","/log*") 
     .permitAll() 
     .antMatchers("/admin**").hasAuthority("admin") 
     .anyRequest() 
     .authenticated() 
     .and() 
     .formLogin() 
     .loginPage("/login") 
     .successHandler(authHandler) 
     .failureHandler(authFailureHandler) 
     .usernameParameter("username").passwordParameter("password") 
     .permitAll() 
     .and() 
     .logout() 
     .invalidateHttpSession(true) 
     .logoutSuccessUrl("/login?logout") 
     .permitAll() 
     .and() 
     .csrf().disable(); 

公共類AuthSuccessHandler擴展SimpleUrlAuthenticationSuccessHandler {

private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 
@Override 
protected void handle(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { 
    HttpSession session = request.getSession(false); 
    String isFirstTimePwd = String.valueOf(session.getAttribute("IsFirstTimeLogIn")); 
    if (isFirstTimePwd.equalsIgnoreCase("true")) 
    { 
     redirectStrategy.sendRedirect(request,response,"/firstTime"); 
    } 
    else 
    { 
     redirectStrategy.sendRedirect(request, response, "/home"); 
    } 
} 

}

@RequestMapping(value = "/firstTime", method = RequestMethod.GET) 
public String displayFirstTimeLoginPage(HttpServletRequest request,HttpServletResponse response) { 
    return "firstTime"; 
} 

@RequestMapping(value = "/home", method = RequestMethod.GET) 
public ModelAndView homePage(HttpServletRequest request,HttpServletResponse response) { 
    HttpSession session = request.getSession(); 
    User user =(User) session.getAttribute("User"); 
    return new ModelAndView("home", "loggedInUser", user); 
} 

而且我嘗試通過重寫onAuthenticationsuccess()來實現authenticationsuccesshandler,但仍然重定向home.jsp,而不是密碼重置頁面。

+1

你嘗試調試找到什麼isFirstTimePwd.equalsIgnoreCase的'(價值」真「)? – chaoluo

+0

是的,它的第一次控制器映射和重定向密碼重置頁面之後,立即重定向到home.jsp,如果我將home.jsp重命名爲index.jsp其未顯示的文件。 – aap

+0

@ap:你必須重寫'onAuthenticationsuccess()'。顯示級別爲「DEBUG」的Spring安全日誌。它會顯示,爲什麼你沒有被重定向到正確的頁面。 – dur

回答

0

你最好去主頁和測試,如果它的firstTime或不

@RequestMapping(value = "/home", method = RequestMethod.GET) 
    public ModelAndView homePage(HttpServletRequest request,HttpServletResponse response) { 

    if (isFirstTimePwd.equalsIgnoreCase("true")) 
     { 
      redirectStrategy.sendRedirect(request,response,"/firstTime"); 
     } 

     HttpSession session = request.getSession(); 
     User user =(User) session.getAttribute("User"); 
     return new ModelAndView("home", "loggedInUser", user); 
    } 

EDIT1:

@RequestMapping(value = "/home", method = RequestMethod.GET) 
     public ModelAndView homePage(HttpServletRequest request,HttpServletResponse response) { 

     HttpSession session = request.getSession(); 
      User user =(User) session.getAttribute("User"); 
     if (isFirstTimePwd.equalsIgnoreCase("true")) 
      { 
       return new ModelAndView("firstTime", "loggedInUser", user); 
      } 

      return new ModelAndView("home", "loggedInUser", user); 
     } 
+0

這裏的問題是它的重定向兩次,第一次重定向重置頁面之後立即觸發下一次重定向到home.jsp第二次重定向時它沒有進入控制器映射。基本上它的壓倒一切。 – aap

+0

,如果我們返回新的ModelAndView(「firstTime」,「loggedInUser」,用戶);成「如果」? (請參閱Edit1) –