try*_*arn 7 java spring spring-security spring-java-config
是否可以以从外部文件读取配置详细信息并相应配置的方式配置Spring安全性.
(我不是在运行时更改配置.我在谈论在启动时从文件中读取)
我现有 Sporing安全配置的一个示例是:
@EnableWebSecurity
@Configuration
public class SecurityConfig {
@Bean
public UserDetailsService userDetailsService() throws Exception {
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
manager.createUser(User.withUsername("user").password("userPass").roles("USER").build());
manager.createUser(User.withUsername("admin").password("adminPass").roles("ADMIN").build());
return manager;
}
@Configuration
@Order(1)
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user").password("user").roles("USER");
auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN");
}
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/v1/**")
.authorizeRequests()
.antMatchers("/api/v1/**").authenticated()
.and()
.httpBasic();
}
}
@Configuration
@Order(2)
public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Override
public void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("user").roles("USER");
auth.inMemoryAuthentication().withUser("admin1").password("admin").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/test/**")
.authorizeRequests()
.antMatchers("/api/test/**").authenticated()
.and()
.formLogin();
}
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在使用多个配置(看一下Order()注释).我想要做的是在启动次数和配置类型时决定.示例第一客户端可能希望具有2个配置,例如LdapConfig和SamlConfig.其他可能想要LdapConfig和SqlConfig第三可能想要4-5配置.有可能吗?
注意:我没有使用Spring Boot
编辑
我为什么这样做的总结:
通过客户我的意思是会买我的产品的公司.并通过用户 我的意思是买我的产品,公司的实际最终用户.所以我把产品运到了3家公司.首先将它配置为具有ldap auth flow和google-oauth2 auth流.第一家公司的用户将看到包含这两个选项的登录页面.公司2现在可能有一个 ldap auth流和saml auth流程,该公司的用户将看到这两个选项.该公司正在启动之前选择可用的选项.
您可以在创建 WebApplicationContext 之前加载属性,例如数据库凭据。看下面的例子:
public class WebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Tell the EnvironmentManager to load the properties. The path to the config
// file is set by Tomcat's home variable. If you change the container you might
// need to change this, too.
EnvironmentParamManager.initialize(System.getProperty("catalina.home"));
// now create the Spring Context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(RootConfig.class);
rootContext.setServletContext(servletContext);
SpringApplicationContextProvider.configure(rootContext);
// ... other config
}
Run Code Online (Sandbox Code Playgroud)
EnvironmentParamManager 可能如下所示。我决定将其设为静态,以便可以从任何地方访问这些属性,即使在应用程序的非 Spring 部分也是如此。
public class EnvironmentParamManager {
private static Properties properties = new Properties();
public static void initialize(String pathToConfigFile) {
BufferedInputStream stream;
try {
stream = new BufferedInputStream(new FileInputStream(
pathToConfigFile + "myconfig.props"));
properties.load(stream);
stream.close();
} catch (Throwable e) {
throw new Error("Cannot read environment settings from file " + pathToConfigFile);
}
}
public static String getMongoDBHostname() {
return properties.getProperty("mongodb.username");
}
}
Run Code Online (Sandbox Code Playgroud)
使用 JavaConfig 时,您可以在 Bean 创建阶段轻松访问您的配置属性,如下所示
@Configuration
public class CoreConfig {
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
...
ServerAddress address = new
ServerAddress(EnvironmentParamManager.getMongoDBHost(),
EnvironmentParamManager.getMongoDBPort());
...
}
Run Code Online (Sandbox Code Playgroud)
当然,您可以自由地连接到任何其他服务,例如 LDAP 等,就像在 Spring Context 引导之前加载本地属性文件一样。希望有帮助。
| 归档时间: |
|
| 查看次数: |
1303 次 |
| 最近记录: |