簡體   English   中英

Spring配置文件默認行為

[英]Spring profile default behaviour

我有一個彈簧配置文件“DEV”,這是我唯一的配置文件,我不想創建一個“生產”配置文件。 因此,只有當配置文件是“DEV”時,我才會啟動某種類型的bean用於Spring安全性(這是一個內存來賓用戶和一個userdetails bean)

但是如果我的tomcat啟動中沒有提供spring配置文件(生產中就是這種情況),我希望我的應用程序繼續它已經在做的事情(使用ldap authenticatin提供程序)。

有沒有辦法定義“默認”bean行為而不需要在啟動時提供配置文件? 或者您可以查看下面的代碼,並提出一個不同的解決方案。

@Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth, final AuthenticationProvider provider) throws Exception {

        auth
                .eraseCredentials(false)
                .authenticationProvider(provider)
                .authenticationProvider(jwtConfig.jwtAuthenticationProvider());
}


@Bean
public UserDetailsService userDetailsService() {
    final LdapUserDetailsService ldapUserDetailsService = new LdapUserDetailsService(ldapUserSearch(), ldapAuthoritiesPopulator());
    return new CompositeUserDetailsService(Arrays.asList(technicalUserDetailsService(), ldapUserDetailsService));
}

@Bean
@Profile("DEV")
public UserDetailsService devUserDetailsService() {
 useAnonymous = true;
        InMemoryUserDetailsManagerBuilder b = new InMemoryUserDetailsManagerBuilder()
                .withUser("user").password("password").authorities(ROLE_USER, ROLE_ADMIN).and();

        return new CompositeUserDetailsService(Arrays.asList(b.build(),
                technicalUserDetailsService()));

}
@Bean
public AuthenticationProvider ldapAuthenticationProvider() {
    final BindAuthenticator ba = new BindAuthenticator((BaseLdapPathContextSource) contextSource());
    ba.setUserSearch(ldapUserSearch());
    return new LdapAuthenticationProvider(ba, ldapAuthoritiesPopulator());
}

我認為對@Profile作用存在誤解。 標記為@Profile Bean僅在該配置文件處於活動狀態時加載,但無論選擇的配置文件如何,所有其他bean(沒有@Profile )仍始終加載。

我看到幾種解決方法:

1)使用@Primary標記所有那些帶有@Profile("dev") @Primary因此Spring知道在加載兩個相同類型的bean時要選擇哪一個(因為你不想使用生產配置文件)。

2)記住,當輪廓開發與活躍, 應該裝豆@Profile("!dev") -僅適用於春季3.2和更高版本(見https://github.com/spring-projects/spring-framework / commit / bcd44f3798ed06c0704d2a3564b8a9735e747e87 )。

要么...

3)使用生產配置文件並在例如web.xml文件(您可能不在本地使用的東西)中激活它。

只需創建多個@Configuration類並使用配置文件標記整個類(它還有助於將相關內容保存在一起)。 典型的例子是數據庫。 為生產數據庫(使用JNDI和Oracle)創建一個配置類,為本地開發和測試(HSQLDB)創建一個配置類。

使用@Profile("production")標記JNDI Configuration類,使用@Profile("dev")標記另一個 - 不需要標記單個bean,只需在邏輯上將它們分成兩個不同的@Configuration類。

這對我們來說非常有效,當與集成測試結合使用時也是如此。

我會以這種方式覆蓋bean定義:

@Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth, final AuthenticationProvider provider) throws Exception {

        auth
                .eraseCredentials(false)
                .authenticationProvider(provider)
                .authenticationProvider(jwtConfig.jwtAuthenticationProvider());
}


@Bean("myUserDetailService")
public UserDetailsService userDetailsService() {
    final LdapUserDetailsService ldapUserDetailsService = new LdapUserDetailsService(ldapUserSearch(), ldapAuthoritiesPopulator());
    return new CompositeUserDetailsService(Arrays.asList(technicalUserDetailsService(), ldapUserDetailsService));
}

@Bean("myUserDetailService")
@Profile("DEV")
public UserDetailsService devUserDetailsService() {
 useAnonymous = true;
        InMemoryUserDetailsManagerBuilder b = new InMemoryUserDetailsManagerBuilder()
                .withUser("guest").password("guest").authorities(ROLE_USER, ROLE_ADMIN).and();

        return new CompositeUserDetailsService(Arrays.asList(b.build(),
                technicalUserDetailsService()));

}
@Bean("myAuthProvider")
public AuthenticationProvider ldapAuthenticationProvider() {
    final BindAuthenticator ba = new BindAuthenticator((BaseLdapPathContextSource) contextSource());
    ba.setUserSearch(ldapUserSearch());
    return new LdapAuthenticationProvider(ba, ldapAuthoritiesPopulator());
}

@Bean("myAuthProvider")
@Profile("DEV")
public AuthenticationProvider devAuthenticationProvider() {

   //find a way to return userdetails here

}

這樣,當啟動“DEV”配置文件時,該配置文件中定義的bean應覆蓋默認bean

當你自動裝豆時,你應該使用@Qualifier注釋

我希望它有用

安傑洛

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM