【问题标题】:how to set always-use-default-target in spring security?如何在 Spring Security 中设置始终使用默认目标?
【发布时间】:2018-05-13 11:48:40
【问题描述】:

我需要在我的 Spring Boot 应用程序中的 SavedRequestAwareAuthenticationSuccessHandler 中设置 always-use-default-target="true"。我怎样才能做到这一点?我试过了:

@Bean
SavedRequestAwareAuthenticationSuccessHandler handler() {
    SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler();
    handler.setAlwaysUseDefaultTargetUrl(true);
    return  handler;
}

但没有成功。似乎使用了完全不同的 bean

我的安全配置:

@EnableOAuth2Sso
@Configuration
@Order(2)
public class FormWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired OAuth2ProtectedResourceDetails oAuth2ProtectedResourceDetails;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/**")
                .authorizeRequests()
                .antMatchers("/login**", "/assets/**", "/uaa/**", "/management/health").permitAll()
                .anyRequest().authenticated()
            .and()
                .headers()
                .defaultsDisabled()
                .frameOptions()
                .sameOrigin()
            .and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers("/uaa/**")
            .and()
                .logout()
                .logoutSuccessUrl("/login?logout")
                .permitAll();
    }
}

和:

@Configuration
@Order(1)
public class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired OAuth2ProtectedResourceDetails oAuth2ProtectedResourceDetails;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/api/**")
                .authorizeRequests()
                .anyRequest().authenticated()
            .and()
                .csrf()
                .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
                .formLogin()
                .successForwardUrl("/")
            .and()
                .exceptionHandling().authenticationEntryPoint(new Unauthorized401EntryPoint());
    }

    public static class Unauthorized401EntryPoint implements AuthenticationEntryPoint {

        @Override
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception)
                throws IOException, ServletException {

            response.sendError(HttpServletResponse.SC_UNAUTHORIZED);

        }
    }
}

【问题讨论】:

  • 我尝试声明新bean,我尝试设置.successHandler(...) 我尝试defaultSuccessUrl("/", true) 没有工作
  • 设置.successHandler(...) 应该可以工作。你能edit你的问题与设置成功处理程序的代码吗?我们谈论表单身份验证吗?
  • 这使用 oauth,所以它重定向做身份提供者,然后重定向回 /login
  • 你能看看stackoverflow.com/questions/47693963/…吗?也许对你有帮助。

标签: java spring spring-boot spring-security


【解决方案1】:

将以下用于基于注释的设置:

defaultSuccessUrl("/dashboard",true)

第二个布尔参数将always-use-default-target设置为true

【讨论】:

    猜你喜欢
    • 2014-11-18
    • 2016-06-06
    • 2019-02-04
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多