栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Boot中的ACL安全性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot中的ACL安全性

找不到数据的原因有点棘手。

MethodSecurityexpressionHandler
在配置中定义bean后,数据库表中就没有数据。这是因为您的
data.sql
文件未执行。

在解释为什么

data.sql
不执行之前,我首先要指出您没有按预期使用该文件。

data.sql
在初始化hibernate状态后由spring-
boot执行,并且通常仅包含DML语句。您
data.sql
包含DDL(模式)语句和DML(数据)语句。这是不理想的,因为您的某些DDL语句与hibernate的
hibernate.hbm2ddl.auto
行为发生冲突(请注意,在使用嵌入式时,spring-
boot使用“ create-drop”
DataSource
)。您应该将DDL语句放入,
schema.sql
并将DML语句放入
data.sql
。手动定义所有表时,您应该禁用
hibernate.hbm2ddl.auto
(添加
spring.jpa.hibernate.ddl-auto=none
applciation.properties
)。

话虽如此,让我们看一下为什么

data.sql
不执行。

的执行

data.sql
是通过触发的,通过
ApplicationEvent
触发的
BeanPostProcessor
。这
BeanPostProcessor
DataSourceInitializedPublisher
)作为弹簧启动的hibernate/
JPA自动配置(见的一部分创建
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration

org.springframework.boot.autoconfigure.orm.jpa.DataSourceInitializedPublisher
org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer
)。

通常,

DataSourceInitializedPublisher
在创建(嵌入式)之前
DataSource
创建,并且一切都会按预期进行,但是通过定义自定义
MethodSecurityexpressionHandler
,常规bean的创建顺序会改变。按照配置
@EnableGlobalMethodSecurity
,您将自动导入
GlobalMethodSecurityConfiguration

与spring安全相关的bean是在早期创建的。由于您

MethodSecurityexpressionHandler
需要
DataSource
ACL内容,而与spring-
security相关的bean则需要您的custom
MethodSecurityexpressionHandler
,因此
DataSource
它的创建要比平时更早;实际上,它创建得太早了,
DataSourceInitializedPublisher
还没有创建spring-
boot
。在
DataSourceInitializedPublisher
以后创建的,但因为它没有注意到的创建
DataSource
豆,它也不会触发的执行
data.sql

长话短说:安全性配置会更改正常的Bean创建顺序,从而导致

data.sql
无法加载。

我猜想固定Bean的创建顺序就可以解决问题,但是由于我现在不怎么做(无需进一步实验),我提出了以下解决方案:手动定义您

DataSource
的数据并进行数据初始化。

@Configurationpublic class DataSourceConfig {    @Bean    public EmbeddedDatabase dataSource() {        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)      //as your data.sql file contains both DDL & DML you might want to rename it (e.g. init.sql)     .addscript("classpath:/data.sql")     .build();    }}

由于data.sql文件包含应用程序所需的所有DDL,因此可以将其禁用

hibernate.hbm2ddl.auto
。添加
spring.jpa.hibernate.ddl-auto=none
applciation.properties

定义自己的

DataSource
弹簧靴时,
DataSourceAutoConfiguration
通常会退出,但是如果您想确保也可以将其排除(可选)。

@SpringBootConfiguration@EnableAutoConfiguration(exclude = DataSourceAutoConfiguration.class)@ComponentScan@EnableCachingpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

这应该可以解决您的“无数据”问题。但是,要使所有功能按预期工作,您需要再进行2次修改。

首先,您应该只定义一个

MethodSecurityexpressionHandler
bean。当前,您正在定义2个
MethodSecurityexpressionHandler
bean。Spring-
security不知道要使用哪个安全性,而是(静静地)使用它自己的内部安全性
MethodSecurityexpressionHandler
。请参阅
org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration#setMethodSecurityexpressionHandler

@Configuration@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)public class MyACLConfig {    //...    @Bean    public MethodSecurityexpressionHandler createexpressionHandler() {        DefaultMethodSecurityexpressionHandler securityexpressionHandler = new DefaultMethodSecurityexpressionHandler();        securityexpressionHandler.setPermissionevaluator(new AclPermissionevaluator(aclService()));        securityexpressionHandler.setPermissionCacheOptimizer(new AclPermissionCacheOptimizer(aclService()));        return securityexpressionHandler;    }}

您需要做的最后一件事是使

getId()
Car中的方法公开。

@Entitypublic class Car {    //...        public long getId() {        return id;    }    //...}

ObjectIdentityRetrievalStrategy
当在ACL权限评估期间尝试确定对象的身份时,该标准将查找公共方法“ getId()”。

(请注意,我的回答基于

ACLConfig4
。)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/568716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号