gpt4 book ai didi

java - Spring Security的ACL配置问题

转载 作者:搜寻专家 更新时间:2023-11-01 03:49:32 24 4
gpt4 key购买 nike

我是 Spring 世界的新手,我正在使用 Spring Boot 1.2.5 和 Spring Security 3.1.2。由于我的项目要求,我需要配置一个 ACL 安全模型。我有以下 java 类配置:

@Configuration
public class ACLConfig {

@Autowired
DataSource dataSource;


@Bean
JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource);
}

@Bean
DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource);
}

@Bean
EhCacheBasedAclCache aclCache() {
EhCacheFactoryBean factoryBean = new EhCacheFactoryBean();
EhCacheManagerFactoryBean cacheManager = new EhCacheManagerFactoryBean();
cacheManager.setAcceptExisting(true);
cacheManager.setCacheManagerName(CacheManager.getInstance().getName());
cacheManager.afterPropertiesSet();

factoryBean.setName("aclCache");
factoryBean.setCacheManager(cacheManager.getObject());
factoryBean.setMaxBytesLocalHeap("16M");
factoryBean.setMaxEntriesLocalHeap(0L);
factoryBean.afterPropertiesSet();
return new EhCacheBasedAclCache(factoryBean.getObject());
}

@Bean
LookupStrategy lookupStrategy() {
return new BasicLookupStrategy(dataSource, aclCache(), aclAuthorizationStrategy(), new ConsoleAuditLogger());
}

@Bean
AclAuthorizationStrategy aclAuthorizationStrategy() {
return new AclAuthorizationStrategyImpl(new SimpleGrantedAuthority("ROLE_SUPER_ADMIN"),
new SimpleGrantedAuthority("ROLE_SUPER_ADMIN"),
new SimpleGrantedAuthority("ROLE_SUPER_ADMIN"));
}

@Bean
JdbcMutableAclService aclService() {
JdbcMutableAclService service = new JdbcMutableAclService(dataSource, lookupStrategy(), aclCache());
service.setClassIdentityQuery("select currval(pg_get_serial_sequence('acl_class', 'id'))");
service.setSidIdentityQuery("select currval(pg_get_serial_sequence('acl_sid', 'id'))");
return service;
}

@Bean
AclEntryVoter aclDeleteVoter()
{
AclEntryVoter voter = new AclEntryVoter(aclService(),"ACL_NOMCITY_DELETE", new Permission[] {BasePermission.DELETE});
voter.setProcessDomainObjectClass(NomCity.class);
return voter;
}

@Bean
AclEntryVoter aclUpdateVoter()
{
return new AclEntryVoter(aclService(),"ACL_NOMCITY_UPDATE", new Permission[]{BasePermission.ADMINISTRATION});
}

@Bean
AclEntryVoter aclReadVoter()
{
return new AclEntryVoter(aclService(),"ACL_NOMCITY_READ", new Permission[]{BasePermission.READ});
}

@Bean
AccessDecisionManager accessDecisionManager (){

List<AccessDecisionVoter<? extends Object>> list = new ArrayList<>();
list.add(aclDeleteVoter());
list.add(aclReadVoter());
list.add(aclUpdateVoter());
return new AffirmativeBased(list);
}

}

我有以下 RestController 的方法,它使用前面定义的 ACL:

@RequestMapping(value = "/nomCitys",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@Transactional
@Secured({"ROLE_ADMIN","ROLE_USER"})
public ResponseEntity<NomCity> create(@Valid @RequestBody NomCity nomCity) throws URISyntaxException {

NomCity result = nomCityRepository.save(nomCity);

User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
ObjectIdentity oi = new ObjectIdentityImpl(NomCity.class,result.hashCode());
MutableAcl acl = mutableAclService.createAcl(oi);

acl.insertAce(0, BasePermission.ADMINISTRATION, new GrantedAuthoritySid("ROLE_ADMIN"), true);
acl.insertAce(1, BasePermission.DELETE, new PrincipalSid(user.getUsername()), true);
acl.insertAce(2, BasePermission.READ, new GrantedAuthoritySid("ROLE_USER"), true);

mutableAclService.updateAcl(acl);

return ResponseEntity.created(new URI("/api/nomCitys/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert("nomCity", result.getId().toString()))
.body(result);
}

当我创建一个新城市时,也会创建以下 ACL 条目:

  • 具有 ROLE_ADMIN 角色的用户具有管理员权限。
  • 创建城市的用户有删除权限。
  • 具有 ROLE_USER 角色的用户可以读取城市。

下面的方法是删除方法:

@RequestMapping(value = "/nomCitys/{id}",
method = RequestMethod.DELETE,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
@Transactional
@Secured("ACL_NOMCITY_DELETE")
public ResponseEntity<Void> delete(@PathVariable Long id) {

nomCityRepository.delete(id);

ObjectIdentity oid = new ObjectIdentityImpl(NomCity.class,id);
mutableAclService.deleteAcl(oid, true);

return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("nomCity", id.toString())).build();
}

当我创建一个新城市时,一切正常,ACL 条目被创建并存储在数据库中,但是当我去删除一个城市时,我得到一个 403,尽管我正在使用创建该城市的用户登录,查看一些页面我看到了以下 xml 条目:

<security:global-method-security
secured-annotations="enabled" access-decision-manager ref="customAccessDecisionManager" />

我想它注册了 AccessDecisionManager,但我不知道如何使用 Java Config 做同样的事情,如果这是我所有问题的原因,我也不知道。

最佳答案

这个问题是关于@secure 注释的,但我最终解决了这个问题,为使用@Pre 和@Post 注释创建了一个类配置,我在这个 question 的答案中发布了一个配置 java 类。 .

关于java - Spring Security的ACL配置问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32209484/

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