SpringBoot集成remote-shell与spring-security集成的注意点

开发过程中遇到奇怪的现象,当我集成spring-security后变进入不了remote-shell的操作界面,也就是说用户名和密码设置了但是不起作用。本节说明的内容是SpringBoot的版本为1.4.0.RELEASE。
首先,我们再yml中配置的内容如下

management.shell.auth:
                    simple.user:
                            name: test
                            password: test

表示的是用户名和密码都是test.
先说一下我们的解决方案:

management.shell.auth:
                    simple.user:
                            name: test
                            password: test
management.shell.auth.type: simple

其实就是手动指定了一下认证的方式为simple。但是为什么通过手动指定就可以呢?让我们看一下加上和不加上对于扫描Bean的区别(只看关键的Bean)。我们首先需要了解的是spring boot 引入CRaSH是通过CrshAutoConfiguration。
如果不指定认证方式的话,

SpringBoot集成remote-shell与spring-security集成的注意点_第1张图片
不指定.png

如果指定认证方式:

SpringBoot集成remote-shell与spring-security集成的注意点_第2张图片
指定.png

可以看出,指定时使用了SimpleAuthenticationProperties,不指定时会多出AuthenticationManagerAdapterConfiguration,AuthenticationManagerAdapter,SpringAuthenticationProperties。
下面我截取了里面的关键代码

    @Configuration
    static class CrshAdditionalPropertiesConfiguration {

        @Bean
        @ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "jaas")
        @ConditionalOnMissingBean(CrshShellAuthenticationProperties.class)
        public JaasAuthenticationProperties jaasAuthenticationProperties() {
            return new JaasAuthenticationProperties();
        }

        @Bean
        @ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "key")
        @ConditionalOnMissingBean(CrshShellAuthenticationProperties.class)
        public KeyAuthenticationProperties keyAuthenticationProperties() {
            return new KeyAuthenticationProperties();
        }

        @Bean
        //条件属性management.shell.auth.type如果设置了为simple(如果没有设置那么也可以匹配)那么就会初始化SimpleAuthenticationProperties
        @ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "simple", matchIfMissing = true)
        //没有CrshShellAuthenticationProperties类型的实例创建SimpleAuthenticationProperties 的实例
        @ConditionalOnMissingBean(CrshShellAuthenticationProperties.class)
        public SimpleAuthenticationProperties simpleAuthenticationProperties() {
            return new SimpleAuthenticationProperties();
        }

    }

    /**
     * Class to configure CRaSH to authenticate against Spring Security.
     */
    @Configuration
    @ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "spring", matchIfMissing = true)
      //AuthenticationManager类型的Bean存在的情况下才会初始化AuthenticationManagerAdapterConfiguration Bean
    @ConditionalOnBean(AuthenticationManager.class)
    public static class AuthenticationManagerAdapterConfiguration {

        private final ManagementServerProperties management;

        public AuthenticationManagerAdapterConfiguration(
                ObjectProvider managementProvider) {
            this.management = managementProvider.getIfAvailable();
        }

        @Bean
        public AuthenticationManagerAdapter shellAuthenticationManager() {
            return new AuthenticationManagerAdapter();
        }

        @Bean
        @ConditionalOnMissingBean(CrshShellAuthenticationProperties.class)
        public SpringAuthenticationProperties springAuthenticationProperties() {
            SpringAuthenticationProperties authenticationProperties = new SpringAuthenticationProperties();
            if (this.management != null) {
                List roles = this.management.getSecurity().getRoles();
                authenticationProperties
                        .setRoles(roles.toArray(new String[roles.size()]));
            }
            return authenticationProperties;
        }
    }

一些关键的注解都做了解释,现在来总结一下。
当没有使用到spring-security的时候,因为

@ConditionalOnBean(AuthenticationManager.class)

条件不满足,所以AuthenticationManagerAdapterConfiguration这个Bean就无法实例化。然后

        @Bean
        @ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "simple", matchIfMissing = true)
        @ConditionalOnMissingBean(CrshShellAuthenticationProperties.class)
        public SimpleAuthenticationProperties simpleAuthenticationProperties() {
            return new SimpleAuthenticationProperties();
        }

条件都满足,所以初始化了SimpleAuthenticationProperties,而这个类里面放的就是我们的设置的用户名和密码。

当我们引入了spring-security并且有AuthenticationManager类型的Bean的时候,那么

@Configuration
    @ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "spring", matchIfMissing = true)
    @ConditionalOnBean(AuthenticationManager.class)
    public static class AuthenticationManagerAdapterConfiguration {...}

这个Bean就会实例化。随后

@Bean
        public AuthenticationManagerAdapter shellAuthenticationManager() {
            return new AuthenticationManagerAdapter();
        }

        @Bean
        @ConditionalOnMissingBean(CrshShellAuthenticationProperties.class)
        public SpringAuthenticationProperties springAuthenticationProperties() {
            // In case no management.shell.auth.type property is provided fall back to
            // Spring Security based authentication and get role to access shell from
            // ManagementServerProperties.
            // In case management.shell.auth.type is set to spring and roles are
            // configured using shell.auth.spring.roles the below default role will be
            // overridden by ConfigurationProperties.
            SpringAuthenticationProperties authenticationProperties = new SpringAuthenticationProperties();
            if (this.management != null) {
                List roles = this.management.getSecurity().getRoles();
                authenticationProperties
                        .setRoles(roles.toArray(new String[roles.size()]));
            }
            return authenticationProperties;
        }

这两个Bean也会实例化。而我们需要的SimpleAuthenticationProperties这个Bean因为

@ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "simple", matchIfMissing = true)

这个条件不满足所以不能初始化成功。因为type已经是spring了。

也许有人会有疑惑,为什么

@ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "simple", matchIfMissing = true)
@ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "spring", matchIfMissing = true)

spring会选择后者,因为后者打在了Configuration上,而前者打在了Bean
Spring会先处理Configuration后处理Bean。

综上所述,要想保证功能remote-shell功能正常运行,加上认证的类型。

你可能感兴趣的:(SpringBoot集成remote-shell与spring-security集成的注意点)