我有一个web应用程序,当用户点击个人资料链接时,如果他没有登录,我想重定向他到登录页面,然后当他登录时,我会将他发回到他最初点击的链接。Spring MVC和登录重定向
在这种情况下,他的个人资料。
我也做了一部分,直到其中重定向他到登录页面,但我试图找出如何记得他最初的点击,并重定向到这个页面,他在成功登录后。
我使用Spring登录安全。
我有一个web应用程序,当用户点击个人资料链接时,如果他没有登录,我想重定向他到登录页面,然后当他登录时,我会将他发回到他最初点击的链接。Spring MVC和登录重定向
在这种情况下,他的个人资料。
我也做了一部分,直到其中重定向他到登录页面,但我试图找出如何记得他最初的点击,并重定向到这个页面,他在成功登录后。
我使用Spring登录安全。
幸运的是,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
流#2(你想要的流量)
JavaConfig
,就可以实现同样的事情(他是不是带到默认目标网址因为总是使用默认的目标设置为false。)对于那些你使用
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
的结构。
@Configuration
指示类声明一个或多个@Bean方法,并且可以通过Spring容器进行处理以产生用于那些bean进行定义和服务请求运行时
@EnableWebSecurity
这增加此批注的@Configuration类具有在任何WebSecurityConfigurer定义的春季安全配置
WebSecurityConfigurerAdapter
其中为创建WebSecurityConfigurer实例提供了一个方便的基类。该实现允许通过重写方法进行定制。
Autowire
在一个公共的方法来处理身份验证的AuthenticationManagerBuilder
需要我希望这有助于为那些有类似的问题,谁不” t使用XML
配置。
这是否意味着对于任何请求的页面,spring安全将开始检查用户是否已经登录?如果不是,那么如果你的意思是说只有当/secure/kittens.html被请求时,spring会尝试检查用户是否登录,我想知道这个检查是在哪里或如何进行的?我问的原因是我刚刚测试过,但它不起作用,我想知道如果我必须在/secure/kittens.html中编写一些重定向逻辑? – 2012-02-13 23:01:12
我试过返回“重定向:signin.htm”;在kittens.html中,但它不起作用 – 2012-02-13 23:16:09
这是否意味着对于任何请求的页面,spring安全将开始检查用户是否已经登录?如果不是,那么如果你的意思是说只有当/secure/kittens.html被请求时,spring会尝试检查用户是否登录,我想知道这个检查是在哪里或如何进行的?我问的原因是我刚刚测试过,但它不起作用,我想知道如果我必须在/secure/kittens.html中编写一些重定向逻辑? – 2012-02-13 23:48:20