2012-02-13 50 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配置。