-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SpringBoot初始化模板v2.1.9 更新国际化模块,添加初始化字典以及ReturnCode枚举国际化方法,设计加密模块。
- Loading branch information
1 parent
1d6e42f
commit a02d37f
Showing
25 changed files
with
999 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/top/sharehome/springbootinittemplate/config/encrypt/EncryptConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package top.sharehome.springbootinittemplate.config.encrypt; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.context.annotation.Configuration; | ||
import top.sharehome.springbootinittemplate.config.encrypt.condition.EncryptCondition; | ||
import top.sharehome.springbootinittemplate.utils.encrypt.AESUtils; | ||
import top.sharehome.springbootinittemplate.utils.encrypt.RSAUtils; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.crypto.SecretKey; | ||
import java.security.KeyPair; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* 加密配置 | ||
* | ||
* @author AntonyCheng | ||
*/ | ||
@Configuration | ||
@Conditional({EncryptCondition.class}) | ||
@Slf4j | ||
public class EncryptConfiguration { | ||
|
||
public static String RSAPublicKey = null; | ||
|
||
public static String RSAPrivateKey = null; | ||
|
||
public static String AESSecretKey = null; | ||
|
||
static { | ||
CompletableFuture.runAsync(() -> { | ||
KeyPair keyPair = RSAUtils.generateKeyPair(4096); | ||
RSAPublicKey = RSAUtils.getStringFromPublicKey(keyPair.getPublic()); | ||
RSAPrivateKey = RSAUtils.getStringFromPrivateKey(keyPair.getPrivate()); | ||
SecretKey secretKey = AESUtils.generateKey(32); | ||
AESSecretKey = AESUtils.getStringFromKey(secretKey); | ||
log.info("############ Encryption Key is Generated"); | ||
}); | ||
} | ||
|
||
/** | ||
* 依赖注入日志输出 | ||
*/ | ||
@PostConstruct | ||
private void initDi() { | ||
log.info("############ {} Configuration DI.", this.getClass().getSimpleName().split("\\$\\$")[0]); | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/top/sharehome/springbootinittemplate/config/encrypt/annotation/RSADecrypt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package top.sharehome.springbootinittemplate.config.encrypt.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* 需要RSA算法解密的方法注解 | ||
* | ||
* @author AntonyCheng | ||
*/ | ||
@Inherited | ||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface RSADecrypt { | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/top/sharehome/springbootinittemplate/config/encrypt/annotation/RSAEncrypt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package top.sharehome.springbootinittemplate.config.encrypt.annotation; | ||
|
||
import java.lang.annotation.*; | ||
|
||
/** | ||
* 需要RSA算法加密的参数或字段注解 | ||
* | ||
* @author AntonyCheng | ||
*/ | ||
@Inherited | ||
@Target({ElementType.PARAMETER, ElementType.FIELD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface RSAEncrypt { | ||
} |
10 changes: 5 additions & 5 deletions
10
.../config/i18n/condition/I18nCondition.java → ...g/encrypt/condition/EncryptCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
package top.sharehome.springbootinittemplate.config.i18n.condition; | ||
package top.sharehome.springbootinittemplate.config.encrypt.condition; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.context.annotation.Condition; | ||
import org.springframework.context.annotation.ConditionContext; | ||
import org.springframework.core.type.AnnotatedTypeMetadata; | ||
|
||
/** | ||
* 国际化自定义配置条件 | ||
* 请求参数加密自定义配置条件 | ||
* | ||
* @author AntonyCheng | ||
*/ | ||
public class I18nCondition implements Condition { | ||
public class EncryptCondition implements Condition { | ||
|
||
@Override | ||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { | ||
String property = context.getEnvironment().getProperty("spring.messages.enable"); | ||
return StringUtils.equals(Boolean.TRUE.toString(), property); | ||
String encryptProperty = context.getEnvironment().getProperty("encrypt.enable"); | ||
return StringUtils.equals(Boolean.TRUE.toString(), encryptProperty); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...ava/top/sharehome/springbootinittemplate/config/encrypt/properties/EncryptProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package top.sharehome.springbootinittemplate.config.encrypt.properties; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* 请求参数加密配置 | ||
* | ||
* @author AntonyCheng | ||
*/ | ||
@Data | ||
@ConfigurationProperties(prefix = "encrypt") | ||
public class EncryptProperties { | ||
|
||
/** | ||
* 请求参数加密开关 | ||
*/ | ||
private Boolean enable = false; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 11 additions & 5 deletions
16
.../java/top/sharehome/springbootinittemplate/config/i18n/controller/I18nDemoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
package top.sharehome.springbootinittemplate.config.i18n.controller; | ||
|
||
import org.springframework.context.annotation.Conditional; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import top.sharehome.springbootinittemplate.common.base.R; | ||
import top.sharehome.springbootinittemplate.common.base.ReturnCode; | ||
import top.sharehome.springbootinittemplate.config.i18n.I18nManager; | ||
import top.sharehome.springbootinittemplate.config.i18n.condition.I18nCondition; | ||
import top.sharehome.springbootinittemplate.exception.customize.CustomizeReturnException; | ||
|
||
import java.util.Random; | ||
|
||
/** | ||
* 国际化示例控制器 | ||
* 国际化示例控制器(测试时) | ||
* | ||
* @author AntonyCheng | ||
*/ | ||
@RestController | ||
@Conditional(I18nCondition.class) | ||
public class I18nDemoController { | ||
|
||
@GetMapping("/i18n") | ||
public R<String> welcome(@RequestParam String name) { | ||
return R.ok(I18nManager.getMessage("msg_welcome", new String[]{name})); | ||
int seed = new Random().nextInt(); | ||
if (seed % 2 == 0) { | ||
throw new CustomizeReturnException(ReturnCode.ACCOUNT_AND_EMAIL_DO_NOT_MATCH.toI18n()); | ||
}else { | ||
return R.ok(I18nManager.getMessage("welcome", name)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.