[code="java"]
public class SpringSecurityMetaDataSource implements FilterInvocationSecurityMetadataSource {
//@Resource(name = "resourceDao")
private ResourceDao resourceDao;
private static Map<String, Collection<ConfigAttribute>> resourceMap = null;
@Autowired
public SpringSecurityMetaDataSource(ResourceDao resourceDao) {
this.resourceDao = resourceDao;
loadResourceDefine();
}
}
/**
* 加载所有资源与权限的关系
*/
@SuppressWarnings("unused")
private void loadResourceDefine() {
if(resourceMap == null) {
resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
List<ResourceManager> resources = this.resourceDao.listAll(ResourceManager.class);
for (ResourceManager resource : resources) {
Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
//以权限名封装为Spring的security Object
ConfigAttribute configAttribute = new SecurityConfig(resource.getName());
configAttributes.add(configAttribute);
resourceMap.put(resource.getUrl(), configAttributes);
}
}
Set<Entry<String, Collection<ConfigAttribute>>> resourceSet = resourceMap.entrySet();
Iterator<Entry<String, Collection<ConfigAttribute>>> iterator = resourceSet.iterator();
}
[/code]
断点跟进去后发现:
List resources = this.resourceDao.listAll(ResourceManager.class);
是这抛出的错,报Caused by: org.hibernate.HibernateException: No Session found for current thread
我怀疑 通过构造方法注入的resourceDao不受hibernate的事务管理,
因此上诉代码改为了:
[code="java"]
@Resource(name = "resourceDao")
private ResourceDao resourceDao;
private static Map<String, Collection<ConfigAttribute>> resourceMap = null;
@Autowired
public SpringSecurityMetaDataSource() {
loadResourceDefine();
}
[/code]
这样的话,又会报resourceDao为空指针错,
详见代码附件。
jar包如下:
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjrt.jar
aspectjweaver.jar
commons-fileupload-1.3.jar
commons-logging-1.1.2.jar
dom4j-1.6.1.jar
druid-0.2.12.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.2.0.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jta-1.1.jar
mysql-connector-java-5.1.21.jar
spring-aop-3.2.2.RELEASE.jar
spring-aspects-3.2.2.RELEASE.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar
spring-jdbc-3.2.2.RELEASE.jar
spring-orm-3.2.2.RELEASE.jar
spring-security-config-3.2.0.M1.jar
spring-security-core-3.2.0.M1.jar
spring-security-web-3.2.0.M1.jar
spring-tx-3.2.2.RELEASE.jar
spring-web-3.2.2.RELEASE.jar
spring-webmvc-3.2.2.RELEASE.jar