SpringBoot集成remote-shell與spring-security集成的注意點(diǎn)

開發(fā)過程中遇到奇怪的現(xiàn)象腾供,當(dāng)我集成spring-security后變進(jìn)入不了remote-shell的操作界面鲜滩,也就是說用戶名和密碼設(shè)置了但是不起作用。本節(jié)說明的內(nèi)容是SpringBoot的版本為1.4.0.RELEASE榜聂。
首先须肆,我們?cè)賧ml中配置的內(nèi)容如下

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

表示的是用戶名和密碼都是test.
先說一下我們的解決方案:

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

其實(shí)就是手動(dòng)指定了一下認(rèn)證的方式為simple豌汇。但是為什么通過手動(dòng)指定就可以呢泄隔?讓我們看一下加上和不加上對(duì)于掃描Bean的區(qū)別(只看關(guān)鍵的Bean)佛嬉。我們首先需要了解的是spring boot 引入CRaSH是通過CrshAutoConfiguration。
如果不指定認(rèn)證方式的話赡盘,

不指定.png

如果指定認(rèn)證方式:

指定.png

可以看出陨享,指定時(shí)使用了SimpleAuthenticationProperties抛姑,不指定時(shí)會(huì)多出AuthenticationManagerAdapterConfiguration定硝,AuthenticationManagerAdapter毫目,SpringAuthenticationProperties诲侮。
下面我截取了里面的關(guān)鍵代碼

    @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如果設(shè)置了為simple(如果沒有設(shè)置那么也可以匹配)那么就會(huì)初始化SimpleAuthenticationProperties
        @ConditionalOnProperty(prefix = AUTH_PREFIX, name = "type", havingValue = "simple", matchIfMissing = true)
        //沒有CrshShellAuthenticationProperties類型的實(shí)例創(chuàng)建SimpleAuthenticationProperties 的實(shí)例
        @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存在的情況下才會(huì)初始化AuthenticationManagerAdapterConfiguration Bean
    @ConditionalOnBean(AuthenticationManager.class)
    public static class AuthenticationManagerAdapterConfiguration {

        private final ManagementServerProperties management;

        public AuthenticationManagerAdapterConfiguration(
                ObjectProvider<ManagementServerProperties> 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<String> roles = this.management.getSecurity().getRoles();
                authenticationProperties
                        .setRoles(roles.toArray(new String[roles.size()]));
            }
            return authenticationProperties;
        }
    }

一些關(guān)鍵的注解都做了解釋,現(xiàn)在來總結(jié)一下绽慈。
當(dāng)沒有使用到spring-security的時(shí)候辈毯,因?yàn)?/p>

@ConditionalOnBean(AuthenticationManager.class)

條件不滿足谆沃,所以AuthenticationManagerAdapterConfiguration這個(gè)Bean就無法實(shí)例化唁影。然后

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

條件都滿足,所以初始化了SimpleAuthenticationProperties啃炸,而這個(gè)類里面放的就是我們的設(shè)置的用戶名和密碼南用。

當(dāng)我們引入了spring-security并且有AuthenticationManager類型的Bean的時(shí)候掏湾,那么

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

這個(gè)Bean就會(huì)實(shí)例化融击。隨后

@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<String> roles = this.management.getSecurity().getRoles();
                authenticationProperties
                        .setRoles(roles.toArray(new String[roles.size()]));
            }
            return authenticationProperties;
        }

這兩個(gè)Bean也會(huì)實(shí)例化尊浪。而我們需要的SimpleAuthenticationProperties這個(gè)Bean因?yàn)?/p>

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

這個(gè)條件不滿足所以不能初始化成功。因?yàn)閠ype已經(jīng)是spring了捣作。

也許有人會(huì)有疑惑券躁,為什么

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

spring會(huì)選擇后者,因?yàn)楹笳叽蛟诹薈onfiguration上以舒,而前者打在了Bean
Spring會(huì)先處理Configuration后處理Bean蔓钟。

綜上所述岸军,要想保證功能remote-shell功能正常運(yùn)行瓦侮,加上認(rèn)證的類型肚吏。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末罚攀,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子杯瞻,更是在濱河造成了極大的恐慌魁莉,老刑警劉巖募胃,帶你破解...
    沈念sama閱讀 217,734評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件检疫,死亡現(xiàn)場(chǎng)離奇詭異屎媳,居然都是意外死亡论巍,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門晒来,熙熙樓的掌柜王于貴愁眉苦臉地迎上來湃崩,“玉大人,你說我怎么就攤上這事朵诫〖舴担” “怎么了?”我有些...
    開封第一講書人閱讀 164,133評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長日缨。 經(jīng)常有香客問我匣距,道長,這世上最難降的妖魔是什么尚卫? 我笑而不...
    開封第一講書人閱讀 58,532評(píng)論 1 293
  • 正文 為了忘掉前任吱涉,我火速辦了婚禮邑飒,結(jié)果婚禮上疙咸,老公的妹妹穿的比我還像新娘。我一直安慰自己撒轮,他們只是感情好题山,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,585評(píng)論 6 392
  • 文/花漫 我一把揭開白布顶瞳。 她就那樣靜靜地躺著,像睡著了一般慨菱。 火紅的嫁衣襯著肌膚如雪焰络。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,462評(píng)論 1 302
  • 那天符喝,我揣著相機(jī)與錄音闪彼,去河邊找鬼。 笑死协饲,一個(gè)胖子當(dāng)著我的面吹牛畏腕,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播茉稠,決...
    沈念sama閱讀 40,262評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼描馅,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了战惊?” 一聲冷哼從身側(cè)響起流昏,我...
    開封第一講書人閱讀 39,153評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤吞获,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后烤黍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體娘赴,經(jīng)...
    沈念sama閱讀 45,587評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡竿奏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,792評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了伍纫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片泌神。...
    茶點(diǎn)故事閱讀 39,919評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡损趋,死狀恐怖蒋失,靈堂內(nèi)的尸體忽然破棺而出镊靴,到底是詐尸還是另有隱情煮落,我是刑警寧澤,帶...
    沈念sama閱讀 35,635評(píng)論 5 345
  • 正文 年R本政府宣布呀枢,位于F島的核電站裙秋,受9級(jí)特大地震影響进宝,放射性物質(zhì)發(fā)生泄漏徐块。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,237評(píng)論 3 329
  • 文/蒙蒙 一橙困、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽美澳。三九已至,卻和暖如春凫岖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評(píng)論 1 269
  • 我被黑心中介騙來泰國打工峭弟, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人赌蔑。 一個(gè)月前我還...
    沈念sama閱讀 48,048評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親请祖。 傳聞我的和親對(duì)象是個(gè)殘疾皇子慎陵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,864評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容