手写starter之RSAStarter

手写starter

rsa-spring-boot-starter

starter开发步骤:
1.新建一个Maven工程,引入starter开发需要的依赖包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <version>3.0.5</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <version>3.2.3</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
    <version>3.2.0</version>
</dependency>
2.编写提供服务的类、服务的属性配置类、服务的自动装配类

先写属性配置类:

@Data
@ConfigurationProperties(prefix = "qf.rsa")
public class RsaProperties {
    /**
     * rsa加密使用的安全码
     */
    private String secure = "Java2024";

    /**
     * rsa加密使用的秘钥大小
     */
    private int keySize = 512;


}

再写提供服务的类:

public class RsaService {

    private final PublicKey publicKey;
    private final PrivateKey privateKey;


    public RsaService(RsaProperties properties) throws NoSuchAlgorithmException {
        KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(properties.getKeySize(),new SecureRandom(properties.getSecure().getBytes()));
        KeyPair keyPair = generator.genKeyPair();
        publicKey = keyPair.getPublic();
        privateKey = keyPair.getPrivate();
    }

    /**
     * 加密
     * @param source 待加密字符串
     * @return
     */
    public String encrypt(String source) {
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE,publicKey);
            byte[] bytes = cipher.doFinal(source.getBytes());
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 解密
     * @param encodeStr 待解密字符串
     * @return
     */
    public String decrypt(String encodeStr){
        try {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE,privateKey);
            byte[] bytes = Base64.getDecoder().decode(encodeStr);
            byte[] result = cipher.doFinal(bytes);
            return new String(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

然后写自动装配类:

@Configuration//这是一个用于自动配置的类
@EnableConfigurationProperties(RsaProperties.class)
@ConditionalOnClass(RsaService.class)//类路径下具有条件:存在RsaService类
public class RsaAutoConfig {   
    @Bean //当IOC容器中不存在RsaService的bean对象的时候,执行此方法创建对象放入ioc容器
    @ConditionalOnBean(RsaService.class)
    public RsaService rsaService(RsaProperties properties) throws NoSuchAlgorithmException {
        return new RsaService(properties);
    }
}

注意:当含有@Bean注解的方法中有参数,那么必须保证IOC容器中存在这个参数类型的bean对象,因为这里有@EnableConfigurationProperties注解,所以IOC容器中会创建RsaProperties对象。

3.在resources文件夹下新建一个META-INF文件夹,然后在这个文件夹下新建spring.factories文件,这个文件就是自动装配的配置文件。
org.springframework.boot.autoconfigure.EnableAutoConfiguration = com.qf.rsaStarter.config.RsaAutoConfig

4.使用Maven的功能对工程进行打包(Lifecycle->install)

5.在其他工程中引用starter进行测试。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单的示例,展示了如何手写一个Spring Boot Starter: 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.4</version> </dependency> </dependencies> ``` 接下来,创建一个自定义的自动配置类,用于配置你的 Starter: ```java @Configuration @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { private final MyStarterProperties properties; public MyStarterAutoConfiguration(MyStarterProperties properties) { this.properties = properties; } // 在此处定义你的自动配置逻辑 @Bean public MyStarterService myStarterService() { return new MyStarterService(properties); } } ``` 然后,创建一个属性类,用于将外部配置绑定到属性上: ```java @ConfigurationProperties("my.starter") public class MyStarterProperties { private String message; // 提供 getter 和 setter } ``` 最后,创建一个自定义的服务类,该服务类将在你的 Starter 中使用: ```java public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public void showMessage() { System.out.println(properties.getMessage()); } } ``` 现在,你的 Spring Boot Starter 已经准备就绪了!你可以将其打包并使用在其他 Spring Boot 项目中。在其他项目的 `pom.xml` 文件中,添加你的 Starter 依赖: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-starter</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` 然后,在你的应用程序中使用 `MyStarterService`: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyStarterService myStarterService; public MyApplication(MyStarterService myStarterService) { this.myStarterService = myStarterService; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myStarterService.showMessage(); } } ``` 这样,你就成功地创建了一个简单的 Spring Boot Starter!当其他项目引入你的 Starter 并运行时,将会输出预定义的消息。 当然,这只是一个简单的示例,真实的 Starter 可能包含更多的配置和功能。你可以根据自己的需求进行扩展和定制。希望对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值