Hexo


  • 首页

  • 关于

  • 分类

  • 归档

https接口调用去掉证书验证

发表于 2019-12-06 | 分类于 日常踩坑 | 阅读次数:

https接口调用示例

1、POST格式调用

通过POST方式调用https接口

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
TrustManager[] tm = {new HttpsManager()};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tm, new SecureRandom());
SSLSocketFactory ssf = sslContext.getSocketFactory();
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setUseCaches(false);
httpsURLConnection.setInstanceFollowRedirects(true);
httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpsURLConnection.setHostnameVerifier(new HttpsManager().new TrustAnyHostnameVerifier());
httpsURLConnection.setSSLSocketFactory(ssf);
httpsURLConnection.setDoOutput(true);
httpsURLConnection.connect();
DataOutputStream out = new DataOutputStream(httpsURLConnection.getOutputStream());
out.write(data.getBytes("UTF-8"));
out.flush();
out.close();
BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), "UTF-8"));
String lines;
while ((lines = responseReader.readLine()) != null) {
result.append(lines);
}
responseReader.close();
// 断开连接
httpsURLConnection.disconnect();

2、GET格式调用

通过GET方式调用https接口
相比较于POST方式调用https接口,GET方式要注意一下几点

  • httpsURLConnection.setRequestMethod(“GET”)
  • 去掉httpsURLConnection.setDoOutput(true)
  • 去掉new DataOutputStream(httpsURLConnection.getOutputStream())。否则即使将RequestMethod设置为GET后,通过new DataOutputStream()也==会将RequestMethod重置为POST==
    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
    TrustManager[] tm = {new HttpsManager()};
    SSLContext sslContext = SSLContext.getInstance("TLS");
    sslContext.init(null, tm, new SecureRandom());
    SSLSocketFactory ssf = sslContext.getSocketFactory();
    HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
    //将POST改为GET
    httpsURLConnection.setRequestMethod("GET");
    httpsURLConnection.setUseCaches(false);
    httpsURLConnection.setInstanceFollowRedirects(true);
    httpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    httpsURLConnection.setHostnameVerifier(new HttpsManager().new TrustAnyHostnameVerifier());
    httpsURLConnection.setSSLSocketFactory(ssf);
    //httpsURLConnection.setDoOutput(true);
    httpsURLConnection.connect();
    /**
    DataOutputStream out = new DataOutputStream(httpsURLConnection.getOutputStream());
    out.write(data.getBytes("UTF-8"));
    out.flush();
    out.close();
    **/
    BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), "UTF-8"));
    String lines;
    while ((lines = responseReader.readLine()) != null) {
    result.append(lines);
    }
    responseReader.close();
    // 断开连接
    httpsURLConnection.disconnect();

不校验https证书的调用方式

某些情况下服务端只能通过https进行访问,但服务端并没有正式合法证书,这时可以通过绕过证书校验的方式访问到服务端,修改点如下(==以GET方式为例==):

  1. 重写TrustManager
1
2
3
4
5
6
7
8
9
10
11
12
13
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
@Override
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
@Override
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
  1. 初始化sslContext
1
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
  1. 重写HostnameVerifier
1
2
3
4
5
6
7
HostnameVerifier allHostsValid = new HostnameVerifier(){
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}

};
  1. HostnameVerifier属性赋值
1
httpsURLConnection.setHostnameVerifier(allHostsValid);

获取http接口响应的小窍门

在服务端返回非成功(200)的状态码时,也可以通过==httpsURLConnection.getErrorStream==()获取返回的错误信息用于问题定位

1
2
3
4
5
6
7
8
9
10
11
12
13
BufferedReader responseReader = null;
if (code == 200) {
responseReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream(), "UTF-8"));
} else {
responseReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getErrorStream(), "UTF-8"));
}
String lines;
while ((lines = responseReader.readLine()) != null) {
result.append(lines);
}
responseReader.close();
// 断开连接
httpsURLConnection.disconnect();

【高级功能】实体类字段自动加解密

发表于 2019-12-06 | 分类于 学习笔记 > Mybatis(Plus) | 阅读次数:
针对项目安全性要求,对某些敏感字段数据需要进行加密存储,通过mybatis plus和自定义注解方式来完成对敏感字段的自动加解密

自定义注解

在需要加解密的实体类型注解@==EncryptionClass==,在需要加解密的字段上注解@==EncryptionField==

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 对实体类打标
*/
@Documented
@Inherited
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptionClass {
}

/**
* 对实体类的字段打标
*/
@Documented
@Inherited
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptionField {
SecretMode mode() default SecretMode.AES;
}

自定义mybatis拦截器

入库或更新时拦截器

ParamInterceptor.java
image

==注意:==

1、@Intercepts注解。method为update代表插入和更新时执行

1
2
3
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class})
})

2、@ConditionalOnProperty注解。根据yml文件中==entity.encrypt==与==havingValue==值进行比较,一致则拦截器起效,不一致则拦截器不起效

1
@ConditionalOnProperty(prefix = "entity", value = "encrypt", havingValue = "true")

查询时拦截器

ResultInterceptor.java

==注意点同入库拦截器==

加解密工具类

SensitiveUtils.java

【高级功能】公共字段自动填充

发表于 2019-12-06 | 分类于 学习笔记 > Mybatis(Plus) | 阅读次数:
对于一些基础字段,比如创建时间、修改时间、创建人、修改人等基础字段,可以使用mybatis plus的公共字段自动填充功能进行自动入库、更新,减少手写代码及代码耦合。

提取公共字段

将需要自动填充的功能字段提取出来,形成父类,所有有这些字段的实体继承父类即可

@TableField注解

fill 值为INSERT时代表insert方法生效,INSERT_UPDATE代表insert和update方法都生效

1
2
3
 @TableField(fill = FieldFill.INSERT)

@TableField(fill = FieldFill.INSERT_UPDATE)

自定义公共字段填充处理器

实现MetaObjectHandler接口,重新update和insert方法

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
@Log4j2
@Component
public class MetaHandler implements MetaObjectHandler {
/**
* 更新数据执行
* @param metaObject
*/
@Override
public void updateFill(MetaObject metaObject) {
String userId = BaseContextHandler.getUserID();
this.setFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
this.setFieldValByName("updatedBy", userId!=null?userId:"auto", metaObject);
}



/**
* 新增数据执行
* @param metaObject
*/
@Override
public void insertFill(MetaObject metaObject) {
String userId = BaseContextHandler.getUserID();
this.setFieldValByName("createdTime", LocalDateTime.now(), metaObject);
this.setFieldValByName("createdBy", userId!=null?userId:"auto", metaObject);
this.setFieldValByName("updatedTime", LocalDateTime.now(), metaObject);
this.setFieldValByName("updatedBy", userId!=null?userId:"auto", metaObject);
}
}

【基础使用】增删改查

发表于 2019-12-06 | 分类于 学习笔记 > Mybatis(Plus) | 阅读次数:

根据generate自动生成的数据库映射entity代码

image

根据generate自动生成的mapper文件,继承BaseMapper

image

使用BaseMapper中自带API即可满足单表简单查询的大部分需求

image

service层继承ServiceImpl有部分进阶API可供使用,比如批量入库,批量更新等

image

【基础使用】条件查询

发表于 2019-12-06 | 分类于 学习笔记 > Mybatis(Plus) | 阅读次数:
条件查询通过LambdaQueryWrapper条件查询包装器完成

LambdaQueryWrapper使用方式

image

LambdaQueryWrapper条件参数说明

image

通过BaseMapper中API通过条件构造器参数获取结果

image

【基础使用】分页查询

发表于 2019-12-06 | 分类于 学习笔记 > Mybatis(Plus) | 阅读次数:

创建项目分页类

QueryPageVo.java

==前端查询条件要封装成一个名为vo的实体对象==

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
public class QueryPageVo<T,E> extends AbstractVo<E> implements IPage<T> {
/**
* 每页第一行 index,从0开始
*/
@Getter
@Setter
private long index;
/**
* 每页记录数
*/
@Getter
@Setter
private long pageSize;
/**
* 当前页结果集
*/
private List<T> rows = new ArrayList<>();
/**
* 总记录数
*/
@Getter
@Setter
private long total;
/**
* 总页数
*/
@Getter
@Setter
private int totalPageCount;
}

后台查询条件、分页条件参数接收

image

单表分页查询

使用BaseMapper自带selectPage传入指定参数即可
image

多表关联分页查询

需要手写查询sql,封装分页查询结果

service层调用mapper手写分页方法

image

对应的mapper.xml文件

==注意点:==

  • 需要指定参数类型和返回值类型
  • 查询条件直接从封装好的名为vo的实体类中调用即可

image

【基础使用】项目配置

发表于 2019-12-06 | 分类于 学习笔记 > Mybatis(Plus) | 阅读次数:

基于Springboot的MybatisPlus配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
mybatis-plus:
# 扫描 mapper.xml
mapper-locations: classpath*:/mapper/*Mapper.xml
#typeAliasesPackage: com.act.service.entity
global-config:
#banner: false
db-config:
id-type: INPUT #默认值0
logic-delete-value: 1 #默认值1
logic-not-delete-value: 0
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
#pagehelper分页插件配置
pagehelper:
helperDialect: mysql #设置sql语言
reasonable: true
supportMethodsArguments: true
params: count=countSql

Nacos安装

发表于 2019-12-06 | 分类于 组件安装 | 阅读次数:

Nacos致力于帮助您发现、配置和管理微服务,将使用Nacos作为微服务架构中的注册中心(替代:eurekba、consul等传统方案)以及配置中心(spring cloud config)来使用。

Nacos下载

https://pan.baidu.com/s/185VXiqRw0yK73IvI1ICeXA

提取码:i5mn

单机版安装

安装

将压缩包上传至服务器并解压

启动

进行安装目录/bin,执行命令

1
sh startup.sh -m standalone

启动完成之后,访问:http://IP:8848/nacos/ ,默认用户名密码为:nacos

集群版安装

image

MySQL数据源配置

  1. 第一步:初始化MySQL数据库,数据库初始化文件:nacos-mysql.sql,该文件可以在Nacos程序包下的conf目录下获得。
  2. 第二步:修改conf/application.properties文件,增加支持MySQL数据源配置,添加(目前只支持mysql)数据源的url、用户名和密码。配置样例如下:
1
2
3
4
5
6
spring.datasource.platform=mysql

db.num=1
db.url.0=jdbc:mysql://localhost:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=

集群配置

在Nacos的conf目录下有一个cluster.conf.example,可以直接把example扩展名去掉来使用,也可以单独创建一个cluster.conf文件,然后打开将后续要部署的Nacos实例地址配置在这里。

本文以在本地不同端点启动3个Nacos服务端为例,可以如下配置:

1
2
3
127.0.0.1:8841
127.0.0.1:8842
127.0.0.1:8843

启动实例

生产环境下直接执行命令即可 sh startup.sh

本地安装集群,则需要指定端口启动各个实例,修改startup.sh脚本中的Dserver.port即可

Proxy配置

在Nacos的集群启动完毕之后,根据架构图所示,我们还需要提供一个统一的入口给我们用来维护以及给Spring Cloud应用访问。简单地说,就是我们需要为上面启动的的三个Nacos实例做一个可以为它们实现负载均衡的访问点。这个实现的方式非常多,这里就举个用Nginx来实现的简单例子吧。

  • Nginx安装

传送门:http://note.youdao.com/noteshare?id=fd83221332c62e79f1bc5deb579aab64&sub=3DE8BD1782EF4741A34A9E87E2C9762F

  • Nginx配置

image

Nginx安装

发表于 2019-12-06 | 分类于 组件安装 | 阅读次数:

安装依赖包

1
2
//一键安装上面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

下载Nginx安装包

任意目录下载安装包(/usr/local/目录下除外)。如果在/usr/local/目录下,make install时会报错(复制配置文件已存在)

1
wget  http://nginx.org/download/nginx-1.13.11.tar.gz

创建Nginx安装目录

我习惯于创建在/usr/local/目录下

1
mkdir -p /usr/local/nginx

编译Nginx

在安装包目录下解压安装包,执行./configure命令,prefix指定创建的安装目录

1
2
./configure --prefix=/usr/local/nginx
make && make install

Nginx配置文件

配置文件中可更改Nginx监听端口(默认80,一般和Apache服务端口冲突)

Nginx命令

在安装目录(/usr/local/nginx/sbin)下

1
2
3
./nginx             启动
./nginx -s stop 停止
./nginx -s reload 重启

Hexo博客迁移

发表于 2019-08-13 | 分类于 Hexo | 阅读次数:
背景:
将Hexo环境源码上传至github后,在新电脑(什么环境都未安装)上如何将Hexo环境复刻下来,重新开始写博客上传

一、组件安装

1、git

1
yum install -y git

2、nodejs

1
yum install -y nodejs

3、npm

1
yum install -y npm

4、hexo-cli

1
yum install -y hexo-cli

二、环境复刻

1、将github上hexo源码分支clone至本地(我的分支名为hexo)

1
git clone -b hexo https://github.com/Liang2L/liang2l.github.io.git

2、依赖安装

1
npm install

== 备注 ==:在npm安装依赖时,可能会报错

1
npm: relocation error: npm: symbol SSL_set_cert_cb, version libssl.so.10 not defined in file libssl.so.10 with link time reference

此时将openssl更新即可

1
yum update -y openssl

3、安装git推送环境

1
2
3
4
5
6
7
8
9
10
11
12
git config --global user.name "Liang2L"
git config --global user.email "469449505@qq.com"

//检查输入是否正确
git config user.name
git config user.email

//创建SSH
ssh-keygen -t rsa -C "469449505@qq.com"

//获取公钥
cat /root/.ssh/id_rsa.pub

4、在github上settings中创建SSH,将获取的公钥复制进去即可

1
2
//查看SSH是否创建成功
ssh -T git@github.com

三、新环境上更新博客

1、将环境源码推送至hexo分支上

1
2
3
git add .
git commit -m "hexo迁移测试"
git push origin hexo

2、将生成的html静态文件推送至master分支上

1
2
hexo g
hexo d

参考文档:
安装:https://blog.csdn.net/sinat_37781304/article/details/82729029
迁移:https://blog.csdn.net/White_Idiot/article/details/80685990

123

Liang2L

21 日志
12 分类
© 2019 Liang2L
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4