Collegues, i have a bean:
@Component
public class BasicAuthAuthorizationInterceptor extends SoapHeaderInterceptor {
private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptor.class);
@Value("#{${accepted.username.pass1}}")
private Map<String,String> authMap;
@Override
public void handleMessage(Message message) throws Fault {
AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
if (policy == null) {
sendErrorResponse(message, HttpURLConnection.HTTP_UNAUTHORIZED);
return;
}
String username = policy.getUserName();
String password = policy.getPassword();
// CHECK USERNAME AND PASSWORD
if (!checkLogin(username,password)) {
sendErrorResponse(message, HttpURLConnection.HTTP_FORBIDDEN);
}
}
public boolean checkLogin(String username, String password) {
MapUtils.debugPrint(System.out, "Map: " , authMap);
if (authMap.containsKey(username.trim()) && password.trim().equals(authMap.get(username).trim())) {
return true;
}
return false;
}
//some other methods
}
When i run test case:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpumConfig.class)
public class BasicAuthAuthorizationInterceptorTest {
private static final Logger log = LoggerFactory.getLogger(BasicAuthAuthorizationInterceptorTest.class);
@Autowired
BasicAuthAuthorizationInterceptor basicAuthAuthorizationInterceptor;
@Test
public void handleMessage() throws Exception {
log.info(String.valueOf(basicAuthAuthorizationInterceptor.checkLogin("abc", "321")));
}}
I receive true
and MapUtils.debugPrint
method prints all keys and values:
Map: = { abc = 325 java.lang.String cda = 322 java.lang.String sss = Bas3 java.lang.String } java.util.Collections$UnmodifiableMap
But when i compile a jar file, run application and than call web service I recieve
Map: = null
Why does it happen and how correctly inject map into BasicAuthAuthorizationInterceptor
?
Update
@Configuration
@ComponentScan(basePackages = {"com.comp.spum"})
@PropertySource("classpath:spum-${env}.properties")
public class spumConfig {
private static final Logger log = LoggerFactory.getLogger(spumConfig.class);
@Autowired
Environment environment;
@Autowired
spumCommons spumCommons;
@Bean
public String appProps() {
log.info("test.db.url = " + environment.getProperty("test.db.url"));
return null;
}
@Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
{ return new PropertySourcesPlaceholderConfigurer();}
}
Update 2
According to log properties are resolving and basicAuthAuthorizationInterceptor
was created:
2017-08-22 10:25:00,573 [main] DEBUG o.s.b.f.annotation.InjectionMetadata - Processing injected element of bean 'basicAuthAuthorizationInterceptor': AutowiredFieldElement for private java.util.Map com.comp.spum.service.interceptors.BasicAuthAuthorizationInterceptor.authMap 2017-08-22 10:25:00,574 [main] TRACE o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'accepted.username.pass1' in [environmentProperties] 2017-08-22 10:25:00,574 [main] TRACE o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'accepted.username.pass1' in [systemProperties] 2017-08-22 10:25:00,574 [main] TRACE o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'accepted.username.pass1' in [systemEnvironment] 2017-08-22 10:25:00,574 [main] TRACE o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'accepted.username.pass1' in [class path resource [spum-dev.properties]] 2017-08-22 10:25:00,574 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'accepted.username.pass1' in [class path resource [spum-dev.properties]] with type [String] 2017-08-22 10:25:00,574 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'accepted.username.pass1' in [environmentProperties] with type [String] 2017-08-22 10:25:00,574 [main] TRACE o.s.util.PropertyPlaceholderHelper - Resolved placeholder 'accepted.username.pass1' 2017-08-22 10:25:00,595 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'basicAuthAuthorizationInterceptor'