SpringBoot封装starter组件
starter命名
- starter组件命名规则:
SpringBoot 官方的建议是,如果是我们开发者自己开发的 starter 组件(即属于第三方组件),那么命名规范是{name}-spring-boot-starter,而如果是 SpringBoot 官方自己开发的组件,则命名为 spring-boot-starter-{name}。
封装组件spyu-spring-boot-starter
maven项目结构

pom文件
代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sunpy</groupId>
<artifactId>spyu-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spyu-spring-boot-starter</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.3</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
util工具类
代码
public class PwdUtils {
/**
* AES加密
* @param content
* @return
*/
public static String encodeByAES(String content, byte[] aesKey) {
byte[] key = null;
// 随机生成密钥
if (!Objects.isNull(aesKey) && aesKey.length > 0) {
key = aesKey;
} else {
key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
}
// 构建
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
// 加密
String encryptHex = aes.encryptHex(content);
return encryptHex;
}
/**
* AES解密
* @param content
* @return
*/
public static String decodeByAES(String content, byte[] aesKey) {
byte[] key = null;
// 随机生成密钥
if (!Objects.isNull(aesKey) && aesKey.length > 0) {
key = aesKey;
} else {
key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
}
// 构建
SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);
// 解密
String decryptStr = aes.decryptStr(content, CharsetUtil.CHARSET_UTF_8);
return decryptStr;
}
}
service服务
代码
/**
* 加密解密服务
*/
public class PwdService {
public String encodeStrByAES(String str, byte[] key) {
return PwdUtils.encodeByAES(str, key);
}
public String decodeStrByAES(String str, byte[] key) {
return PwdUtils.decodeByAES(str, key);
}
}
定义自动装载入口
代码
@Configuration
public class SpyuAutoConfiguration {
@Bean
public PwdService getPwdService() {
PwdService pwdService = new PwdService();
return pwdService;
}
}
配置spring.factories文件,让springboot自动装载
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sunpy.spring.boot.autoconfigure.SpyuAutoConfiguration
引入后,可以使用maven打成jar包
使用组件spyu-spring-boot-starter
引入jar包
<dependency>
<groupId>com.sunpy</groupId>
<artifactId>spyu-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
使用加密服务PwdService
@Service
public class MenuServiceImpl {
private Map<String, byte[]> keyMap;
@Autowired
private PwdService pwdService;
@PostConstruct
public void initKey() {
keyMap = new HashMap<>();
byte[] keyArr = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
keyMap.put("key", keyArr);
}
public String encodeStr(String str) {
return pwdService.encodeStrByAES(str, keyMap.get("key"));
}
public String decodeStr(String str) {
return pwdService.decodeStrByAES(str, keyMap.get("key"));
}
}
测试
- 加密

- 解密


浙公网安备 33010602011771号