0%

引入依赖包

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>

如果使用HTML5严格语法的话,可不加入nekohtml的依赖包
使用LEGACYHTML5的话,则必须加入nekohtml的依赖包

添加配置信息

选择项目中的bootstrap.yml或者application.yml等配置文件其中之一,添加如下内容

1
2
3
4
spring:
thymeleaf:
mode: LEGACYHTML5
cache: false

如果是application.properties,请按照spring.thymeleaf.mode= LEGACYHTML5的格式进行添加
添加cache是防止开发阶段,修改html文件厚始终不更新页面的问题

html页面使用该模板解析器

修改HTML页面,将html标签的开始标签更改为<html xmlns:th="http://www.thymeleaf.org">即可。

引入必要的依赖包

1
2
3
4
5
6
7
8
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>

redis连接配置

1
2
3
4
5
spring:
redis:
host: 192.168.1.189
port: 6379
password: GHleYdcc

主类添加必要的注解

在程序入口类中添加@EnableRedisHttpSession注解,即可完成session的共享。

推荐设置@EnableRedisHttpSession(redisNamespace="user",maxInactiveIntervalInSeconds=1200),通过redisNamespace为该应用的session添加前缀防止冲突,maxInactiveIntervalInSeconds来设置session的过期时间。

ssh 是客户端
sshd 应该是服务器端的
ssh-agent ssh-agent是一种控制用来保存公钥身份验证所使用的私钥的程序

查看ssh状态

ps -e |grep ssh或者rpm -qa |grep ssh

Failed to start ssh.service: Unit ssh.service not found.

未安装ssh,安装即可apt-get install openssh-server

su passwd
先给root用户设置密码,然后才可以通过root账户登陆

设置/etc/ssh/sshd_config

  1. 设置后允许root用户远程登录PermitRootLogin yes
  2. PasswordAuthentication yes
  3. UseDNS no

重启ssh/etc/init.d/ssh restart

如若还是不能远程登录的话,可能是root密码忘记设置了
sudo passwd root
设置一下密码就可以远程登录了

产生KEY的相关文件位置变更

如果不需要更改key默认生成位置和相关文件请跳过本步骤

文字描述

  1. 在桌面我的电脑右击,选择最下边的属性,同样也可以打开系统属性界面(快捷键windwos+Break组合键)
  2. 点击高级系统设置,接着点击环境变量
  3. 打开环境变量编辑界面后,点击新建,添加一个新的系统变量HOME,值为自己想设定的路径。最后点击确定,确定,确定。完成环境变量的设置。
阅读全文 »

自定义数据库数据权限验证

基于springboot的web项目

引入必要的依赖包

1
2
3
4
5
6
7
8
9
10
11
12
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

添加数据库的连接配置信息

1
2
3
4
5
6
spring:
datasource:
url: jdbc:mysql://**/db?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: name
password: password
driver-class-name: com.mysql.jdbc.Driver

创建表结构语句:

1
2
3
4
5
6
7
8
9
CREATE TABLE `db`.`base_user`  (
`id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称',
`addtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '添加时间',
`password` varchar(32) CHARACTER SET gbk COLLATE gbk_chinese_ci NULL DEFAULT NULL COMMENT '密码',
PRIMARY KEY (`id`) USING BTREE
) ;

INSERT INTO `db`.`base_user`(`id`, `name`,`addtime`, `password`) VALUES ('1', 'admin', 'buzhidao');

添加自定义的UserDetailsService

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
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Configuration
public class CustomUserDetailsService implements UserDetailsService {

@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Map<String, Object> user = jdbcTemplate.queryForMap("select id,name,password from base_user where name='" + username + "'");
if (user.size() > 0) {
return User.withUsername(username).password(user.get("password").toString()).roles("test").build();
}
return null;
}
}

添加权限验证类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.springframework.beans.factory.annotation.Autowired;
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.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class WbUserSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomUserDetailsService customUserDetailsService;

@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()
.and().formLogin().loginPage("/login").permitAll()
.and().rememberMe().tokenValiditySeconds(2419200).key("wbuserkey");
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(customUserDetailsService);
}

}

登陆页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>login</title>
</head>
<body>
<form action="/login" method="POST">
<input name="username"/>
<input name="password" type="password"/>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
<input type="checkbox" name="remember-me"/><label>记住我</label>
<button type="submit">登陆</button>

</form>
</body>
</html>

注意: 添加权限类的时候注意auth.userDetailsService(customUserDetailsService);,customUserDetailsService要自动注入,并且要写在参数为AuthenticationManagerBuilder的重载方法中。

version 3.2.15

下载安装包

wget https://github.com/coreos/etcd/releases/download/v3.2.15/etcd-v3.2.15-linux-arm64.tar.gz

设置etcd和etcdctl

1
2
3
4
5
6
tar -xzvf etcd-v3.2.15-linux-arm64.tar.gz
cd etcd-v3.2.15-linux-arm64
cp etcd etcdctl /usr/bin
chmod +x /usr/bin/etcd
chmod +x /usr/bin/etcdctl
sudo mkdir /var/lib/etcd

设置etcd.service文件

vim /lib/systemd/system/etcd.service

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Etcd Server
After=network.target

[Service]
#Type=simple
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/default/etcd
ExecStart=/usr/bin/etcd

[Install]
WantedBy=multi-user.target

设置自定义配置文件

vim /etc/default/etcd

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
# [member]
ETCD_NAME="etcd1"
# etcd数据保存目录
ETCD_DATA_DIR="/var/lib/etcd"
# 供外部客户端使用的URL
ETCD_LISTEN_CLIENT_URLS="https://192.168.31.189:2379,http://127.0.0.1:2379"
# 广播给外部客户端使用的URL
ETCD_ADVERTISE_CLIENT_URLS="https://192.168.31.189:2379,http://127.0.0.1:2379"
# [cluster]
# 集群内部通信使用的URL
ETCD_LISTEN_PEER_URLS="https://192.168.31.189:2380"
# 广播给集群内其他成员访问的URL
ETCD_INITIAL_ADVERTISE_PEER_URLS="https://192.168.31.189:2380"
# 初始集群成员列表
ETCD_INITIAL_CLUSTER="etcd1=https://192.168.31.189:2380"
# 初始集群状态,new为新建集群
ETCD_INITIAL_CLUSTER_STATE="new"
# 集群名称
ETCD_INITIAL_CLUSTER_TOKEN="wb"
ETCD_CERT_FILE=/srv/kubernetes/ssl/etcd.pem
ETCD_KEY_FILE=/srv/kubernetes/ssl/etcd-key.pem
ETCD_TRUSTED_CA_FILE=/srv/kubernetes/ssl/ca.pem
ETCD_CLIENT_CERT_AUTH=true
ETCD_PEER_CERT_FILE=/srv/kubernetes/ssl/etcd.pem
ETCD_PEER_KEY_FILE=/srv/kubernetes/ssl/etcd-key.pem
ETCD_TRUSTED_CA_FILE=/srv/kubernetes/ssl/ca.pem
ETCD_PEER_CLIENT_CERT_AUTH=true

拷贝证书文件

1
2
3
cd /data/certs/
mkdir -p /srv/kubernetes/ssl
cp ca* etcd* /srv/kubernetes/ssl/

启动etcd服务

1
2
3
sudo systemctl daemon-reload
sudo systemctl enable etcd.service
sudo systemctl start etcd.service

设置仓库

更新包索引

sudo apt update

安装必备软件包

1
2
3
4
5
6
sudo apt-get -y install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common \
bridge-utils

添加官方GPG密钥

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

添加仓库地址

amd64

1
2
3
4
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"

安装Docker CE

更新包索引

sudo apt update

安装最新版的Docker CE

sudo apt install docker-ce

安装指定版本的Docker CE

查看所有Docker CE版本列表

sudo apt-cache madison docker-ce

安装指定版本Docker CE

sudo apt-get -y install docker-ce=17.09.0~ce-0~ubuntu

设置开机启动

1
2
3
sudo systemctl daemon-reload
sudo systemctl enable docker.service
sudo systemctl restart docker.service

更改存储目录

编辑/lib/systemd/system/docker.service,在ExecStart=/usr/bin/dockerd -H fd://后面添加-g /var/lib/docker1.

sudo systemctl daemon-reload,sudo systemctl restart docker.service就生效了

删除所有包含的镜像

docker rmi $(docker images | grep '\bdemo.demo.com/demo\b' | grep '<none>' | awk '{print $3}')

哈哈,后来使用Next主题了,不适用啦,从新将现有博客系统的内容更新了一遍。2019年04月16日19:19:15

参考文章:

初始化hexo

1
2
3
4
5
6
7
npm install hexo-cli -g
hexo init sky0504
cd sky0504
npm install
npm install hexo-renderer-pug --save
npm install hexo-renderer-sass --save
npm install hexo-deployer-git --save

更换主题

  1. fork大道至简主题的仓库
  2. 克隆《大道至简》的主题

    1
    git clone https://github.com/sky0504/maupassant-hexo.git themes/maupassant
  3. 修改配置,启用主题vim _config.yml(将theme: landscape更改为theme: maupassant)

  4. 修改语言vim _config.yml(更改language:language: zh-CN)

添加评论系统

  1. 注册Oauth Application
  2. 查看注册应用的id、secret等信息
  3. 修改配置文件vim _config.yml,修改gitment的有关配置信息
  4. 修改主题文件themes/maupassant/layout/_partial/comments.pug,在gitment初始化时添加一行id: '#{page.date}',,整体如下所示:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    var gitment = new Gitment({
    id: '#{page.date}',
    owner: '#{theme.gitment.owner}',
    repo: '#{theme.gitment.repo}',
    oauth: {
    client_id: '#{theme.gitment.client_id}',
    client_secret: '#{theme.gitment.client_secret}',
    },
    })
  5. 需要评论的文章,第一次点击initialize comments按钮,然后在下边的评论框填写评论即可

部署到GitHub上

  1. 修改配置_config.yml中的deploy

    1
    2
    3
    4
    deploy:
    type: git
    repo: https://github.com/sky0504/sky0504.github.io.git
    branch: master
  2. 创建文件vim source/CNAME,写入一行sky.mafeifei.cn

  3. 打开https://github.com/sky0504/sky0504.github.io.git的设置,设置Custom domainsky.mafeifei.cn
  4. 修改域名解析,添加记录类型为CNAME的一条记录,指向sky0504.github.io

添加备案信息

  1. 公安部注册备案略
  2. 下载备案图标到hexo的source目录下
  3. vim themes/maupassant/layout/_partial/footer.pug在该文件最后添加备案图标和备案号信息代码
    1
    2
    3
    4
    5
    p 浙ICP备16006348号 | 
    |
    img(src="/images/ba.png")
    |
    a(rel='nofollow', target='_blank', href='http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=33010302001929') 公安机关备案33010302001929号

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment