開發(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)證方式的話赡盘,
如果指定認(rèn)證方式:
可以看出陨享,指定時(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)證的類型肚吏。