【发布时间】:2021-06-17 21:48:30
【问题描述】:
我在 Spring Security 配置中使用了过滤器,因此除了用户名和密码之外,我还可以从登录页面获得更多参数。这是用于身份验证的过滤器:
public SimpleAuthenticationFilter authenticationFilter() throws Exception {
SimpleAuthenticationFilter filter = new SimpleAuthenticationFilter();
filter.setAuthenticationManager(authenticationManagerBean());
filter.setAuthenticationFailureHandler(failureHandler());
filter.setAuthenticationSuccessHandler(successHandler());
return filter;
}
这是成功处理程序。我认为这会完全正常工作 .defaultSuccessUrl("/home", false) ,但它不是......
public SimpleUrlAuthenticationSuccessHandler successHandler() {
SimpleUrlAuthenticationSuccessHandler successHandler = new SimpleUrlAuthenticationSuccessHandler();
successHandler.setDefaultTargetUrl("/home");
successHandler.setAlwaysUseDefaultTargetUrl(false);
return successHandler;
}
这是安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.antMatchers(resources).permitAll()
.antMatchers("/somePage").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.and()
.logout()
.logoutSuccessUrl("/login")
.and()
.csrf().disable();
}
关于我缺少什么的任何想法?目标是跳过默认的目标网址。
谢谢一百万。
【问题讨论】:
-
请出示安全配置
-
好的,我刚刚编辑了显示配置的帖子。
-
当您使用
.defaultSuccessUrl("/home", false)时,您定义了一个SavedRequestAwareAuthenticationSuccessHandler,它使用会话中保存的请求来重定向它。因此,如果您请求/home并以/login结束,登录成功后您将被重定向到/home。在您的SimpleUrlAuthenticationSuccessHandler中,您应该将alwaysUseDefaultTargetUrl设置为true。让我知道它是否适合您。
标签: url filter spring-security