java_wxp 2013-04-30 12:28
浏览 209
已采纳

spring3.2+hibernate4.2 整合 spring security报No Session found for current thread

[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

  • 写回答

4条回答 默认 最新

  • jinnianshilongnian 2013-04-30 13:16
    关注

    这样的话,又会报resourceDao为空指针错, -------->


    <!-- 鍙涓氬姟閫昏緫灞傚疄鏂戒簨鍔?-->


    /aop:config

    <aop:config expose-proxy="true" proxy-target-class="true" />
    

    -------------改为--------->


    <!-- 鍙涓氬姟閫昏緫灞傚疄鏂戒簨鍔?-->


    /aop:config

    试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题