2011-12-06 9 views
7

私はこれをやろうとしています:Make spring security add the return to url in the query string for the login page、つまり:私が来たログインページを伝えるためにspringを取得します。私はいくつかのSSOの統合を持っているので、私はそれらにURLを送信するか、彼らは私のためにリファラーを追加するので、ユーザーがログインして、/some/urlに送信されるべきであることを知っている。それはすべてダンディです。あなたが私にAuthenticationEntryPointを実装する正当な理由を教えてくれない限り、私が抱えている問題はLoginUrlAuthenticationEntryPointを拡張することにあります。LoginUrlAuthenticationEntryPointを拡張する方法またはAuthenticationEntryPointを実装する方法

RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 

// only append returnto to '/login' urls 
if(request.getServletPath() == "/login") { 
    // indicates we want to return to this page after login 
    redirectStrategy.sendRedirect(request, response, "/login?returnto=" + request.getServletPath()); 
} 

私は残りのリクエストを自分たちの行いにすることができますか?これは、(私は何をやっていた)間違っています:

RequestDispatcher dispatcher = request.getRequestDispatcher("/login"); 

// just forward the request 
dispatcher.forward(request, response); 

それはリダイレクトループで私たちを置くため。しかし、それはwhat spring does in its version of commenceのようです。よくわかりません。私がLoginUrlAuthenticationEntryPointへの私のカスタム拡張のcommenceで何をしているのですか?

答えて

6

'returnto'が追加されたログインページに転送するだけでした。 LoginUrlAuthenticationEntryPointは一見「これはユーザーをログインページにリダイレクトする」という意味ではないため、最初は混乱していました。これは私が必要なすべてのです:

@Override 
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authenticationException) throws IOException, ServletException { 
    RedirectStrategy redirectStrategy = new DefaultRedirectStrategy(); 

    redirectStrategy.sendRedirect(request, response, "/login?returnto=" + request.getServletPath()); 
} 

が、これは有用であった:LoginUrlAuthenticationEntryPoint.java on grepcode.com

0

ジョシュのコードでは、私はまだ他のすべての要求のデフォルトのログインのリダイレクトロジックを維持しながら、JavaScriptでAJAXの認証エラーをキャッチするために必要な正確に何をしました。すべてのAJAXリクエストは "/ rest"で始まるので、これをフィルタリングすることができました。

if(request.getRequestURI().contains("/rest/")) 
    response.sendError(403); 
else 
    new DefaultRedirectStrategy().sendRedirect(request, response, "/login"); 
関連する問題