java - 如何在Spring Security中设置always-use-default-target?

标签 java spring spring-boot spring-security

我需要在 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);

        }
    }
}

最佳答案

使用以下内容进行基于注释的设置:

defaultSuccessUrl("/dashboard",true)

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

关于java - 如何在Spring Security中设置always-use-default-target?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47558375/

相关文章:

java - 我无法通过两步定义在 Cucumber Spring Boot 测试中使用 @Spy 对象

java - 使用 Mockito 模拟 EntityManager 的查找方法?

java - 为 spring 项目配置 liquibase

java - Spring Boot 中的库不匹配问题

java - 如何在 Spring Boot 集成测试中读取生成的属性文件?

java - 有没有办法让线程池线程退出处理给定的任务?

java - JNLP 作为 HTML 页面中的 Applet

java - 当我执行 ""+1 时,我得到一个字符串 - 为什么

spring - 让 @SpringBootTest 在每次测试中使用新的应用程序

java - 定制/扩展Spring对shiro的@Async支持