0

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'

May12
  • 2,420
  • 12
  • 63
  • 99
  • You need to post your `SpumConfig.class`. We need to see your Spring configuration. – William Callahan Aug 21 '17 at 14:35
  • @William, I add it, but it is empty at the current moment. – May12 Aug 21 '17 at 14:42
  • 1
    You are sure that the archive you are building contains the properties file, right? – William Callahan Aug 21 '17 at 15:17
  • 1
    Maybe try one of the answers from this page: https://stackoverflow.com/questions/15937592/spring-value-is-not-resolving-to-value-from-property-file Add this to your configuration: `@Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); }` – William Callahan Aug 21 '17 at 15:18
  • @William, I am sure about placement of properties file: If I add `@Bean public String appProps() {log.info("test.db.url = " + environment.getProperty("test.db.url")); return null;}` the programm print out `test.db.url = jdbc:..` when run. – May12 Aug 22 '17 at 06:51
  • @William, also i add `PropertySourcesPlaceholderConfigurer` bean. – May12 Aug 22 '17 at 07:05
  • 1
    When you build the archive, where are the properties being placed? Are they in the classes folder? – William Callahan Aug 22 '17 at 18:38
  • Yes, properties in the root of jar. – May12 Aug 24 '17 at 06:39

1 Answers1

0

Quite possibly the jar is not picking up the right path of the property file to begin with. Please validate the path of the property file first.

Then use the spring' PropertySourcesPlaceholderConfigurer to add the right path. You can validate if your application picks up the property file in the server logs when you deploy your application.

Nilsaw
  • 103
  • 7
  • Nilsaw, I am sure that properties is picking up because there is info about it in the log `2017-08-24 09:45:04,848 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'accepted.username.pass1' in [class path resource [spectrum-dev.properties]] with type [String] 2017-08-24 09:45:04,848 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'accepted.username.pass1' in [environmentProperties] with type [String] 2017-08-24 09:45:04,849 [main] TRACE o.s.util.PropertyPlaceholderHelper - Resolved placeholder 'accepted.username.pass1'` – May12 Aug 24 '17 at 07:04