【发布时间】:2017-08-21 14:25:01
【问题描述】:
同学们,我有一颗豆子:
@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
}
当我运行测试用例时:
@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")));
}}
我收到true 和MapUtils.debugPrint 方法打印所有键和值:
地图:= { abc = 325 java.lang.String cda = 322 java.lang.String sss = Bas3 java.lang.String } java.util.Collections$UnmodifiableMap
但是当我编译一个 jar 文件时,运行应用程序然后调用我收到的 web 服务
Map: = null
为什么会发生这种情况以及如何正确地将地图注入BasicAuthAuthorizationInterceptor?
更新
@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();}
}
更新 2
根据日志属性正在解析并创建basicAuthAuthorizationInterceptor:
2017-08-22 10:25:00,573 [主要] 调试 o.s.b.f.annotation.InjectionMetadata - 处理注入的元素 豆'basicAuthAuthorizationInterceptor':AutowiredFieldElement 私有 java.util.Map com.comp.spum.service.interceptors.BasicAuthAuthorizationInterceptor.authMap 2017-08-22 10:25:00,574 [主要] 跟踪 o.s.c.e.PropertySourcesPropertyResolver - 搜索密钥 [environmentProperties] 中的“accepted.username.pass1” 2017-08-22 10:25:00,574 [主] TRACE o.s.c.e.PropertySourcesPropertyResolver - 在 [systemProperties] 中搜索键 'accepted.username.pass1' 2017-08-22 10:25:00,574 [主要] 跟踪 o.s.c.e.PropertySourcesPropertyResolver - 搜索密钥 [systemEnvironment] 中的“accepted.username.pass1” 2017-08-22 10:25:00,574 [主] TRACE o.s.c.e.PropertySourcesPropertyResolver - 在 [class path 资源中搜索 key 'accepted.username.pass1' [spum-dev.properties]] 2017-08-22 10:25:00,574 [主要] 调试 o.s.c.e.PropertySourcesPropertyResolver - 找到密钥 [类路径资源中的“accepted.username.pass1” [spum-dev.properties]] 类型为 [String] 2017-08-22 10:25:00,574 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - 找到密钥 [environmentProperties] 中的“accepted.username.pass1”,类型为 [字符串] 2017-08-22 10:25:00,574 [主要] 跟踪 o.s.util.PropertyPlaceholderHelper - 已解决的占位符 'accepted.username.pass1' 2017-08-22 10:25:00,595 [主要] 调试 o.s.b.f.s.DefaultListableBeanFactory - 完成创建实例 bean 'basicAuthAuthorizationInterceptor'
【问题讨论】:
-
您需要发布您的
SpumConfig.class。我们需要查看您的 Spring 配置。 -
@William,我添加了它,但目前它是空的。
-
您确定您正在构建的存档包含属性文件,对吧?
-
也许试试这个页面的答案之一:stackoverflow.com/questions/15937592/… 将此添加到您的配置中:
@Bean public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } -
构建存档时,属性放在哪里?它们在类文件夹中吗?