0%

SpringBoot 连接配置

1
2
3
4
5
6
7
8
9
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://1.1.0.1:3306/test?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowPublicKeyRetrieval=true
username: test
password: test
db-type: mysql #数据库类型 oracle mysql pg
table-schema: test #数据库schema

Druid 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
spring:
datasource:
druid:
validationQuery: select 1
testWhileIdle: true
testOnBorrow: true
testOnReturn: true
# 定期输出监控日志(保存监控记录)
time-between-log-stats-millis: 60000
filters: stat
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
web-stat-filter:
enabled: true #是否启用StatFilter默认值false
url-pattern: /*
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,/sql-monitor/*"
session-stat-enable: false
session-stat-max-count: 1000
# principal-session-name:
# principal-cookie-name:
# 监控单个url调用的sql列表
profile-enable: true
# StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
# 允许清空统计数据
stat-view-servlet:
enabled: true #是否启用StatViewServlet(监控页面)默认值为false(考虑到安全问题默认并未启动,如需启用建议设置密码或白名单以保障安全)
url-pattern: /sql-monitor/*
reset-enable: true
login-username: admin
login-password: admin
# 访问控制,不支持IPv6
allow: 127.0.0.1
deny:
# Spring监控配置,说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置
# aop-patterns: com.skytech.* # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
filter: #DruidFilterConfiguration
# 配置StatFilter
stat:
enabled: true
log-slow-sql: false
slow-sql-millis: 10
# 配置WallFilter
wall:
enabled: false
config:
delete-allow: false
drop-table-allow: false

# 其他 Filter 配置不再演示

本文地址: https://github.com/maxzhao-it/blog/post/f46977ff/

下载

MongoDB 下载地址:https://www.mongodb.com/try/download/community

MongoDB Shell 下载地址:https://www.mongodb.com/try/download/shell

Web 管理工具下载:

  1. https://www.mongodb.com/download-center/compass
  2. DBeaver
  3. NoSQLBooster https://www.mongobooster.com/downloads
  4. ClusterControl
  5. Mongo Management Studio
  6. Nosqlclient

安装

解压 mongodb-windows-x86_64-6.0.4.zip 目录 D:\develop\mongodb\mongodb

解压 mongosh-1.8.0-win32-x64.zip 目录 D:\develop\mongodb\mongosh

创建库:

1
2
3
mkdir D:\develop\mongodb\mongodb\log
mkdir D:\develop\mongodb\mongodb\data
D:\develop\mongodb\mongodb\bin\mongod.exe --dbpath "D:\develop\mongodb\mongodb\data" --logpath "D:\develop\mongodb\mongodb\log\MongoDB.log" --install --serviceName "mongo" --logappend --directoryperdb

运行:

1
D:\develop\mongodb\mongodb\bin\mongod.exe

连接:

1
D:\develop\mongodb\mongosh\bin\mongosh "mongodb://localhost:27017"

本文地址: https://github.com/maxzhao-it/blog/post/1b6c4bb8/

Spring Security 版本 5.7.3

PasswordEncoder 生成类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import com.maxzhao.security.crypto.BootPasswordProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
@AutoConfigureAfter(BootPasswordProperties.class)
public class PasswordEncoderFactory {
@Resource
private BootPasswordProperties bootPasswordProperties;
/**
* 加密方式的实现
*/
private final Map<String, PasswordEncoder> idToPasswordEncoder = new HashMap<>(16);


/**
* 注册bean
*
* @return PasswordEncoder
*/
@Bean
public PasswordEncoder passwordEncoder() {
return getDelegatingPasswordEncoder();
}

/**
* 自定义加密密码
*
* @param idForEncode 加密密码的方式
* @param rawPassword 密码
* @return 加密后的密码
*/
public String encode(String idForEncode, CharSequence rawPassword) {
if (idForEncode == null) {
throw new IllegalArgumentException("idForEncode cannot be null");
}
PasswordEncoder passwordEncoder = idToPasswordEncoder.get(idForEncode);
if (passwordEncoder == null) {
throw new IllegalArgumentException(idForEncode + " PasswordEncoder cannot be null");
}
return passwordEncoder.encode(rawPassword);
}

/**
* 构建 多密码策略
*
* @return DelegatingPasswordEncoder
*/
public PasswordEncoder getDelegatingPasswordEncoder() {
String defaultEncoderId = bootPasswordProperties.getDefaultEncoderId();
if (!StringUtils.hasText(defaultEncoderId)) {
log.warn("默认密码加密方式不存在,已使用默认配置 {}", BootPasswordProperties.DEFAULT_ENCODER_ID);
defaultEncoderId = BootPasswordProperties.DEFAULT_ENCODER_ID;
}
if (idToPasswordEncoder.isEmpty()) {
Map<String, String> encoder = bootPasswordProperties.getEncoder();
if (encoder == null) {
encoder = new HashMap<>(0);
}
encoder.forEach((key, value) -> {
try {
Class<?> com = Class.forName(value);
Object o = com.newInstance();
if (o instanceof PasswordEncoder) {
idToPasswordEncoder.put(key, (PasswordEncoder) o);
} else {
log.error("加载自定义 {} 失败:{} 需要实现 PasswordEncoder 接口 ", key, value);
}
} catch (ClassNotFoundException e) {
log.error("加载自定义 {} 失败:PasswordEncoder[{}] ", key, value);
throw new RuntimeException(e);
} catch (InstantiationException | IllegalAccessException e) {
log.error("实例化 {} 失败:PasswordEncoder[{}] ", key, value);
throw new RuntimeException(e);
}
});
idToPasswordEncoder.put("bcrypt", new BCryptPasswordEncoder());
idToPasswordEncoder.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder());
idToPasswordEncoder.put("MD4", new org.springframework.security.crypto.password.Md4PasswordEncoder());
idToPasswordEncoder.put("MD5", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("MD5"));
idToPasswordEncoder.put("noop", org.springframework.security.crypto.password.NoOpPasswordEncoder.getInstance());
idToPasswordEncoder.put("pbkdf2", new Pbkdf2PasswordEncoder());
idToPasswordEncoder.put("scrypt", new SCryptPasswordEncoder());
idToPasswordEncoder.put("SHA-1", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-1"));
idToPasswordEncoder.put("SHA-256", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256"));
idToPasswordEncoder.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder());
idToPasswordEncoder.put("argon2", new Argon2PasswordEncoder());
}
/*Tip[DelegatingPasswordEncoder]: 使用当前密码策略,需要考虑历史密码的迁移*/
return new DelegatingPasswordEncoder(defaultEncoderId, idToPasswordEncoder);
}
}

BootPasswordProperties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
@Data
@Configuration
@ConfigurationProperties(prefix = "boot.security.crypto")
public class BootPasswordProperties {
/**
* 默认密码加密方式
*/
public static final String DEFAULT_ENCODER_ID = "bcrypt";
/**
* 默认密码加密方式 <br>
* 切换密码加密方式,修改当前配置
*/
private String defaultEncoderId = DEFAULT_ENCODER_ID;
/**
* 密码加密方式<br>
* key 名称,value 类的全路径 <br>
* 示例
* boot:
* security:
* crypto:
* defaultEncoderId: sm2
* encoder:
* - key: sm2
* value: com.maxzhao.security.crypto.password.Sm2PasswordEncoder
*/
private Map<String, String> encoder = new HashMap<>(4);

}

yml 配置自定义密码加密方式

1
2
3
4
5
6
boot:
security:
crypto:
default-encoder-id:
encoder:
sm2: com.maxzhao.security.crypto.password.Sm2PasswordEncoder

Sm2PasswordEncoder 需要实现 org.springframework.security.crypto.password.PasswordEncoder 接口

SpringSecurity 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Slf4j
@Configuration
public class AuthorizationServerSecurityConfiguration {
/**
* 密码加密创建工具
*/
@Resource
private UserDetailsService userDetailsService;

/**
* 密码加密创建工具
*/
@Resource
private PasswordEncoder passwordEncoder;
@Bean
@Order(2)
public SecurityFilterChain standardSecurityFilterChain(HttpSecurity http, UserCache userCache) throws Exception {
AuthenticationManagerBuilder authenticationBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
/*自定义 DaoAuthenticationProvider */
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService);
provider.setPasswordEncoder(passwordEncoder);
/*配置缓存*/
provider.setUserCache(userCache);
authenticationBuilder.authenticationProvider(provider);
http.authenticationManager(authenticationBuilder.build());
/* ***************** */
return http.build();
}

}

本文地址: https://github.com/maxzhao-it/blog/post/737197b3/

Spring Security 版本 5.7.3

SpringSecurity 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
public class AuthorizationServerSecurityConfiguration {

@Bean
@Order(2)
public SecurityFilterChain standardSecurityFilterChain(HttpSecurity http, UserCache userCache) throws Exception {
AuthenticationManagerBuilder authenticationBuilder = http.getSharedObject(AuthenticationManagerBuilder.class);
/*自定义 DaoAuthenticationProvider */
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(userDetailsService());
provider.setPasswordEncoder(getDelegatingPasswordEncoder());
/*配置缓存*/
provider.setUserCache(userCache);
authenticationBuilder.authenticationProvider(provider);
http.authenticationManager(authenticationBuilder.build());
/* ***************** */
return http.build();
}

public UserDetailsService userDetailsService() {
// @formatter:off
UserDetails userDetails = User.builder().passwordEncoder((password) -> getDelegatingPasswordEncoder().encode(password))
.username("user")
.password("password")
.roles("USER")
.build();
// @formatter:on
return username -> {
if (userDetails.getUsername().equals(username)) {
return userDetails;
}
return null;
};
}

private static PasswordEncoder getDelegatingPasswordEncoder() {
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

/**
* 默认缓存的实现
* 如需覆盖,请添加 @Primary
* @return UserCache
*/
@Bean
@Order(2)
public UserCache defaultUserCache() {
return new UserCache() {
@Override
public UserDetails getUserFromCache(String username) {
log.debug("");
return null;
}

@Override
public void putUserInCache(UserDetails user) {
log.debug("");
}

@Override
public void removeUserFromCache(String username) {
log.debug("");
}
};
}
}

本文地址: https://github.com/maxzhao-it/blog/post/6fb67330/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGBUS (0x7) at pc=0x00007fcbad0529ea, pid=27894, tid=0x00007fcb5ccaf700
#
# JRE version: Java(TM) SE Runtime Environment (8.0_361) (build 1.8.0_361-b09)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.361-b09 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# v ~StubRoutines::jint_disjoint_arraycopy
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /es2/esdata/elasticsearch-5.5.3/bin/hs_err_pid27894.log
[thread 140511412946688 also had an error]
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
#
已放弃

JDK11

本文地址: https://github.com/maxzhao-it/blog/post/343291111/

安装

有个联网设备

打开 visual studio

下载 vs_enterprise.exe

参考 创建基于网络的安装

执行命令

  • –layout 指定下载到哪个路径
  • –add 下载的内容ID 参考
  • –includeRecommended 安装推荐
  • –lang 指定语言 参考

参考1:

1
C:\Users\Administrator\Downloads\vs_Enterprise.exe   --layout D:\tools\c++\2022enterprise --lang Zh-cn  --includeRecommended

参考2:

1
C:\Users\Administrator\Downloads\vs_Enterprise.exe   --layout D:\tools\c++\2022enterprise --lang Zh-cn   --add Microsoft.VisualStudio.Workload.CoreEditor --add Microsoft.VisualStudio.Component.ManagedDesktop.Core --add Microsoft.VisualStudio.Workload.ManagedDesktop

到离线环境下安装

执行 vs_setup.exe

本文地址: https://github.com/maxzhao-it/blog/post/34329111155/

安装依赖

1
sudo yum install -y libcurl openssl xz-libs wget vim

安装

1
2
3
4
5
mkdir ~/tools && cd ~/tools
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel80-6.0.3.tgz
tar -zxf mongodb-linux-x86_64-rhel70-6.0.3.tgz -C ~/
cd ~/
mv mongodb-linux-x86_64-rhel70-6.0.3 mongodb

复制命令

1
2
3
4
sudo cp ~/mongodb/bin/* /usr/local/bin/
sudo ln -s ~/mongodb/bin/* /usr/local/bin/
sudo mkdir -p /var/lib/mongo
sudo mkdir -p /var/log/mongodb

See UNIX ulimit Settings

创建用户

1
2
3
sudo useradd mongod
sudo chown -R mongod:mongod /var/lib/mongo
sudo chown -R mongod:mongod /var/log/mongodb

编辑配置文件

1
sudo vim /etc/mongod.conf

写入

1
2
3
4
5
6
7
storage:
dbPath: /var/lib/mongo
systemLog:
path: /var/log/mongodb
net:
bindIp: 127.0.0.1
port: 27017

启动

1
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork

本文地址: https://github.com/maxzhao-it/blog/post/6b8ed0b3/

安装

启动 wsl

windows11 - 控制面板 - 启用或关闭 Windows 功能 - 勾选 适用于 Linux 的 Windows 子系统。

安装成功后重启。

查询可在线下载的操作系统

1
2
# 查询在线的版本 --list --online 
wsl.exe -l -o

结果

1
2
3
4
5
6
7
8
9
10
NAME               FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
SLES-12 SUSE Linux Enterprise Server v12
SLES-15 SUSE Linux Enterprise Server v15
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
OracleLinux_8_5 Oracle Linux 8.5
OracleLinux_7_9 Oracle Linux 7.9

安装 kali

1
wsl.exe --install kali-linux  

安装 OpenEuler

1

查询已安装的系统

1
wsl.exe -l -v

结果

1
2
3
  NAME               STATE           VERSION
* kali-linux Running 2
openEuler-22.03 Running 2

设置默认的发行版

1
2
# --set-default
wsl.exe -s openEuler-22.03

运行

启动发行版

1
wsl -d openEuler-22.03

安装依赖

1
sudo yum install -y net-tools vim wget tar unzip cifs-utils

迁移

1
2
3
4
5
wsl --shutdown
wsl --export openEuler-22.03 D:\vm-wsl\openEuler-22.03
wsl --unregister openEuler-22.03
wsl --import openEuler-22.03 D:\vm-wsl\openEuler-22.03 --version 2
wsl -s openEuler-22.03

其它命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
版权所有 (c) Microsoft Corporation。保留所有权利。
有关此产品的隐私信息,请访问 https://aka.ms/privacy。

用法: wsl.exe [参数] [选项...] [命令行]

用于运行 Linux 二进制文件的参数:

如果未提供命令行,wsl.exe 将启动默认 shell。

--exec, -e <命令行>
在不使用默认 Linux shell 的情况下执行指定的命令。

-shell-type <类型>-
使用提供的 shell 类型执行指定的命令。

类型:
标准
使用默认 Linux shell 执行指定的命令。

登录
使用默认的 Linux shell 作为登录 shell 执行指定的命令。


在不使用默认 Linux shell 的情况下执行指定的命令。

--
按当前方式传递剩余的命令行。

选项:
--cd <目录>
将指定目录设置为当前工作目录。
如果使用 ~ ,则将使用 Linux 用户的主路径。如果路径
以 / 字符开头,则将该路径解释为绝对 Linux 路径。
否则,该值必须为绝对 Windows 路径。

--distribution, -d <发行版>
运行指定的发行版。

--user, -u <UserName>
以指定的用户身份运行。

--system
为系统分发启动 shell。

用于管理 Linux 的 Windows 子系统的参数:

--帮助
显示使用情况信息。

--install <发行版> [选项...]
安装适用于 Linux 发行版的 Windows 子系统。
对于有效发行版的列表,请使用 "wsl.exe --list --online"。

选项:
--no-launch, -n
安装后不启动发行版。

--web-download
从 Internet 而不是 Microsoft Store 下载发行版。

--pre-release
下载预发布版本(如果可用)。表示 --web-download。

--mount <磁盘>
在所有 WSL 2 发行版中附加并装载物理或虚拟磁盘。

选项:
--vhd
指定<磁盘>是指虚拟硬盘。

--bare
将磁盘附加到 WSL2,但不进行装载。

--name <名称>
使用安装点的自定义名称安装磁盘。

--type <类型>
装载磁盘时要使用的文件系统,如果未指定,则默认为 ext4。

--options <选项>
其他装载选项。

--partition <索引>
要装载的分区索引,如果未指定,则默认为整个磁盘。

--set-default-version <版本>
更改新发行版的默认安装版本。

--shutdown
立即终止所有正在运行的发行版和 WSL 2
轻型实用程序虚拟机。

--status
显示适用于 Linux 的 Windows 子系统的状态。

--unmount [磁盘]
从所有 WSL2 发行版中卸载并分离磁盘。
如果在没有参数的情况下调用,则卸载和分离所有磁盘。

--update
更新适用于 Linux 包的 Windows 子系统。

选项:
--web-download
从互联网而不是 Microsoft 商店下载更新。

--version, -v
显示版本信息。

用于在适用于 Linux 的 Windows 子系统中管理发行版参数:

--export <发行版> <FileName> [选项]
将发行版导出为 tar 文件。
对于标准输出,文件名可以为 -。

选项:
--vhd
指定该发行版应导出为 .vhdx 文件。

--import <发行版> <InstallLocation> <FileName> [选项]
将指定的 tar 文件作为新发行版导入。
对于标准输入,文件名可以为 -。

选项:
--version <版本>
指定新发行版要使用的版本。

--vhd
指定提供的文件是 .vhdx 文件,而不是 tar 文件。
此操作会在指定的安装位置复制 .vhdx 文件。

--import-in-place <Distro> <FileName>
将指定的 .vhdx 文件导入为新发行版。
此虚拟硬盘必须以 ext4 文件系统类型格式化。

--list, -l [选项]
列出各个发行版。

选项:
--all
列出所有发行版,包括
当前正在安装或卸载的发行版。

--running
仅列出当前正在运行的发行版。

--quiet, -q
仅显示发行版名称。

--verbose, -v
显示有关所有发行版的详细信息。

--online, -o
显示可使用 "wsl.exe --install" 安装的发行版列表。

--set-default, -s <发行版>
将发行版设置为默认。

--set-version <发行版> <版本>
更改指定发行版的版本。

--terminate, -t <发行版>
终止指定的发行版。

--unregister <发行版>
注销发行版并删除根文件系统。

本文地址: https://github.com/maxzhao-it/blog/post/61fad7e8/