我有一个名为的文件security.properties
,如下所示:
com.example.test.admins=username,username1,username2
我希望将该文件作为字符串数组读入。这实际上在一个包中有效:
package com.example.test.security
import org.springframework.beans.factory.annotation.Value
import org.springframework.ldap.core.DirContextOperations
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator
class CustomLdapAuthoritiesPopulator implements LdapAuthoritiesPopulator {
@Value('${com.example.test.admins}')
private String[] admins
@Override
public Collection<? extends GrantedAuthority> getGrantedAuthorities(
DirContextOperations userData, String username) {
def roles = [new SimpleGrantedAuthority("user")]
if (username in admins)
roles.add(new SimpleGrantedAuthority("admin"))
roles
}
}
我检查了每个导入,每个导入都被导入文件以外的东西使用。
在另一个包中,它忽略了字符串插值:
package com.example.test.controller
// imports excluded for brevity
@Controller
class UserController {
@Value('${com.example.test.admins}')
private String[] admins
public User get() {
def name = // Name gets put here
def admin = name in admins
println admins
println admin
return new User(name: name, admin: admin)
}
}
这会在控制台中产生以下输出:
[${com.example.test.admins}]
false
该文件的唯一提及是security-applicationContext.xml
<context:property-placeholder
location="classpath:security.properties"
ignore-resource-not-found="true"
ignore-unresolvable="true"/>
但是,将其复制到applicationContext.xml
并不会改变任何内容。