Я пытаюсь прочитать файл application-userroles.yml. Но, похоже, мое pojo не умеет читать файлы yml.
application.yml
spring:
profiles:
active: qa
spring.profiles.include: userroles
application-qa.yml
server:
port: 8090
servlet:
context-path: /order-manager
osm.service:
#qa url
nceurl: http://qa:80/OrderManagement/wsapi
ncwurl: http://qa2:80/OrderManagement/wsapi
userId: qa
password: qa123
logging.config: config/logback.xml
application-userroles.yml
osmtest:
clients:
- username: tos
password: tos123
roles: ADMIN
- username: tony
password: tony123
roles: USER
- username: sherry
password: sherry123
roles: USER
@Configuration
@ConfigurationProperties(prefix = "osmtest")
public class ApplicationClients {
private List<ApplicationClient> clients = new ArrayList<>();
public List<ApplicationClient> getClients() {
return clients;
}
public void setClients(List<ApplicationClient> clients) {
this.clients = clients;
}
}
@Getter
@Setter
public class ApplicationClient {
private String username;
private String password;
private String[] roles;
}
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
@EnableWebSecurity
@ConditionalOnWebApplication
@ConfigurationPropertiesScan("com.spectrum.sci.config")
@EnableConfigurationProperties(ApplicationClients.class)
@RequiredArgsConstructor
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{
private static final Logger log = LoggerFactory.getLogger(SecurityConfiguration.class);
private final ApplicationClients applicationClients = new ApplicationClients();
public void configure(HttpSecurity httpSecurity) throws Exception {
//httpSecurity.authorizeRequests().antMatchers(HttpMethod.GET,"/*").permitAll();
httpSecurity.formLogin().permitAll()
.and()
.authorizeRequests()
.antMatchers("/order/greet").hasAnyRole("USER", "ADMIN")
.antMatchers("/order").hasRole("USER")
.anyRequest().authenticated();
}
@Bean
public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
final InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
log.info("Importing {} clients: " , applicationClients.getClients().toString());
//Test purposes added bp user
ApplicationClient appClient = new ApplicationClient();
appClient.setUsername("bp");
appClient.setPassword("bp");
String[] roles = {"ADMIN", "USER"};
appClient.setRoles(roles);
applicationClients.getClients().add(appClient);
applicationClients.getClients().forEach(client -> {
manager.createUser(User.withDefaultPasswordEncoder()
.username(client.getUsername())
.password(client.getPassword())
.roles(client.getRoles())
.build());
log.info("Imported Clients {} " , client.getUsername() + "roles = " + client.getRoles().toString());
});
return manager;
}
}
Я попытался протестировать учетную запись bp, которую добавил в метод inMemoryUserDetailsManager, и он работает. Но он не читает файл application-userroles.yml. Я считаю, что я добавил каждую аннотацию, а также мой pom содержит artifactId spring-boot-configuration-processor, и я установил фильтрацию true для src / main / resources.