初始化项目依赖

This commit is contained in:
朱毅骏 2022-08-17 18:12:43 +08:00
parent 2d74086eed
commit 69cf608827
29 changed files with 2747 additions and 103 deletions

97
pom.xml
View File

@ -15,6 +15,8 @@
<version>1.0-SNAPSHOT</version>
<modules>
<module>zyjblogs-oauth</module>
<module>zyjblogs-rbac</module>
<module>zyjblogs-gateway</module>
</modules>
<properties>
@ -27,9 +29,24 @@
<jjwt.version>0.11.1</jjwt.version>
<aliyun.version>4.5.0</aliyun.version>
<spring-authorization-server.version>0.3.1</spring-authorization-server.version>
<feign-hystrix.version>11.8</feign-hystrix.version>
<feign-ribbon.version>11.8</feign-ribbon.version>
<nacos-client.version>2.0.4</nacos-client.version>
<!-- ORM -->
<mybatis-plus-boot-starter.version>3.4.3</mybatis-plus-boot-starter.version>
<mysql-jdbc.version>8.0.21</mysql-jdbc.version>
<!-- spring-cloud-alibaba版本配置 -->
<spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
@ -47,6 +64,12 @@
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- ORM -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-jdbc.version}</version>
</dependency>
<!-- JWT 依赖开始 -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
@ -68,21 +91,47 @@
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-authorization-server -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-oauth2-authorization-server</artifactId>-->
<!-- <version>${spring-authorization-server.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-hystrix</artifactId>
<version>${feign-hystrix.version}</version>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-ribbon</artifactId>
<version>${feign-ribbon.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>${nacos-client.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.11</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis-plus-boot-starter.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-authorization-server -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-oauth2-authorization-server</artifactId>-->
<!-- <version>${spring-authorization-server.version}</version>-->
<!-- </dependency>-->
</dependencies>
</dependencyManagement>
<dependencies>
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-oauth2-authorization-server</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-oauth2-authorization-server</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
@ -97,18 +146,22 @@
<artifactId>jackson-core</artifactId>
</dependency>
<!-- JWT 依赖开始 -->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt-api</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt-impl</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt-jackson</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt-api</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt-impl</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>io.jsonwebtoken</groupId>-->
<!-- <artifactId>jjwt-jackson</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

131
zyjblogs-gateway/.gitignore vendored Normal file
View File

@ -0,0 +1,131 @@
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
*.stackdump
[Dd]esktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msix
*.msm
*.msp
*.lnk
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.gradle
**/build/
!src/**/build/
gradle-app.setting
!gradle-wrapper.jar
.gradletasknamecache
.svn/
.DS_Store
.AppleDouble
.LSOverride
Icon
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
**/nbproject/private/
**/nbproject/Makefile-*.mk
**/nbproject/Package-*.bash
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
.idea
*.iml
out
gen
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
.externalToolBuilders/
*.launch
*.pydevproject
.cproject
.autotools
.factorypath
.buildpath
.target
.tern-project
.texlipse
.springBeans
.recommenders/
.apt_generated/
.apt_generated_test/
.cache-main
.scala_dependencies
.worksheet
/CVS/*
**/CVS/*
.cvsignore
*/.cvsignore
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
.idea/replstate.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser

19
zyjblogs-gateway/pom.xml Normal file
View File

@ -0,0 +1,19 @@
<?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">
<parent>
<artifactId>zyjblogs-parent</artifactId>
<groupId>cn.zyjblogs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zyjblogs-gateway</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
</project>

View File

@ -24,6 +24,54 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- 集成nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<exclusions>
<exclusion>
<artifactId>checker-qual</artifactId>
<groupId>org.checkerframework</groupId>
</exclusion>
<exclusion>
<artifactId>error_prone_annotations</artifactId>
<groupId>com.google.errorprone</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 集成 Nacos 作为服务注册中心配置 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -2,6 +2,8 @@ package cn.zyjblogs.oauth;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
@ -11,6 +13,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @description
* @date 2022/8/5 8:49
*/
@EnableAsync
@EnableFeignClients
@SpringBootApplication
public class OauthApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,28 @@
package cn.zyjblogs.oauth.config.mybatis;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description
* @date 2022/8/17 17:09
*/
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}

View File

@ -0,0 +1,34 @@
package cn.zyjblogs.oauth.config.redis;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description
* @date 2022/8/17 17:58
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisSerializer keySerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> valSerializer = new Jackson2JsonRedisSerializer(Object.class);
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(keySerializer);
redisTemplate.setValueSerializer(valSerializer);
redisTemplate.setHashKeySerializer(valSerializer);
redisTemplate.setHashValueSerializer(valSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

View File

@ -0,0 +1,9 @@
package cn.zyjblogs.oauth.config.redis.lock;
public abstract class AbstractLockExecutor<T> implements LockExecutor<T> {
protected T obtainLockInstance(boolean locked, T lockInstance) {
return locked ? lockInstance : null;
}
}

View File

@ -0,0 +1,19 @@
package cn.zyjblogs.oauth.config.redis.lock;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;
@Component
public class DefaultLockKeyBuilder implements LockKeyBuilder {
@Override
public String buildKey(Collection<String> definitionKeys) {
if (CollectionUtils.isEmpty(definitionKeys)) {
return "";
}
return StringUtils.collectionToDelimitedString(definitionKeys, ".", "", "");
}
}

View File

@ -0,0 +1,40 @@
package cn.zyjblogs.oauth.config.redis.lock;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description 分布式锁核心处理器
* @date 2022/5/23 10:26
*/
public interface LockExecutor<T> {
/**
* 加锁
*
* @param lockKey 锁标识
* @param lockValue 锁值
* @param expire 锁有效时间
* @param acquireTimeout 获取锁超时时间
* @return 锁信息
*/
T acquire(String lockKey, String lockValue, long expire, long acquireTimeout);
/**
* 解锁
*
* <pre>
* 为何解锁需要校验lockValue
* 客户端A加锁一段时间之后客户端A解锁在执行releaseLock之前锁突然过期了
* 此时客户端B尝试加锁成功然后客户端A再执行releaseLock方法则将客户端B的锁给解除了
* </pre>
*
* @param key 加锁key
* @param value 加锁value
* @param lockInstance 锁实例
* @return 是否释放成功
*/
boolean releaseLock(String key, String value, T lockInstance);
}

View File

@ -0,0 +1,14 @@
package cn.zyjblogs.oauth.config.redis.lock;
import java.util.Collection;
public interface LockKeyBuilder {
/**
* 构建key
*
* @param definitionKeys 定义
* @return key
*/
String buildKey(Collection<String> definitionKeys);
}

View File

@ -0,0 +1,66 @@
package cn.zyjblogs.oauth.config.redis.lock;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import org.springframework.util.StringUtils;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description
* @date 2022/5/23 11:01
*/
@Data
@AllArgsConstructor
@Builder
public class RedisLock {
/**
* 锁名称
*/
private String lockKey;
/**
* 锁值
*/
private String lockValue;
/**
* 过期时间
*/
private Long expire;
/**
* 获取锁超时时间
*/
private Long acquireTimeout;
/**
* 获取锁次数
*/
private int acquireCount;
/**
* 锁实例
*/
private Object lockInstance;
/**
* 锁执行器
*/
private LockExecutor lockExecutor;
/***
* 解锁
* @author zhuyijun
* @Description
* @date 15:10
*/
public void unLock() {
if (lockExecutor != null && lockInstance != null && !StringUtils.isEmpty(lockKey)) {
lockExecutor.releaseLock(lockKey, lockValue, lockInstance);
}
}
}

View File

@ -0,0 +1,129 @@
package cn.zyjblogs.oauth.config.redis.lock;
import cn.com.hatechframework.bridge.exception.BcmsException;
import cn.com.hatechframework.common.entity.response.ResponseCode;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description
* @date 2022/5/23 11:02
*/
@Slf4j
@Component
public class RedisLockTemplate {
@Resource
private LockExecutor<String> RedisTemplateLockExecutor;
/**
* 默认失效时间
*/
@Value("${hatech.redis.lock-expire:3000}")
private Long defaultExpire;
@Value("${hatech.redis.lock-timeout:30000}")
private Long defaultAcquireTimeout;
/**
* 获取锁失败时重试时间间隔 单位毫秒
*/
@Value("${hatech.redis.lock-retry:100}")
private Long retryInterval;
@Value("${hatech.redis.lock-prefix:lock}")
private String prefix;
/**
* 获取锁推荐不推荐
*
* @param key
* @param expire
* @return
*/
public RedisLock lock(String key, long expire) {
return tryLock(key, expire, 0, null);
}
/**
* 尝试获取锁方法推荐
*
* @param key 锁key 同一个key只能被一个客户端持有
* @param expire 过期时间(ms) 防止死锁
* @param acquireTimeout 尝试获取锁超时时间(ms)
* @return 加锁成功返回锁信息 失败返回null
*/
public RedisLock tryLock(String key, long expire, long acquireTimeout) {
return tryLock(key, expire, acquireTimeout, null);
}
/**
* 加锁方法
*
* @param key 锁key 同一个key只能被一个客户端持有
* @param expire 过期时间(ms) 防止死锁
* @param acquireTimeout 尝试获取锁超时时间(ms)
* @param executor 执行器
* @return 加锁成功返回锁信息 失败返回null
*/
public RedisLock tryLock(String key, long expire, long acquireTimeout, LockExecutor executor) {
key = prefix + key;
LockExecutor lockExecutor = obtainExecutor(executor);
expire = expire < 0 ? defaultExpire : expire;
long currentAcquireTimeout = acquireTimeout < 0 ? defaultAcquireTimeout : acquireTimeout;
int acquireCount = 0;
String value = IdWorker.get32UUID();
long start = System.currentTimeMillis();
try {
do {
acquireCount++;
Object lockInstance = lockExecutor.acquire(key, value, expire, acquireTimeout);
if (null != lockInstance) {
return RedisLock.builder()
.lockKey(key)
.lockValue(value)
.expire(expire)
.acquireTimeout(currentAcquireTimeout)
.acquireCount(acquireCount)
.lockInstance(lockInstance)
.lockExecutor(lockExecutor)
.build();
}
TimeUnit.MILLISECONDS.sleep(retryInterval);
} while (System.currentTimeMillis() - start < currentAcquireTimeout);
} catch (InterruptedException e) {
log.error("lock error", e);
throw new BcmsException(ResponseCode.INTERNAL_SERVER_ERROR, "加锁失败");
}
return null;
}
protected LockExecutor obtainExecutor(LockExecutor lockExecutor) {
if (null == lockExecutor) {
return RedisTemplateLockExecutor;
}
return lockExecutor;
}
/***
* 解锁
* @param redisLock
* @author zhuyijun
* @Description
* @date 11:29
*/
public boolean unLock(RedisLock redisLock) {
if (null == redisLock) {
return false;
}
return redisLock.getLockExecutor().releaseLock(redisLock.getLockKey(), redisLock.getLockValue(),
redisLock.getLockInstance());
}
}

View File

@ -0,0 +1,63 @@
package cn.zyjblogs.oauth.config.redis.lock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Collections;
@Slf4j
@Component
public class RedisTemplateLockExecutor extends AbstractLockExecutor<String> {
private static final RedisScript<String> SCRIPT_LOCK = new DefaultRedisScript<>("return redis.call('set',KEYS[1]," +
"ARGV[1],'NX','PX',ARGV[2])", String.class);
private static final RedisScript<String> SCRIPT_UNLOCK = new DefaultRedisScript<>("if redis.call('get',KEYS[1]) " +
"== ARGV[1] then return tostring(redis.call('del', KEYS[1])==1) else return 'false' end", String.class);
private static final String LOCK_SUCCESS = "OK";
@Resource
private RedisTemplate<String, String> redisTemplate;
/***
* 枷锁
* @param lockKey
* @param lockValue
* @param expire
* @param acquireTimeout
* @author zhuyijun
* @Description
* @date 10:26
*/
@Override
public String acquire(String lockKey, String lockValue, long expire, long acquireTimeout) {
String lock = redisTemplate.execute(SCRIPT_LOCK,
redisTemplate.getStringSerializer(),
redisTemplate.getStringSerializer(),
Collections.singletonList(lockKey),
lockValue, String.valueOf(expire));
final boolean locked = LOCK_SUCCESS.equals(lock);
return obtainLockInstance(locked, lock);
}
/***
* 解锁
* @param key
* @param value
* @param lockInstance
* @author zhuyijun
* @Description
* @date 10:26
*/
@Override
public boolean releaseLock(String key, String value, String lockInstance) {
String releaseResult = redisTemplate.execute(SCRIPT_UNLOCK,
redisTemplate.getStringSerializer(),
redisTemplate.getStringSerializer(),
Collections.singletonList(key), value);
return Boolean.parseBoolean(releaseResult);
}
}

View File

@ -0,0 +1,78 @@
package cn.zyjblogs.oauth.config.security;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
/**
* @author zhuyijun
* @version 1.0.0
* @description 授权服务
* @date 2022/8/17 17:51
*/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
public AuthorizationServerConfiguration() {
}
/**
* 令牌端点的安全约束
*
* @param security
* @throws Exception
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
// 允许http请求访问以获取token
security
//允许匿名访问端点url:/oauth/token_key
.tokenKeyAccess("permitAll()")
//允许匿名访问端点url:/oauth/check_token
.checkTokenAccess("isAuthenticated()")
.allowFormAuthenticationForClients();
}
/**
* 客户端详情服务
*
* @param clients
* @throws Exception
*/
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
//存储方式
.inMemory()
//客户端id client_id
.withClient("zyjblogs-rbac")
//secret
.secret(new BCryptPasswordEncoder().encode("secret"))
//资源列表
.resourceIds("res1")
.authorizedGrantTypes("authorization_code",
"password", "client_credentials", "implicit", "refresh_token")
//允许授权封范围
.scopes("all")
.autoApprove(false)
//加上验证回调地址
.redirectUris("https://www.baidu.com");
}
/***
* 令牌访问端点
* @param endpoints
* @author zhuyijun
* @Description
* @date 17:52
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
super.configure(endpoints);
}
}

View File

@ -2,8 +2,6 @@ package cn.zyjblogs.oauth.config.security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@ -27,17 +25,15 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public UserDetailsService userDetailsServiceBean() throws Exception {
InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
userDetailsManager.createUser(User.withUsername("zhangsan").password("123456").authorities("p1").build());
userDetailsManager.createUser(User.withUsername("zhangsan").password("$2a$10$jwUQH.QkSvznnPRlte87k.Kw3CaLwBJbanUHM70Ry4to1Q.aXgKTi").authorities("p1").build());
userDetailsManager.createUser(User.withUsername("lisi").password("123456").authorities("p2").build());
return userDetailsManager;
}
/**
* 密码编码解码
* 密码编码解码
*
* @return 解码类
* @author tanyuanzhi
* @date 2021/10/28 14:59
* @return
*/
@Bean
public PasswordEncoder passwordEncoder() {
@ -45,11 +41,11 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
// @Bean
// @Override
// public AuthenticationManager authenticationManagerBean() throws Exception {
// return super.authenticationManagerBean();
// }
/**
@ -59,12 +55,11 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
* @author tanyuanzhi
* @date 2021/10/28 15:05
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
}
// @Override
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//
//
// }
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
@ -72,9 +67,13 @@ public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
http.authorizeRequests()
.antMatchers("/webjars/**", "/swagger-ui.html/**", "/swagger-resources/**", "/v2/api-docs/**")
.permitAll()
.antMatchers("/oauth/**", "/login")
//以下请求必须认证通过
.antMatchers("/demo/**", "/oauth/**", "/login")
.authenticated()
.and().httpBasic();
.and()
//允许表单登录
.formLogin()
.successForwardUrl("/demo/success");
}
@Override

View File

@ -0,0 +1,22 @@
package cn.zyjblogs.oauth.server.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description
* @date 2022/8/17 15:22
*/
@RestController
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/success")
public String success() {
return "登录成功";
}
}

View File

@ -1,21 +0,0 @@
server:
port: 9029
spring:
application:
name: oauth-server
security:
user:
name: user
password: 123456
roles: USER,ADMIN
logging:
level:
cn:
zyjblogs:
oauth: INFO
org:
springframework:
security: INFO
pattern:
console: '%clr(%d{E HH:mm:ss.SSS}){blue} %clr(%-5p) %clr(${PID}){faint} %clr(---){faint}
%clr([%8.15t]){cyan} %clr(%-40.40logger{0}){blue} %clr(:){red} %clr(%m){faint}%n'

View File

@ -0,0 +1,8 @@
hatech:
config:
nacos:
host: ${HATECH_CONFIG_NACOS_HOST:192.168.137.1}
port: ${HATECH_CONFIG_NACOS_PORT:8848}
username: ${HATECH_CONFIG_NACOS_USERNAME:nacos}
password: ${HATECH_CONFIG_NACOS_PASSWORD:nacos}

View File

@ -0,0 +1,28 @@
spring:
profiles:
active: test
---
spring:
application:
name: zyjblogs-oauth
cloud:
nacos:
config:
server-addr: ${hatech.config.nacos.host}:${hatech.config.nacos.port}
username: ${hatech.config.nacos.username}
password: ${hatech.config.nacos.password}
namespace: ${spring.profiles.active}
group: public
file-extension: yml
shared-configs[0]:
data-id: zyjblogs-global-${spring.profiles.active}.yml
group: global
discovery:
server-addr: ${spring.cloud.nacos.config.server-addr}
username: ${spring.cloud.nacos.config.username}
password: ${spring.cloud.nacos.config.password}
namespace: ${spring.cloud.nacos.config.namespace}
group: public
logging:
config: classpath:logback-spring.xml

View File

@ -0,0 +1,198 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 获取application.yml配置文件中的属性 -->
<springProperty scope="context" name="SPRING_APPLICATION_NAME" source="spring.application.name"/>
<springProperty scope="context" name="SPRING_PROFILES_ACTIVE" source="spring.profiles.active"/>
<!-- 配置文件存放路径 -->
<property name="logger.path" value="./log/${SPRING_APPLICATION_NAME}/${SPRING_PROFILES_ACTIVE}" />
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr([%X{traceId}]) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!-- 输出到控制台 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 输出到文件 -->
<!-- 时间滚动输出 level为 DEBUG 日志 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logger.path}/log_debug.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${logger.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${logger.path}/log_info.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 每天日志归档路径以及格式 -->
<fileNamePattern>${logger.path}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录info级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 WARN 日志 -->
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${logger.path}/log_warn.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logger.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录warn级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${logger.path}/log_error.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logger.path}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录ERROR级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--
root节点是必选节点用来指定最基础的日志输出级别只有一个level属性
level:用来设置打印级别大小写无关TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF
不能设置为INHERITED或者同义词NULL。默认是DEBUG
可以包含零个或多个元素标识这个appender将会添加到这个logger。
-->
<!-- 开发环境配置 -->
<springProfile name="dev">
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- 测试环境配置 -->
<springProfile name="test">
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- 上线环境配置 -->
<springProfile name="prod">
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- 高可用环境配置-->
<springProfile name="ha">
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- k8s环境配置-->
<springProfile name="k8s">
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
</configuration>

131
zyjblogs-rbac/.gitignore vendored Normal file
View File

@ -0,0 +1,131 @@
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
*.stackdump
[Dd]esktop.ini
$RECYCLE.BIN/
*.cab
*.msi
*.msix
*.msm
*.msp
*.lnk
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
.gradle
**/build/
!src/**/build/
gradle-app.setting
!gradle-wrapper.jar
.gradletasknamecache
.svn/
.DS_Store
.AppleDouble
.LSOverride
Icon
._*
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
**/nbproject/private/
**/nbproject/Makefile-*.mk
**/nbproject/Package-*.bash
build/
nbbuild/
dist/
nbdist/
.nb-gradle/
.idea
*.iml
out
gen
*.class
*.log
*.ctxt
.mtj.tmp/
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
hs_err_pid*
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders
.externalToolBuilders/
*.launch
*.pydevproject
.cproject
.autotools
.factorypath
.buildpath
.target
.tern-project
.texlipse
.springBeans
.recommenders/
.apt_generated/
.apt_generated_test/
.cache-main
.scala_dependencies
.worksheet
/CVS/*
**/CVS/*
.cvsignore
*/.cvsignore
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
cmake-build-*/
.idea/**/mongoSettings.xml
*.iws
out/
.idea_modules/
atlassian-ide-plugin.xml
.idea/replstate.xml
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
.idea/httpRequests
.idea/caches/build_file_checksums.ser

80
zyjblogs-rbac/pom.xml Normal file
View File

@ -0,0 +1,80 @@
<?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">
<parent>
<artifactId>zyjblogs-parent</artifactId>
<groupId>cn.zyjblogs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>zyjblogs-rbac</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- 集成nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<exclusions>
<exclusion>
<artifactId>checker-qual</artifactId>
<groupId>org.checkerframework</groupId>
</exclusion>
<exclusion>
<artifactId>error_prone_annotations</artifactId>
<groupId>com.google.errorprone</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- 集成 Nacos 作为服务注册中心配置 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,23 @@
package cn.zyjblogs.rbac;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description
* @date 2022/8/17 15:53
*/
@EnableAsync
@EnableFeignClients
@SpringBootApplication
public class RbacApplication {
public static void main(String[] args) {
SpringApplication.run(RbacApplication.class, args);
}
}

View File

@ -0,0 +1,28 @@
package cn.zyjblogs.rbac.config.mybatis;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Copyright (C), 2021, 北京同创永益科技发展有限公司
*
* @author zhuyijun
* @version 3.0.0
* @description
* @date 2022/8/17 17:09
*/
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
return interceptor;
}
}

View File

@ -0,0 +1,8 @@
hatech:
config:
nacos:
host: ${HATECH_CONFIG_NACOS_HOST:192.168.137.1}
port: ${HATECH_CONFIG_NACOS_PORT:8848}
username: ${HATECH_CONFIG_NACOS_USERNAME:nacos}
password: ${HATECH_CONFIG_NACOS_PASSWORD:nacos}

View File

@ -0,0 +1,28 @@
spring:
profiles:
active: test
---
spring:
application:
name: zyjblogs-rbac
cloud:
nacos:
config:
server-addr: ${hatech.config.nacos.host}:${hatech.config.nacos.port}
username: ${hatech.config.nacos.username}
password: ${hatech.config.nacos.password}
namespace: ${spring.profiles.active}
group: public
file-extension: yml
shared-configs[0]:
data-id: zyjblogs-global-${spring.profiles.active}.yml
group: global
discovery:
server-addr: ${spring.cloud.nacos.config.server-addr}
username: ${spring.cloud.nacos.config.username}
password: ${spring.cloud.nacos.config.password}
namespace: ${spring.cloud.nacos.config.namespace}
group: public
logging:
config: classpath:logback-spring.xml

View File

@ -0,0 +1,198 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 获取application.yml配置文件中的属性 -->
<springProperty scope="context" name="SPRING_APPLICATION_NAME" source="spring.application.name"/>
<springProperty scope="context" name="SPRING_PROFILES_ACTIVE" source="spring.profiles.active"/>
<!-- 配置文件存放路径 -->
<property name="logger.path" value="./log/${SPRING_APPLICATION_NAME}/${SPRING_PROFILES_ACTIVE}" />
<!-- 彩色日志 -->
<!-- 彩色日志依赖的渲染类 -->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- 彩色日志格式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr([%X{traceId}]) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!-- 输出到控制台 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>info</level>
</filter>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<!-- 输出到文件 -->
<!-- 时间滚动输出 level为 DEBUG 日志 -->
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logger.path}/log_debug.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 日志归档 -->
<fileNamePattern>${logger.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录debug级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>debug</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 INFO 日志 -->
<appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${logger.path}/log_info.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- 每天日志归档路径以及格式 -->
<fileNamePattern>${logger.path}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录info级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>info</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 WARN 日志 -->
<appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${logger.path}/log_warn.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logger.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录warn级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>warn</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!-- 时间滚动输出 level为 ERROR 日志 -->
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 正在记录的日志文件的路径及文件名 -->
<file>${logger.path}/log_error.log</file>
<!--日志文件输出格式-->
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%X{traceId}] %-5level %logger{50} - %msg%n</pattern>
<charset>UTF-8</charset>
</encoder>
<!-- 日志记录器的滚动策略,按日期,按大小记录 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${logger.path}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
<!-- 日志采集保留天数 -->
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<!-- 此日志文件只记录ERROR级别的 -->
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
<!--
root节点是必选节点用来指定最基础的日志输出级别只有一个level属性
level:用来设置打印级别大小写无关TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF
不能设置为INHERITED或者同义词NULL。默认是DEBUG
可以包含零个或多个元素标识这个appender将会添加到这个logger。
-->
<!-- 开发环境配置 -->
<springProfile name="dev">
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- 测试环境配置 -->
<springProfile name="test">
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- 上线环境配置 -->
<springProfile name="prod">
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- 高可用环境配置-->
<springProfile name="ha">
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
<!-- k8s环境配置-->
<springProfile name="k8s">
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
<appender-ref ref="INFO_FILE" />
<appender-ref ref="WARN_FILE" />
<appender-ref ref="ERROR_FILE" />
</root>
</springProfile>
</configuration>