[spring-projects/spring-boot]自 2.0.0.M4 起,安全配置被忽略

2018-02-16 790 views
0

从 2.0.0.M4 开始,我的SecurityConfiguration扩展实现WebSecurityConfigurerAdapter被完全忽略了。结果,我的应用程序现在无法启动,因为没有AuthenticationProvider.这篇博文中的信息不是很有帮助。我错过了有关如何设置自 2.0.0.M4 以来的安全配置的其他信息(以完整示例的形式),包括AuthorizationManagerUserDetailsServicePasswordEncoder。使用 2.0.0.M3 一切正常。

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(final HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .authorizeRequests().antMatchers("/", "/sign-in").permitAll().and()
                .authorizeRequests().anyRequest().authenticated().and()
                .logout().logoutUrl("/sign-out").logoutSuccessUrl("/");
    }

    @Override
    public void configure(final AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(this.userDetailsService)
                .passwordEncoder(this.passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

回答

6

感谢您联系,但感觉这个问题更适合Stack Overflow。正如贡献指南中提到的,我们更喜欢仅使用 GitHub 问题来解决错误和增强功能。请随意更新此问题,并提供重新发布的问题的链接(以便其他人可以找到它),或者如果您认为这是一个真正的错误,则添加更多详细信息。

2

我们还已经有了一些安全样本,包括这个

3

感谢您联系,但感觉这个问题更适合 Stack Overflow。

不,这对我来说是个问题。自 Spring Boot 2.0.0.M4 以来,现有的 spring 安全配置不再起作用。在我知道为什么这不再起作用之前,我无法迁移到 Spring Boot 2.0.0.M4 或更高版本。

从 Spring Boot 2.0.0.M4 开始,感觉 Spring Security 根本没有执行。我现有的SecurityConfiguration被忽略并且不被执行。这意味着我的应用程序上下文中没有AuthenticationManageror 。未执行PasswordEncoderWeb 安全配置 ( )。HttpSecurity

从我的角度来看,造成这种情况的可能原因只有两个。

  1. 某个地方有一个错误,需要修复。
  2. 缺少文档,应该解释如何迁移现有WebSecurityConfigurerAdapter配置(请参阅我的示例),以便它像以前一样工作。我之前提到的博客文章没有涵盖这一点,您提到的示例也没有涵盖这一点。

我完全一无所知,而你只是将我发送到 Stack Overflow 寻求帮助来解决与 2.0.0M3 配合得很好的 Spring Boot 问题!?

0

如果您希望我们花时间调查该问题,那么您必须花一些时间来说明为什么您认为这是 Spring Boot 中的问题,而不是您的代码中的问题。从一个小片段很难看出。一个完整的、最小的示例将使人们更容易在这里或在 Stack Overflow 上为您提供帮助。这个问题也可能是环境问题。例如,新版本的依赖项在下载时可能已损坏。

9

我尝试了一些事情,我想我发现了一些东西。这都是关于初始化顺序的。

基本上我的项目中的这两个组件很重要:

  • AuthenticationService

    @Service
    class AuthenticationService {
    
        @Autowired
        private AuthenticationManager authenticationManager;
    
        // [...]
    
        public void authenticate(final UsernamePasswordAuthenticationToken credentials) {
            // Use `authenticationManager` to get `Authentication` from given credentials and set it to the `SecurityContext`
        }
    }
  • SecurityConfiguration

    @Configuration
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private UserDetailsService userDetailsService;
    
        @Autowired
        private PasswordEncoder passwordEncoder;
    
        // [...]
    
        @Override
        protected void configure(final HttpSecurity httpSecurity) throws Exception {
            // [...]
        }
    
        @Override
        protected void configure(final AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
            authenticationManagerBuilder
                    .userDetailsService(this.userDetailsService);
                    .passwordEncoder(this.passwordEncoder;
        }
    }

使用 Spring Boot 2.0.0.M3 初始化顺序是正确的。首先初始化SecurityConfiguration,因此当要初始化AuthenticationManager时, 会被初始化并可从应用程序上下文中使用。AuthenticationService可以AuthenticationManager注入并且所有组件都以正确的顺序初始化。

对于 Spring Boot 2.0.0.M4 及更高版本(也使用 RC1 进行测试),SecurityConfigurationAuthenticationService.这意味着AuthenticationManager尚未初始化并且应用程序无法启动,因为AuthenticationManager丢失并且无法注入到AuthenticationService.如果我删除临时文件AuthenticationManager中的依赖项AuthenticationService,我可以清楚地看到 和SecurityConfiguration都已AuthenticationService初始化,但顺序错误。

AuthenticationManagerBuilderconfigure方法中使用是我所知道的最简单的方法,主要使用默认设置来WebSecurityConfigurerAdapter设置,只需添加 a和 a 。AuthenticationManagerUserDetailsServicePasswordEncoder

7

我敢打赌您正在使用 M4 之前的 Spring Security 4.x。检查您的类路径,确保您不应该有 Spring Security 4.x jar。

不,我检查过。我正在使用中定义的版本org.springframework.boot:spring-boot-dependencies

  • Spring Boot 2.0.0.M3 → Spring Security 5.0.0.M3
  • 春季启动 2.0.0.M4 → 春季安全 5.0.0.M4
  • Spring Boot 2.0.0.RC1 → Spring Security 5.0.1.RELEASE
7

谢谢,但是如果没有完整的示例,我们就无法诊断明显的问题。

0

谢谢,但是如果没有完整的示例,我们就无法诊断明显的问题。

我不明白你还需要我做什么。

1

@black2342如果您希望我们花时间调查问题,请提供一个我们可以自己运行的示例,而不是只能让我们猜测问题所在的代码片段。无论如何我们不想这样做。

3

抱歉,但我必须再问一次。我的意思是,是的,这似乎是重复的......但即使是#11136也只是解释说,对 Spring Security 自动配置进行了更改,并且UserDetailsService自动添加到AuthorizationManager.

但是:WebSecurityConfigurerAdapter配置仍然AuthorizationManager在依赖于AuthorizationManager.这意味着这个新的自动配置执行得太晚了,所以我必须通过AuthorizationManager自己创建来解决这个问题?我看不到任何评论、文档、解释或确认,表明这按预期工作。我特别指的是初始化顺序。

此改进的目标是简化安全自动配置,请参阅博客文章。我喜欢这个主意!但现在我必须AuthorizationManager自己创建整个项目,而不是仅仅添加一个,UserDetailsService因为我的自定义 bean(实际上取决于AuthorizationManager)在所有这些之前就已经初始化了!

我看不出这(AuthorizationManager自己创建)如何使我的网络安全配置变得更容易。

AuthorizationManager在提供所需的 Spring Security 自动配置之前初始化的自定义 bean 中使用的官方建议方法是什么AuthorizationManager