gpt4 book ai didi

java - JdbcMutableAclService - 事务必须正在运行

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:09 25 4
gpt4 key购买 nike

我正在尝试将 spring security acl 实现到一个项目中。在构建主要配置部分并创建相应的数据库模式之后,我正在尝试创建一些 ACE 并让奇迹发生。但是我一次又一次地面对这个异常

java.lang.IllegalArgumentException: Transaction must be running
org.springframework.util.Assert.isTrue(Assert.java:65)
org.springframework.security.acls.jdbc.JdbcMutableAclService.createOrRetrieveSidPrimaryKey(JdbcMutableAclService.java:219)
org.springframework.security.acls.jdbc.JdbcMutableAclService$1.setValues(JdbcMutableAclService.java:136)
org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:892)
org.springframework.jdbc.core.JdbcTemplate$4.doInPreparedStatement(JdbcTemplate.java:1)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:614)
org.springframework.jdbc.core.JdbcTemplate.batchUpdate(JdbcTemplate.java:883)
org.springframework.security.acls.jdbc.JdbcMutableAclService.createEntries(JdbcMutableAclService.java:123)
org.springframework.security.acls.jdbc.JdbcMutableAclService.updateAcl(JdbcMutableAclService.java:314)

我的基本配置部分

<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" >
<property name="driverClassName" value="${core.db.driverClassName}" />
<property name="url" value="${core.db.jdbcUrl}" />
<property name="username" value="${core.db.user}" />
<property name="password" value="${core.db.password}" />
</bean>

<bean id="aclCache"
class="org.springframework.security.acls.domain.EhCacheBasedAclCache">
<constructor-arg>
<bean class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
</property>
<property name="cacheName" value="aclCache" />
</bean>
</constructor-arg>
</bean>

<bean id="auditLogger"
class="org.springframework.security.acls.domain.ConsoleAuditLogger" />

<bean id="aclAuthorizationStrategy"
class="org.springframework.security.acls.domain.AclAuthorizationStrategyImpl">
<constructor-arg name="auths">
<list>
<bean
class="org.springframework.security.core.authority.GrantedAuthorityImpl">
<constructor-arg value="ACL_ADMIN" />
</bean>
<bean
class="org.springframework.security.core.authority.GrantedAuthorityImpl">
<constructor-arg value="ACL_ADMIN" />
</bean>
<bean
class="org.springframework.security.core.authority.GrantedAuthorityImpl">
<constructor-arg value="ACL_ADMIN" />
</bean>
</list>
</constructor-arg>
</bean>

<bean id="lookupStrategy" class="org.springframework.security.acls.jdbc.BasicLookupStrategy">
<constructor-arg name="dataSource" ref="dataSource"/>
<constructor-arg name="aclCache" ref="aclCache"/>
<constructor-arg name="aclAuthorizationStrategy" ref="aclAuthorizationStrategy"/>
<constructor-arg name="auditLogger" ref="auditLogger"/>
</bean>

<bean id="aclService"
class="org.springframework.security.acls.jdbc.JdbcMutableAclService" >
<constructor-arg name="dataSource" ref="dataSource" />
<constructor-arg name="lookupStrategy" ref="lookupStrategy" />
<constructor-arg name="aclCache" ref="aclCache" />
<property name="sidIdentityQuery" value="SELECT id FROM acl_sid" />
<property name="classIdentityQuery" value="SELECT id FROM acl_class" />
</bean>

<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref local="jdbcTransactionManager"/></property>
<property name="target"><ref local="aclService" /></property>
<property name="transactionAttributes">
<props>
<prop key="create*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

似乎我遗漏了什么,因为交易应该通过 TransactionProxy 激活。

以这种方式访问​​ Controller 中的服务

...
ObjectIdentity oi = new ObjectIdentityImpl(X.class, vm.hashCode());
Sid sid = new PrincipalSid(userDn);
Permission p = BasePermission.READ;

// Create or update the relevant ACL
MutableAcl acl = null;
try {
acl = (MutableAcl) aclService.readAclById(oi);
} catch (NotFoundException nfe) {
acl = aclService.createAcl(oi);
}

// Now grant some permissions via an access control entry (ACE)
acl.insertAce(acl.getEntries().size(), p, sid, true);
aclService.updateAcl(acl);
...

最佳答案

尝试用事务模板覆盖调用 aclService 的方法:

TransactionTemplate tt = new TransactionTemplate(transactionManager);
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
ObjectIdentity oid = new ObjectId
entityImpl(clazz.getCanonicalName(), securedObject.getId());
// your aclService operation here:
aclService.deleteAcl(oid, true);
}
});

关于java - JdbcMutableAclService - 事务必须正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5714046/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com