2016-12-19 2 views
0

私はSpring 3.1バージョンを使用しています。Springを使用してホームページの代わりに最後にアクセスしたページにURLをリダイレクト

私は自分のWebポータルへのログインに春のセキュリティを実装しています。 1つの問題を除いて正常に動作します。セッションのタイムアウトを2分に設定しました。

タイムアウトが経過してユーザーが任意のURLをクリックすると、ログアウトページにリダイレクトされます。しかし、ユーザーが再認証すると、ユーザーは最後のアクセスページではなく、デフォルトのターゲットURLであるホームページに直接アクセスします。

ユーザーが/home/editproductにアクセスした場合のように、タイムアウト後に&を再認証すると、/ homeページではなくhome/editproductにアクセスする必要があります。ここで

は私のapplicationContext.xmlをファイルです:

<bean id="myNeAdminUserNamePasswordAuthFilter" 
    class="com.ne.mynelson.authentication.adminuser.MyNeAdminUserPasswordAuthFilter"> 
     <property name="authenticationManager" ref="myNeAdminUserAuthManager" /> 
     <property name="authenticationFailureHandler" ref="adminFailureHandler" /> 
     <property name="authenticationSuccessHandler" ref="adminSuccessHandler" /> 

     <property name="authenticationInputProcessor" ref="myNeAdminUserAuthInputProcessor"></property> 
    </bean> 

<bean id="adminSuccessHandler" 
     class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"> 
     <property name="defaultTargetUrl" value="/bookShelfController.htm" /> 

     <property name="useReferer" value="true"/>  
    </bean> 
<bean id="adminFailureHandler" 
     class="com.ne.mynelson.authentication.adminuser.AdminUrlAuthenticationFailureHandler"> 
     <property name="defaultFailureUrl" value="/adminlogin.htm"></property> 
    </bean> 
+0

<ビーンID = "adminSuccessHandler" \t \tクラス= "org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"> \t \t <プロパティ名= "defaultTargetUrl" 値= "/ bookShelfController.htm" /> \t \t \t \t <プロパティ名= "useReferer" 値= "true" を/> \t – Sudhir

答えて

0

あなたが代わりにSimpleUrlAuthenticationSuccessHandlerSavedRequestAwareAuthenticationSuccessHandlerを使用することができます。

ExceptionTranslationFilterは、エントリポイントにリダイレクトする前にRequestCacheにリクエストを保存し、SavedRequestAwareAuthenticationSuccessHandlerはリクエストのターゲットURLを取得してから保存されたURLにリダイレクトしようとします。

<bean id="adminSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler" /> 

More see here

+0

こんにちは私はカスタムsucessHandlerを追加し、私から正常に動作SavedRequestAwareAuthenticationSuccessHandlerからそのクラスを実装します。 – Sudhir

+0

私はさらに何かを追加します、http://stackoverflow.com/questions/41259471/redirect-to-previous-url-after-timeout/41261668#41261668 – chaoluo

0

私はこの方法を行っている、どのように行うために私

public class AdminUrlAutenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler/*SimpleUrlAuthenticationSuccessHandler*/ { 
    //private String defaultFailureUrl; 
    private RequestCache requestCache = new HttpSessionRequestCache(); 

    public RequestCache getRequestCache() { 
     return requestCache; 
    } 

    public void setRequestCache(RequestCache requestCache) { 
     this.requestCache = requestCache; 
    } 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, 
     Authentication authentication) throws ServletException, IOException { 
    SavedRequest savedRequest = requestCache.getRequest(request, response); 

    if (savedRequest == null) { 
     HttpSession session = request.getSession(); 
     if (session != null) { 
      String redirectUrl = (String) session.getAttribute("url_prior_login"); 
      if (redirectUrl != null) { 
       session.removeAttribute("url_prior_login"); 
       getRedirectStrategy().sendRedirect(request, response, redirectUrl); 
      } else { 
       super.onAuthenticationSuccess(request, response, authentication); 
      } 
     } else { 
      super.onAuthenticationSuccess(request, response, authentication); 
     } 

     return; 
    } 

    String targetUrlParameter = getTargetUrlParameter(); 
    if (isAlwaysUseDefaultTargetUrl() 
      || (targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))) { 
     requestCache.removeRequest(request, response); 
     super.onAuthenticationSuccess(request, response, authentication); 

     return; 
    } 

    clearAuthenticationAttributes(request); 

    // Use the DefaultSavedRequest URL 
    String targetUrl = savedRequest.getRedirectUrl(); 
    logger.debug("Redirecting to DefaultSavedRequest Url: " + targetUrl); 
    getRedirectStrategy().sendRedirect(request, response, targetUrl); 
} 
} 

アプリケーション-context.xmlファイル

<bean id="myNeAdminUserNamePasswordAuthFilter" 
class="com.ne.mynelson.authentication.adminuser.MyNeAdminUserPasswordAuthFilter"> 
     <property name="authenticationManager" ref="myNeAdminUserAuthManager" /> 
     <property name="authenticationFailureHandler" ref="adminFailureHandler" /> 
     <property name="authenticationSuccessHandler" ref="adminSuccessHandler" /> 

     <property name="authenticationInputProcessor" ref="myNeAdminUserAuthInputProcessor"></property> 
    </bean> 


<bean id="adminSuccessHandler" 
     class="com.ne.mynelson.authentication.adminuser.AdminUrlAutenticationSuccessHandler"> 
     <property name="defaultTargetUrl" value="/bookShelfController.htm" /> 
     <property name="targetUrlParameter" value="spring-security-redirect"/> 
     <property name="useReferer" value="false"/>  
    </bean> 
    <bean id="adminFailureHandler" class="com.ne.mynelson.authentication.adminuser.AdminUrlAuthenticationFailureHandler"> 
     <property name="defaultFailureUrl" value="/adminlogin.htm"></property> 
    </bean> 

任意の提案のための罰金のようですこれは別の方法で。

+0

"url_prior_login"をいつセッションに設定しましたか?私は 'SavedRequestAwareAuthenticationSuccessHandler'の使用はOKです、なぜあなたは再びオーバーライドする必要があると思いますか? – chaoluo

関連する問題