Spring Boot 自定义配置元数据

Spring Boot 自定义配置元数据

概述

开发Spring Boot应用程序时, 可以把 application.properties文件中的配置属性直接转换为 Java Bean对象。这也是Spring Boot 约定大于配置的理念的体现,通过框架内置的各种属性按照不同的需求进行排列组合,满足特定的业务规则。

server.port=8090

上述配置,相信大家都很熟悉,Spring Boot中默认端口为 8080,可以上面配置将端口更改为 8090. 仔细思考一下,这里有几个细节值得学习:

  • 怎么样找到 Spring Boot 中所有的配置元数据
  • Spring Boot 中的配置元数据格式是怎么样的
  • 如何实现自定义的配置元数据

配置元数据

通常情况下,开发者期望应用程序在一定程度上可以配置,提高程序的灵活性。然而,很少有人能够真正的了解配置参数的作用,是否存在默认值,配置是否过期,属性名是否存在。

为了解决以上问题,Spring Boot 使用JSON文件生成配置元数据,为开发者提供了如何使用配置属性的有用信息。因此,配置元数据是一个描述性文件,其中包含了与配置属性相关的所需信息。

配置格式

使用Spring Boot 开发Web程序时,通常会直接依赖或者间接依赖各种jar文件,Spring Boot 2.7.5 版本为例,配置元数据位于spring-boot-autoconfig-2.7.5.jar里面的 META-INF/spring-configuration-metadata.json,

在这里插入图片描述

格式如下:

{"groups": [
    {
        "name": "server",
        "type": "org.springframework.boot.autoconfigure.web.ServerProperties",
        "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
    }
    ...
],"properties": [
    {
        "name": "server.port",
        "type": "java.lang.Integer",
        "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
    }
    ...
],"hints": [
    {
        "name": "spring.jpa.hibernate.ddl-auto",
        "values": [
            {
                "value": "none",
                "description": "Disable DDL handling."
            }
        ]
    }
]}

元数据文件中的每一个 property 可以指定一个默认的配置,如 server.port 的默认值为 8080。因此当开发者不配置 server.port 属性时,应用程序默认占用的端口为8080.

groups 是更高级别的项,它们本身并不指定属性值,而是为属性提供上下文分组。例如,server 分组,server.port 、server.address,端口、地址都是属于server组的一部分。

hints 属性经常用于帮助用户配置属性做出的提示。

Group 分组

NameTypePurpose
nameStringgroup分组名称,该属性必填
typeStringgroup的全类名(包含package)。例如,如果组基于用@ConfigurationProperties注释的类,则该属性将包含该类的完全限定名称。如果它基于@Bean方法,那么它将是该方法的返回类型。
descriptionStringGroup的简短描述。如果没有可用的描述,可以省略。建议说明采用简短的段落,第一行提供简洁的摘要。
sourceTypeStringgroup的全类名(包含package)。例如,如果组基于用@ConfigurationProperties注释的@Bean方法,则该属性将包含包含该方法的@Configuration类的完全限定名称。如果源类型未知,则可以省略该属性。
sourceMethodString方法全名称(包括括号和参数类型)(例如,@ConfigurationProperties注释@Bean方法的名称)。如果源方法未知,则可以省略。

Property Attributes

NameTypePurpose
nameString属性的全名。名称采用小写句点分隔形式(例如,server.address)。此属性是必需的。
typeString属性数据类型的完整签名(例如java.lang.String),也可以是完整的泛型类型(例如java.util.Map<java.lang.Sstring,com.example.MyEnum>)。您可以使用此属性指导用户输入的值类型。为了保持一致性,原语的类型通过使用其包装器对应项来指定(例如,布尔值变为java.lang.boolean)。注意,这个类可能是一个复杂类型,在绑定值时从String转换而来。如果类型未知,则可以省略。
descriptionString可向用户展示的property简短描述。如果没有可用的描述,可以省略。建议说明采用简短的段落,第一行提供简洁的摘要。
sourceTypeString提供此属性的源的类名。例如,如果属性来自用@ConfigurationProperties注释的类,则该属性将包含该类的完全限定名称。如果源类型未知,则可以省略。
defaultValueObject默认值,如果未指定属性,则使用该值。如果属性的类型是数组,则它可以是值的数组。如果默认值未知,则可以省略。
deprecationDeprecation指定该属性是否已弃用。如果该字段未被弃用或该信息未知,则可以省略该字段。

Hint Attributes

NameTypePurpose
nameString提示引用的属性的全名。名称采用小写句点分隔形式(如spring.mvc.servlet.path)。如果属性引用映射(例如system.contexts),则提示应用于映射的键(system.contents.keys)或映射的值(system.coontexts.values)。此属性是必需的。
valuesValueHint[]ValueHint对象定义的有效值列表(在下表中描述)。每个条目都定义了值,并且可能有描述。
providersValueProvider[]ValueProvider对象定义的提供者列表。

每个提示元素的值属性中包含的JSON对象可以包含下表中描述的属性

NameTypePurpose
valueObject提示引用的元素的有效值。如果属性的类型是数组,它也可以是值的数组。此属性是必需的。
descriptionString可向用户显示的值的简短描述。如果没有可用的描述,可以省略。建议说明采用简短的段落,第一行提供简洁的摘要。说明中的最后一行应以句点(.)结尾。

上面简单的介绍了配置元数据相关信息,一个关键点是JSON 格式的元数据文件是如何生成的?

Maven依赖

为了实现自动生成配置元数据文件,需要添加Maven依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>2.7.5</version>
    <optional>true</optional>
</dependency>

该依赖让开发者在编译项目时,调用 Java 配置注解处理器。

配置元数据-Example

开发一个简单的Java Bean对象,并提供Get/Set方法

@Configuration
@ConfigurationProperties(prefix = "database")
public class DatabaseProperties {
	
    public static class Server {

        private String ip;
        private int port;

        // standard getters and setters
    }
	
    private String username;
    private String password;
    private Server server;
	
    // standard getters and setters
}

在类上使用 @ConfigurationProperties 声明,配置处理器会扫描使用该注解的类、方法,获取配置参数并生成配置元数据

生成配置元数据

使用maven 编译项目(Idea 工具自动生成),在 target/classes/META-INF目录下自动生成 spring-configuration-metadata.json 元数据配置文件

# 1. 删除 target
rm -rf target/

# 2. mvn 编译
mvn clean install -Dmaven.test.skip=true

在这里插入图片描述

spring-configuration-metadata.json文件内容如下

{
  "groups": [
    {
      "name": "database",
      "type": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties",
      "sourceType": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties"
    },
    {
      "name": "database.server",
      "type": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties$Server",
      "sourceType": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties",
      "sourceMethod": "getServer()"
    }
  ],
  "properties": [
    {
      "name": "database.password",
      "type": "java.lang.String",
      "sourceType": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties"
    },
    {
      "name": "database.server.ip",
      "type": "java.lang.String",
      "sourceType": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties$Server"
    },
    {
      "name": "database.server.port",
      "type": "java.lang.Integer",
      "sourceType": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties$Server",
      "defaultValue": 0
    },
    {
      "name": "database.username",
      "type": "java.lang.String",
      "sourceType": "com.springboot.study.springbootstudy.autoconfig.DatabaseProperties"
    }
  ],
  "hints": []
}

代码验证

  • 在application.properties 文件新增配置

    server.port=8090
    database.username=mysql
    
  • Spring Boot 启动类中 输出配置

    package com.springboot.study.springbootstudy;
    
    import com.springboot.study.springbootstudy.autoconfig.DatabaseProperties;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.web.ServerProperties;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class SpringbootStudyApplication {
    
    	public static void main(String[] args) {
    		ConfigurableApplicationContext app = SpringApplication.run(SpringbootStudyApplication.class, args);
    		DatabaseProperties bean = app.getBean(DatabaseProperties.class);
    		ServerProperties server = app.getBean(ServerProperties.class);
    
    		System.out.println("通过配置属性获取  =================: " + app.getEnvironment().getProperty("database.username"));
    		System.out.println("通过bean属性获取 =================: " + bean.getUsername());
    		System.out.println("Spring Boot 内置属性装配 =================: " + server.getPort());
    	}
    }
    
  • 输出结果

通过配置属性获取  =================: mysql
通过bean属性获取 =================: mysql
Spring Boot 内置属性装配 =================: 8090

从输出结果可以分析出 无论是通过 Environment 还是通过bean都能够获取配置的值,且跟框架内置的属性配置方式一致,预期输出也一致。

  • 项目结构

    ├── HELP.md
    ├── mvnw
    ├── mvnw.cmd
    ├── pom.xml
    ├── springboot-study.iml
    ├── src
    │   ├── main
    │   │   ├── java
    │   │   │   └── com
    │   │   │       └── springboot
    │   │   │           └── study
    │   │   │               └── springbootstudy
    │   │   │                   ├── SpringbootStudyApplication.java
    │   │   │                   └── autoconfig
    │   │   │                       └── DatabaseProperties.java
    │   │   └── resources
    │   │       ├── application.properties
    
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要生成Spring Boot SAML SP(服务提供者)元数据,您可以使用以下步骤: 1. 添加spring-security-saml2-core依赖 在Maven或Gradle项目中添加以下依赖: Maven: ``` <dependency> <groupId>org.springframework.security.extensions</groupId> <artifactId>spring-security-saml2-core</artifactId> <version>1.0.10.RELEASE</version> </dependency> ``` Gradle: ``` implementation 'org.springframework.security.extensions:spring-security-saml2-core:1.0.10.RELEASE' ``` 2. 创建SAML SP配置类 创建一个Java类,例如"SAMLSecurityConfig",并添加以下注释: ``` @Configuration @EnableWebSecurity public class SAMLSecurityConfig extends WebSecurityConfigurerAdapter { // ... } ``` 在此类中,您需要配置Spring Security SAML的各种组件,例如SAML身份提供者和SAML服务提供者。您可以参考Spring Security SAML文档以获取更多详细信息。 3. 添加SAML SP元数据生成器 添加以下bean定义以启用SAML SP元数据生成器: ``` @Bean public SAMLConfigurer saml() { return new SAMLConfigurer(); } ``` 然后,您可以使用以下代码生成元数据: ``` @Bean public SAMLConfigurer.EndpointConfigurer samlSPServiceConfigurer() throws Exception { // ... return saml().sso().defaultSuccessURL("/home").and(); } @Bean public SAMLConfigurer.MetaDataGenerator samlSPMetadataGenerator() { SAMLConfigurer.MetaDataGenerator metaDataGenerator = new SAMLConfigurer.MetaDataGenerator(); metaDataGenerator.setEntityId("http://localhost:8080/saml/metadata"); metaDataGenerator.setBindingsSLO(Arrays.asList("redirect", "post")); metaDataGenerator.setBindingsSSO(Arrays.asList("redirect", "post")); metaDataGenerator.setEntityBaseURL("http://localhost:8080"); metaDataGenerator.setIncludeDiscoveryExtension(false); metaDataGenerator.setExtendedMetadataDelegate(samlSPExtendedMetadataDelegate()); return metaDataGenerator; } ``` 在此示例中,您需要提供实体ID,绑定SLO(单点注销)和SSO(单点登录)以及实体基本URL。您还可以为元数据生成器设置其他选项,例如扩展元数据委托。 4. 配置SAML SP安全性 最后,您需要配置SAML SP的安全性。例如,以下代码可确保只有经过身份验证的用户可以访问SAML SP: ``` @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/saml/**").authenticated() .anyRequest().permitAll(); } ``` 这是一个基本的Spring Boot SAML SP元数据生成的示例。您可以根据您的需求进行自定义配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值