1. 程式人生 > >SpringBoot+SpringSecurity之如何forword到登入頁面

SpringBoot+SpringSecurity之如何forword到登入頁面

當我們在專案中引入了SpringSecurity框架進行身份校驗的時候,如果某個請求需要使用者身份認證,那麼SpringSecurity會將使用者redirect到登入頁面。但是有些時候我們希望是forward到登入頁面而不是redirect到登入頁面,這種情況下可做如下配置:

@Bean
public AuthenticationEntryPoint loginUrlAuthenticationEntryPoint() {
    LoginUrlAuthenticationEntryPoint AuthenticationEntryPoint = new LoginUrlAuthenticationEntryPoint("/oauth/login");
    AuthenticationEntryPoint.setUseForward(
true); return AuthenticationEntryPoint; } @Bean public RequestCache requestCache() { return new HttpSessionRequestCache(); } @Bean public ExceptionTranslationFilter exceptionTranslationFilter(AuthenticationEntryPoint authenticationEntryPoint, RequestCache requestCache) { return new ExceptionTranslationFilter(authenticationEntryPoint, requestCache); }

關鍵程式碼就是標紅這部分,但是為了實現這個功能,我們需要建立三個bean。