2017-05-01 11 views
0

現在、私は春のセキュリティを使用して、開発中の私の開発には、春のブート1.4.0のバージョンを使用しています。要件は、初めてのユーザーログインがパスワードリセットページにリダイレクトする必要がある場合です。それ以外の場合は、ホームページにリダイレクトする必要があります。アプリケーションは、成功ハンドラで設定された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); 
} 
を拡張し、何もないのです、私の構成です

また、私はonAuthenticationsccess()をオーバーライドしてauthenticationsuccesshandlerを実装しようとしましたが、パスワードリセットページの代わりにhome.jspをリダイレクトしました。

+1

あなたはisFirstTimePwd.equalsIgnoreCase 'の値が(」何を見つけるデバッグしようですか本当ですか?) '? – chaoluo

+0

はい、その最初のコントローラーのマッピングとパスワードリセットページをリダイレクトしてhome.jspに直ちにリダイレクトした後、home.jspの名前をindex.jspに変更した場合、そのファイルが見つかりません。 – aap

+0

@aap: 'onAuthenticationsUccess()'をオーバーライドする必要があります。 'DEBUG'レベルのSpringセキュリティログを表示します。それは、あなたが正しいページにリダイレクトされない理由を示します。 – dur

答えて

0

それは

@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 FIRSTTIMEかではない場合、あなたがより良いホームページとテストに行きたい:

@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

ここで問題となっているのは、2回リダイレクトすることです。最初にそのリダイレクト・リセット・ページがその直後にトリガーする次のリダイレクトhome.jspに2回目のリダイレクト時にコントローラ・マッピングに入ることはありません。基本的にはオーバーライド。 – aap

+0

を返し、新しいModelAndView( "firstTime"、 "loggedInUser"、user)を返します。 "if"に?? (Edit1を参照) –

関連する問題