Merge pull request #38 from lingting-gzm/master
✨ 添加消费者配置,bean 添加 ConditionalOnMissingBean 注解,用于用户自定义
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
package com.hccake.ballcat.common.conf.config;
|
||||
|
||||
import com.hccake.ballcat.common.core.filter.ActuatorFilter;
|
||||
import com.hccake.ballcat.common.core.filter.XSSFilter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/10/17 20:26
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication
|
||||
public class FilterConfig {
|
||||
|
||||
@Value("${monitor.secret-id:ballcat-monitor}")
|
||||
private String secretId;
|
||||
|
||||
@Value("${monitor.secret-key:=BallCat-Monitor}")
|
||||
private String secretKey;
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<XSSFilter> xssFilterRegistrationBean() {
|
||||
log.debug("XSS 过滤已开启====");
|
||||
FilterRegistrationBean<XSSFilter> registrationBean = new FilterRegistrationBean<>(new XSSFilter());
|
||||
registrationBean.setOrder(-1);
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<ActuatorFilter> actuatorFilterRegistrationBean() {
|
||||
log.debug("Actuator 安全过滤器已开启====");
|
||||
FilterRegistrationBean<ActuatorFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
ActuatorFilter actuatorFilter = new ActuatorFilter(secretId, secretKey);
|
||||
registrationBean.setFilter(actuatorFilter);
|
||||
registrationBean.addUrlPatterns("/actuator/*");
|
||||
registrationBean.setOrder(0);
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.hccake.ballcat.common.conf.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author lingting 2020-10-13 22:39
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "ballcat")
|
||||
public class MonitorProperties {
|
||||
|
||||
private Actuator actuator;
|
||||
|
||||
@Data
|
||||
public static class Actuator {
|
||||
|
||||
/**
|
||||
* 是否开启.
|
||||
*/
|
||||
private Boolean auth = false;
|
||||
|
||||
private String secretId;
|
||||
|
||||
private String secretKey;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.hccake.ballcat.common.conf.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author lingting 2020-10-13 22:39
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "ballcat.security")
|
||||
public class SecurityProperties {
|
||||
|
||||
private Xss xss;
|
||||
|
||||
@Data
|
||||
public static class Xss {
|
||||
|
||||
/**
|
||||
* 是否开启
|
||||
*/
|
||||
private Boolean enabled = true;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.hccake.ballcat.common.conf.web;
|
||||
|
||||
import com.hccake.ballcat.common.conf.config.MonitorProperties;
|
||||
import com.hccake.ballcat.common.core.filter.ActuatorAuthFilter;
|
||||
import com.hccake.ballcat.common.core.filter.XSSFilter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/10/17 20:26
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@ConditionalOnWebApplication
|
||||
public class FilterConfig {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "ballcat.security.xss", name = "enabled", havingValue = "true",
|
||||
matchIfMissing = true)
|
||||
public FilterRegistrationBean<XSSFilter> xssFilterRegistrationBean() {
|
||||
log.debug("XSS 过滤已开启====");
|
||||
FilterRegistrationBean<XSSFilter> registrationBean = new FilterRegistrationBean<>(new XSSFilter());
|
||||
registrationBean.setOrder(-1);
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "ballcat.actuator", name = "auth", havingValue = "true")
|
||||
public FilterRegistrationBean<ActuatorAuthFilter> actuatorFilterRegistrationBean(MonitorProperties properties) {
|
||||
log.debug("Actuator 过滤器已开启====");
|
||||
FilterRegistrationBean<ActuatorAuthFilter> registrationBean = new FilterRegistrationBean<>();
|
||||
MonitorProperties.Actuator actuator = properties.getActuator();
|
||||
if (actuator.getAuth()) {
|
||||
// 监控开启
|
||||
ActuatorAuthFilter filter = new ActuatorAuthFilter(actuator.getSecretId(), actuator.getSecretKey());
|
||||
registrationBean.setFilter(filter);
|
||||
registrationBean.addUrlPatterns("/actuator/*");
|
||||
registrationBean.setOrder(0);
|
||||
}
|
||||
|
||||
return registrationBean;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.hccake.ballcat.common.conf.config.FilterConfig,\
|
||||
com.hccake.ballcat.common.conf.web.FilterConfig,\
|
||||
com.hccake.ballcat.common.conf.config.JacksonConfig,\
|
||||
com.hccake.ballcat.common.conf.exception.ExceptionHandleAutoConfiguration,\
|
||||
com.hccake.ballcat.common.conf.mybatis.MybatisPlusConfig,\
|
||||
com.hccake.ballcat.common.conf.web.WebMvcConfig,\
|
||||
com.hccake.ballcat.common.conf.config.ExceptionHandleConfig
|
||||
com.hccake.ballcat.common.conf.config.ExceptionHandleConfig,\
|
||||
com.hccake.ballcat.common.conf.config.SecurityProperties,\
|
||||
com.hccake.ballcat.common.conf.config.MonitorProperties
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ import java.io.IOException;
|
||||
* @version 1.0
|
||||
* @date 2019 /10/17 20:28
|
||||
*/
|
||||
public class ActuatorFilter extends OncePerRequestFilter {
|
||||
public class ActuatorAuthFilter extends OncePerRequestFilter {
|
||||
|
||||
private final String secretId;
|
||||
|
||||
@@ -34,7 +34,7 @@ public class ActuatorFilter extends OncePerRequestFilter {
|
||||
* @param secretId the secret id
|
||||
* @param secretKey the secret key
|
||||
*/
|
||||
public ActuatorFilter(String secretId, String secretKey) {
|
||||
public ActuatorAuthFilter(String secretId, String secretKey) {
|
||||
this.secretId = secretId;
|
||||
this.secretKey = secretKey;
|
||||
}
|
||||
@@ -16,6 +16,11 @@ spring:
|
||||
ballcat:
|
||||
redis:
|
||||
key-prefix: 'ballcat:'
|
||||
# actuator 加解密密钥
|
||||
actuator:
|
||||
auth: true
|
||||
secret-id: 'ballcat-monitor'
|
||||
secret-key: '=BallCat-Monitor'
|
||||
|
||||
security:
|
||||
oauth2:
|
||||
@@ -55,12 +60,6 @@ mybatis-plus:
|
||||
password:
|
||||
secret-key: '==BallCat-Auth=='
|
||||
|
||||
# 监控加解密密钥
|
||||
monitor:
|
||||
secret-id: 'ballcat-monitor'
|
||||
secret-key: '=BallCat-Monitor'
|
||||
|
||||
|
||||
# 定时任务相关配置
|
||||
xxl:
|
||||
job:
|
||||
|
||||
@@ -2,11 +2,11 @@ package com.hccake.ballcat.monitor.security;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.hccake.ballcat.common.core.constant.HeaderConstants;
|
||||
import de.codecentric.boot.admin.server.domain.entities.Instance;
|
||||
import de.codecentric.boot.admin.server.web.client.HttpHeadersProvider;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
import de.codecentric.boot.admin.server.domain.entities.Instance;
|
||||
import de.codecentric.boot.admin.server.web.client.HttpHeadersProvider;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
@@ -16,30 +16,37 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class AuthHeaderProvider implements HttpHeadersProvider {
|
||||
|
||||
@Value("${monitor.secret-id:ballcat-monitor}")
|
||||
@Value("${ballcat.actuator.auth:false}")
|
||||
private Boolean auth;
|
||||
|
||||
@Value("${ballcat.actuator.secret-id:ballcat-monitor}")
|
||||
private String secretId;
|
||||
|
||||
@Value("${monitor.secret-key:=BallCat-Monitor}")
|
||||
@Value("${ballcat.actuator.secret-key:=BallCat-Monitor}")
|
||||
private String secretKey;
|
||||
|
||||
/**
|
||||
* 当授权中心剥离时,目前BallCat的设计是简单启动 所以不做独立的授权中心,在此记录下拓展使用方式 可以考虑 client_credentials 客户端授权模式
|
||||
*
|
||||
* @param instance
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public HttpHeaders getHeaders(Instance instance) {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
// 当前时间戳
|
||||
long reqTime = System.currentTimeMillis();
|
||||
headers.set(HeaderConstants.REQ_TIME, String.valueOf(reqTime));
|
||||
// 客户端ID
|
||||
headers.set(HeaderConstants.SECRET_ID, secretId);
|
||||
// sign
|
||||
String tempSign = new StringBuilder().append(reqTime).reverse().append(secretId).append(secretKey).toString();
|
||||
String sign = SecureUtil.md5(tempSign);
|
||||
headers.set(HeaderConstants.SIGN, sign);
|
||||
|
||||
if (auth) {
|
||||
// 当前时间戳
|
||||
long reqTime = System.currentTimeMillis();
|
||||
headers.set(HeaderConstants.REQ_TIME, String.valueOf(reqTime));
|
||||
// 客户端ID
|
||||
headers.set(HeaderConstants.SECRET_ID, secretId);
|
||||
// sign
|
||||
String tempSign =
|
||||
new StringBuilder().append(reqTime).reverse().append(secretId).append(secretKey).toString();
|
||||
String sign = SecureUtil.md5(tempSign);
|
||||
headers.set(HeaderConstants.SIGN, sign);
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,3 +20,10 @@ spring:
|
||||
monitor:
|
||||
secret-id: 'ballcat-monitor'
|
||||
secret-key: '=BallCat-Monitor'
|
||||
|
||||
ballcat:
|
||||
# actuator 加解密密钥
|
||||
actuator:
|
||||
auth: true
|
||||
secret-id: 'ballcat-monitor'
|
||||
secret-key: '=BallCat-Monitor'
|
||||
@@ -1,11 +1,14 @@
|
||||
package com.hccake.starter.kafka;
|
||||
|
||||
import com.hccake.extend.kafka.KafkaConsumerBuilder;
|
||||
import com.hccake.extend.kafka.KafkaExtendProducer;
|
||||
import com.hccake.extend.kafka.KafkaProducerBuilder;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
/**
|
||||
* @author lingting 2020/7/28 21:17
|
||||
@@ -16,20 +19,23 @@ import org.springframework.context.annotation.Bean;
|
||||
public class KafkaAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(KafkaExtendProducer.class)
|
||||
public KafkaExtendProducer<String, String> stringKafkaExtendProducer(KafkaProperties properties) {
|
||||
KafkaProducerBuilder builder = new KafkaProducerBuilder()
|
||||
.addAllBootstrapServers(properties.getBootstrapServers()).putAll(properties.getExtend());
|
||||
|
||||
if (properties.getKeySerializer() != null || properties.getKeySerializerClassName() != null) {
|
||||
builder.keySerializer(properties.getKeySerializer() == null ? properties.getKeySerializerClassName()
|
||||
: properties.getKeySerializer().getName());
|
||||
}
|
||||
|
||||
if (properties.getValueSerializer() != null || properties.getValueSerializerClassName() != null) {
|
||||
builder.valueSerializer(properties.getValueSerializer() == null ? properties.getValueSerializerClassName()
|
||||
: properties.getValueSerializer().getName());
|
||||
}
|
||||
builder.keySerializer(properties.getKeySerializerClassName());
|
||||
builder.valueSerializer(properties.getValueSerializerClassName());
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
@ConditionalOnMissingBean(KafkaConsumerBuilder.class)
|
||||
public KafkaConsumerBuilder consumerBuilder(KafkaProperties properties) {
|
||||
return new KafkaConsumerBuilder().addAllBootstrapServers(properties.getBootstrapServers())
|
||||
.keyDeserializer(properties.getKeyDeserializerClassName())
|
||||
.valueDeserializer(properties.getValueDeserializerClassName()).groupId(properties.getGroupId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package com.hccake.starter.kafka;
|
||||
|
||||
import lombok.Data;
|
||||
import org.apache.kafka.common.serialization.Serializer;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import lombok.Data;
|
||||
import org.apache.kafka.common.serialization.Deserializer;
|
||||
import org.apache.kafka.common.serialization.Serializer;
|
||||
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||
import org.apache.kafka.common.serialization.StringSerializer;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author lingting 2020/7/28 21:15
|
||||
@@ -16,6 +18,11 @@ import java.util.Set;
|
||||
@ConfigurationProperties(prefix = "ballcat.starter.kafka")
|
||||
public class KafkaProperties {
|
||||
|
||||
/**
|
||||
* 用于指定分组
|
||||
*/
|
||||
private String groupId;
|
||||
|
||||
/**
|
||||
* 所属服务
|
||||
*/
|
||||
@@ -27,7 +34,7 @@ public class KafkaProperties {
|
||||
private Class<? extends Serializer<?>> keySerializer = StringSerializer.class;
|
||||
|
||||
/**
|
||||
* key 序列化 类名
|
||||
* key 序列化 类名, 如果填写本值则 valueSerializer 无效
|
||||
*/
|
||||
private String keySerializerClassName;
|
||||
|
||||
@@ -37,13 +44,61 @@ public class KafkaProperties {
|
||||
private Class<? extends Serializer<?>> valueSerializer = StringSerializer.class;
|
||||
|
||||
/**
|
||||
* value 序列化 类名
|
||||
* value 序列化 类名, 如果填写本值则 valueSerializer 无效
|
||||
*/
|
||||
private String valueSerializerClassName;
|
||||
|
||||
/**
|
||||
* key 反序列化
|
||||
*/
|
||||
private Class<? extends Deserializer<?>> keyDeserializer = StringDeserializer.class;
|
||||
|
||||
/**
|
||||
* key 反序列化 类名, 如果填写本值则 keyDeserializer 无效
|
||||
*/
|
||||
private String keyDeserializerClassName;
|
||||
|
||||
/**
|
||||
* value 反序列化
|
||||
*/
|
||||
private Class<? extends Deserializer<?>> valueDeserializer = StringDeserializer.class;
|
||||
|
||||
/**
|
||||
* value 反序列化 类名, 如果填写本值则 valueDeserializer 无效
|
||||
*/
|
||||
private String valueDeserializerClassName;
|
||||
|
||||
/**
|
||||
* 额外参数
|
||||
*/
|
||||
private Map<String, Object> extend = new HashMap<>();
|
||||
|
||||
public String getKeyDeserializerClassName() {
|
||||
if (StrUtil.isNotEmpty(keyDeserializerClassName)) {
|
||||
return keyDeserializerClassName;
|
||||
}
|
||||
return getKeyDeserializer().getName();
|
||||
}
|
||||
|
||||
public String getValueDeserializerClassName() {
|
||||
if (StrUtil.isNotEmpty(valueDeserializerClassName)) {
|
||||
return valueDeserializerClassName;
|
||||
}
|
||||
return getValueDeserializer().getName();
|
||||
}
|
||||
|
||||
public String getKeySerializerClassName() {
|
||||
if (StrUtil.isNotEmpty(keySerializerClassName)) {
|
||||
return keySerializerClassName;
|
||||
}
|
||||
return getKeySerializer().getName();
|
||||
}
|
||||
|
||||
public String getValueSerializerClassName() {
|
||||
if (StrUtil.isNotEmpty(valueSerializerClassName)) {
|
||||
return valueSerializerClassName;
|
||||
}
|
||||
return getValueSerializer().getName();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user