2012-02-13 49 views
3

我有一個web應用程序,當用戶點擊個人資料鏈接時,如果他沒有登錄,我想重定向他到登錄頁面,然後當他登錄時,我會將他發回到他最初點擊的鏈接。Spring MVC和登錄重定向

在這種情況下,他的個人資料。

我也做了一部分,直到其中重定向他到登錄頁面,但我試圖找出如何記得他最初的點擊,並重定向到這個頁面,他在成功登錄後。

我使用Spring登錄安全。

回答

8

幸運的是,Spring Security具有內置的功能,用於記住最初請求的URL,並在成功登錄後將用戶重定向到那裏。對您的問題的快速回答是,您需要通過在Spring安全配置中將始終使用默認目標選項設置爲false來啓用此功能。

例如,這裏是從Spring安全配置公共線:

<form-login 
    login-page="/login.html" 
    authentication-failure-url="/login.html?status=LOGIN_FAILURE" 
    default-target-url="/secure/index.html" 
    always-use-default-target="false" /> 

此配置將進行以下兩個流可能:

流#1

  1. 薩利requests /login.html
  2. Sally提供有效的用戶名和密碼
  3. 薩利被重定向到/secure/index.html,因爲這是默認目標網址

流#2(你想要的流量)

  1. 大衛請求/secure/kittens.html
  2. 由於David未登錄,因此他會看到登錄頁面。
  3. 提供了有效的用戶名和密碼後,David被重定向到/secure/kittens.html,這是他嘗試訪問的原始頁面。使用Spring的JavaConfig,就可以實現同樣的事情(他是不是帶到默認目標網址因爲總是使用默認的目標設置爲false。)
+0

這是否意味着對於任何請求的頁面,spring安全將開始檢查用戶是否已經登錄?如果不是,那麼如果你的意思是說只有當/secure/kittens.html被請求時,spring會嘗試檢查用戶是否登錄,我想知道這個檢查是在哪裏或如何進行的?我問的原因是我剛剛測試過,但它不起作用,我想知道如果我必須在/secure/kittens.html中編寫一些重定向邏輯? – 2012-02-13 23:01:12

+0

我試過返回「重定向:signin.htm」;在kittens.html中,但它不起作用 – 2012-02-13 23:16:09

+0

這是否意味着對於任何請求的頁面,spring安全將開始檢查用戶是否已經登錄?如果不是,那麼如果你的意思是說只有當/secure/kittens.html被請求時,spring會嘗試檢查用戶是否登錄,我想知道這個檢查是在哪裏或如何進行的?我問的原因是我剛剛測試過,但它不起作用,我想知道如果我必須在/secure/kittens.html中編寫一些重定向邏輯? – 2012-02-13 23:48:20

4

對於那些你使用

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.crypto.password.PasswordEncoder; 
import org.springframework.security.web.util.matcher.AntPathRequestMatcher; 

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    ... 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     // authentication logic   
    } 

    @Bean 
    public PasswordEncoder passwordEncoder() { 
     //password encoding logic 
     //preferably BCrpyt 
    } 

    @Override 
    public void configure(WebSecurity web) throws Exception { 
     // @formatter:off 
     web.ignoring() 
      .antMatchers("/css/**")  //Allow CSS resources. 
      .antMatchers("/js/**")  //Allow JavaScript resources. 
      .antMatchers("/images/**"); //Allow image resources. 
     // @formatter:on 
    }   
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
     .authorizeRequests() 
     .anyRequest().authenticated() 
     .and() 
     .formLogin() 
      .loginPage("/login.html")      //login-page 
      .failureUrl("/login.html?status=LOGIN_FAILURE") //authentication-failure-url 
      .defaultSuccessUrl("/secure/index.html", false) //default-target-url. set always-use-default-target to `false` 
      .permitAll() 
     .and() 
      .logout() 
      .permitAll() 
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout")); 
    } 
} 

上面的代碼段示出了沒有XML的結構。

  1. 類都被註解@Configuration指示

類聲明一個或多個@Bean方法,並且可以通過Spring容器進行處理以產生用於那些bean進行定義和服務請求運行時

  • 類都被註解@EnableWebSecurity這增加
  • 此批註的@Configuration類具有在任何WebSecurityConfigurer定義的春季安全配置

  • 該類還延伸WebSecurityConfigurerAdapter其中
  • 爲創建WebSecurityConfigurer實例提供了一個方便的基類。該實現允許通過重寫方法進行定製。

  • 您可以Autowire在一個公共的方法來處理身份驗證的AuthenticationManagerBuilder需要
  • 我希望這有助於爲那些有類似的問題,誰不」 t使用XML配置。