Skip to content

Commit

Permalink
✨ 添加 mica-nats 模块,待完善
Browse files Browse the repository at this point in the history
  • Loading branch information
li-xunhuan committed Aug 18, 2023
1 parent d8c62a4 commit dac1849
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
2 changes: 2 additions & 0 deletions mica-nats/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dependencies {
api project(":mica-core")
api "io.nats:jnats:2.16.13"
implementation "jakarta.validation:jakarta.validation-api"
implementation "org.springframework.boot:spring-boot-starter"
compileOnly "org.springframework.cloud:spring-cloud-context"
compileOnly "net.dreamlu:mica-auto:${micaAutoVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@
@Retention(RetentionPolicy.RUNTIME)
public @interface NatsListener {

/**
* subject
*
* @return subject
*/
String value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@

package net.dreamlu.mica.nats.config;

import io.nats.client.Connection;
import io.nats.client.ConnectionListener;
import io.nats.client.Nats;
import io.nats.client.Options;
import net.dreamlu.mica.core.utils.ResourceUtil;
import net.dreamlu.mica.core.utils.StringUtil;
import net.dreamlu.mica.nats.core.DefaultNatsTemplate;
import net.dreamlu.mica.nats.core.NatsTemplate;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.Resource;
import org.springframework.util.StringUtils;

import javax.net.ssl.*;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
Expand Down Expand Up @@ -93,6 +98,16 @@ public Options natsOptions(NatsProperties properties,
return builder.build();
}

@Bean
public Connection natsConnection(Options natsOptions) throws IOException, InterruptedException {
return Nats.connect(natsOptions);
}

@Bean
public NatsTemplate natsTemplate(Connection natsConnection) {
return new DefaultNatsTemplate(natsConnection);
}

private static SSLContext createSSLContext(NatsProperties properties) {
try {
return initSSLContext(properties);
Expand All @@ -104,7 +119,8 @@ private static SSLContext createSSLContext(NatsProperties properties) {
private static KeyStore loadKeystore(String path, char[] password, String keyStoreType)
throws IOException, GeneralSecurityException {
KeyStore store = KeyStore.getInstance(keyStoreType);
try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(path))) {
Resource resource = ResourceUtil.getResource(path);
try (BufferedInputStream in = new BufferedInputStream(resource.getInputStream())) {
store.load(in, password);
}
return store;
Expand All @@ -113,10 +129,10 @@ private static KeyStore loadKeystore(String path, char[] password, String keySto
private static KeyManager[] createKeyManagers(String path, String passwordStr,
String keyStoreProvider, String keyStoreType)
throws IOException, GeneralSecurityException {
if (!StringUtils.hasText(keyStoreProvider)) {
if (StringUtil.isBlank(keyStoreProvider)) {
keyStoreProvider = "SunX509";
}
if (!StringUtils.hasText(keyStoreProvider)) {
if (StringUtil.isBlank(keyStoreProvider)) {
keyStoreType = "PKCS12";
}
char[] password;
Expand All @@ -134,10 +150,10 @@ private static KeyManager[] createKeyManagers(String path, String passwordStr,
private static TrustManager[] createTrustManagers(String path, String passwordStr,
String trustStoreProvider, String trustStoreType)
throws IOException, GeneralSecurityException {
if (trustStoreProvider == null || trustStoreProvider.length() == 0) {
if (StringUtil.isBlank(trustStoreProvider)) {
trustStoreProvider = "SunX509";
}
if (trustStoreType == null || trustStoreType.length() == 0) {
if (StringUtil.isBlank(trustStoreType)) {
trustStoreType = "PKCS12";
}
char[] password;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.NotBlank;
import java.time.Duration;

/**
Expand All @@ -35,6 +37,7 @@
*/
@Getter
@Setter
@Validated
@RefreshScope
@ConfigurationProperties(NatsProperties.PREFIX)
public class NatsProperties {
Expand All @@ -43,6 +46,7 @@ public class NatsProperties {
/**
* nats服务器的URL,可以是 , 逗号分隔的列表。
*/
@NotBlank
private String server;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.dreamlu.mica.nats.core;

import io.nats.client.Connection;
import lombok.RequiredArgsConstructor;

/**
* 默认的 NatsTemplate
*
* @author L.cm
*/
@RequiredArgsConstructor
public class DefaultNatsTemplate implements NatsTemplate {
private final Connection connection;



}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package net.dreamlu.mica.xss.core;

import net.dreamlu.mica.core.utils.Charsets;
import net.dreamlu.mica.core.utils.StringUtil;
import net.dreamlu.mica.xss.config.MicaXssProperties;
import net.dreamlu.mica.xss.config.MicaXssProperties.Mode;
import net.dreamlu.mica.xss.utils.XssUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Entities;
import org.springframework.util.StringUtils;
import org.springframework.web.util.HtmlUtils;

import java.nio.charset.StandardCharsets;

/**
* 默认的 xss 清理器
*
Expand All @@ -49,13 +50,13 @@ private static Document.OutputSettings getOutputSettings(MicaXssProperties prope
@Override
public String clean(String name, String bodyHtml, XssType type) {
// 1. 为空直接返回
if (StringUtil.isBlank(bodyHtml)) {
if (!StringUtils.hasText(bodyHtml)) {
return bodyHtml;
}
Mode mode = properties.getMode();
if (Mode.ESCAPE == mode) {
// html 转义
return HtmlUtils.htmlEscape(bodyHtml, Charsets.UTF_8_NAME);
return HtmlUtils.htmlEscape(bodyHtml, StandardCharsets.UTF_8.name());
} else if (Mode.VALIDATE == mode) {
// 校验
if (Jsoup.isValid(bodyHtml, XssUtil.WHITE_LIST)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.auto.annotation.AutoIgnore;
import net.dreamlu.mica.core.utils.StringPool;
import net.dreamlu.mica.xss.config.MicaXssProperties;
import net.dreamlu.mica.xss.utils.XssUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -59,12 +58,6 @@ public static class StringPropertiesEditor extends PropertyEditorSupport {
private final XssCleaner xssCleaner;
private final MicaXssProperties properties;

@Override
public String getAsText() {
Object value = getValue();
return value != null ? value.toString() : StringPool.EMPTY;
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
if (text == null) {
Expand Down

0 comments on commit dac1849

Please sign in to comment.