私は、ログアウト/ログインを処理するために、デフォルトの春のセキュリティを使用しています。私はそのハンドルコントローラメソッドを持っています/login
。
私がログアウトすると、私は春のセキュリティに私をリダイレクトすることを確認しますapp/login?logout
。この春作成したパラメータ(とも時々の存在はapp/login?error
)私のように私のログインハンドラを記述することができます:
@GetMapping("/participant/login")
public ModelAndView loginPage(HttpServletRequest request, HttpServletResponse response,
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
log.info("Entering participant login page");
ModelAndView mav = new ModelAndView(LOGIN_JSP);
if (null != error) {
// We're coming to the login page after an Error
mav.addObject("info", "My generic error message");
} else if(null != logout){
// We're coming to the login page after a Logout
mav.addObject("info", "My generic logout message");
}
// ...Otherwise, normal Login page, no extra information
さて問題は、私はログアウト時に、私がする必要があるということですへ/ログイン転送にする/ログアウトカスタムパラメータを渡します。目標は、私が中のparam受信する必要がある/login
私はシステム作成のように調べることができることerror
としますlogout
。
このカスタムparamはあると仮定exitMsg
。
私のアプリから私はこの春のセキュリティログアウトURLを(ログアウトが自動的に行われますので、私はそれのための特定のハンドラを持たない)を発行します。
myapp.com/app/logout?exitMsg=MyMessage
すぐに、ログイン・ハンドラは、このPARAMを失い、私はそれを持っていません。
私は自分の書き込みと考え/logout
、私は手動でログアウト(セッションを無効に)し、このPARAMでログインして自分自身をリダイレクトするハンドラを、。これは、提案され、ここで。私はそれを行う場合でも、私は春の自動取得する機能を失う ?logout
と?error
要求PARAMSを。自動シナリオでは、私はそれらを得ていた、そして今、私はしませんよ。私は私が自分自身を指定するカスタムパラメータを取得しています。私は維持する必要がある?logout
と?error
し、また私自身の新しいのparamのためのテスト。
任意の考えは非常に感謝しています。
春のセキュリティ設定:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.antMatcher("/participant/**").authorizeRequests()
.antMatchers("/participant/id/**").permitAll()
.antMatchers("/participant/faq").permitAll()
.antMatchers("/participant/forgetPassword").permitAll()
.antMatchers("/participant/securityQuestions").permitAll()
.antMatchers("/participant/securityCheck").permitAll()
.antMatchers("/participant/resetPassword").permitAll()
.antMatchers("/participant/**").authenticated()
.and()
.formLogin().loginPage("/participant/login").permitAll()
.failureUrl("/participant/login?error").permitAll()
.defaultSuccessUrl("/participant/home")
.usernameParameter("username").passwordParameter("password")
.and()
.logout().logoutUrl("/participant/logout")
.logoutSuccessUrl("/participant/login?logout").permitAll()
.and()
.csrf().disable();
}
あなたは必要なlogoutSuccessHandler
のではなく、.logoutSuccessUrl("/login?logout")
設定logoutSuccessHandler
の下に与えられたとして、
@Override
protected void configure(final HttpSecurity http) throws Exception
{
http
.authorizeRequests()
.antMatchers("/resources/**", "/", "/login", "/api/**")
.permitAll()
.antMatchers("/app/admin/*")
.hasRole("ADMIN")
.antMatchers("/app/user/*")
.hasAnyRole("ADMIN", "USER")
.and().exceptionHandling().accessDeniedPage("/403")
.and().formLogin()
.loginPage("/login").usernameParameter("userName")
.passwordParameter("password")
.defaultSuccessUrl("/app/user/dashboard")
.failureUrl("/login?error=true")
.and().logout()
.logoutSuccessHandler(new CustomLogoutSuccessHandler())
.invalidateHttpSession(true)
.and().csrf().disable();
http.sessionManagement().maximumSessions(1).expiredUrl("/login?expired=true");
}
私は自分の/ログアウト・ハンドラを書いて考えられ
実際にログアウトハンドラではありませんが、それはログアウトイニシエーターです。
あなたがリクエストのparamのを取得することができますし、下記のとおりに、再びそれを設定することができます使用CustomLogoutSuccessHandler、。
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler
{
@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException
{
Cookie cookie = new Cookie("JSESSIONID", null);
cookie.setPath(request.getContextPath());
cookie.setMaxAge(0);
response.addCookie(cookie);
if(request.getParameter("expired") != null)
{
response.sendRedirect(request.getContextPath()+"/login?expired=true");
}
else
{
response.sendRedirect(request.getContextPath() + "/login?logout=true");
}
}
}
私のアプリから私はこの春のセキュリティログアウトURLを(ログアウトが自動的に行われますので、私はそれのための特定のハンドラを持っていない)を発行します
サーブレット/春のセキュリティには自動ログアウト機能はありません。唯一の私たちが達成できることで
ログアウト要求送信する1.自動化クライアント
サーバーで2.私たちは、セッションが過去の日付にクッキーの年齢を設定/クッキーを削除することによって無効にすることができそうという、セッションのmaxInactiveIntervalを設定することができます。セッションは、スプリングセキュリティフィルター・チェーン内のフィルタの次の要求のいずれかに無効にされた後のparamとに/ログインをリダイレクトするページが期限切れ。/login?expired
あなたがログアウト春のセキュリティがクッキーを削除します開始した場合/のparamログアウトをする/ログインページセッションやリダイレクトを無効にします。/login?logout
春のセキュリティでログアウトを達成するためのコンフィギュレーションの2つのタイプがあります。
.and().logout()
.invalidateHttpSession(true)
//or
.deleteCookies("JSESSIONID")
EDIT:OPの回答を見た後。ここで追加欠落情報。OPと私は、我々が発見(チャットや答えが削除されたことを)一時的な答えで長いチャットだったLOGIN-WALLを。
「ログイン-壁は」カスタムログインページを定義し、それが認証後にアクセスを許可されているリソースとして構成されて悪い*設定の結果です。(access=isAuthenticated
)私たちはセッション春のセキュリティ・ブロックのログインページへのアクセスを無効にした後、ログインページにリダイレクトする場合CustomLogoutSuccessHandlerから(401-アン認定)、そのページのみ認証されたユーザーに許可されているので。春のセキュリティをブロックした後の構成で構成されたページにリダイレクトの世話をします。`.loginPage( "/ someloginpage")。何それは考えることを強制することは、正確に起こって正しくログインページにリダイレクトするが、私は、クエリ文字列で送信されたのparamsはloginpageに来ていないログアウトです。何も間違っている私は推測するのは非常に難しいパズルとしてこれを呼び出す場合。