Bean的冒险船


  • 首页

  • 归档

  • 标签

  • 公益404

  • 搜索

CryptoJS的使用引入

发表于 2017-11-30

CryptoJS是原生javascript写的加密类库,提供了各种编码和加密算法,如MD5、SHA-1、SHA-256、AES、HMAC、PBKDF等。
以常见的Md5摘要及Base64编码为例,测试代码如下:

index.html

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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>测试加密</title>
</head>
<body>
<script src="js/jquery-3.2.1.js" type="text/javascript"></script>
<script src="js/core.js" type="text/javascript"></script>
<script src="js/md5.js" type="text/javascript"></script>
<script src="js/enc-base64.js" type="text/javascript"></script>
<script src="js/main.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
console.log("-----start------");
var origin = "a12345678";
console.log("origin string is :" + origin);
console.log("md5 hex string is :" + md5HexUtil(origin));
console.log("md5 byte direct to base64 method1 is :" + md5ByteDirectToBase64UtilMethod1(origin));
console.log("md5 byte direct to base64 method2 is :" + md5ByteDirectToBase64UtilMethod2(origin));
console.log("base4 encode finish :" + base64Encode(origin));
console.log("base4 decode finish :" + base64Decode(base64Encode(origin)));
console.log("md5 hex string, and then base4 is :" + base64Encode(md5HexUtil(origin)));
console.log("-----end--------");
});
</script>
</body>
</html>
阅读全文 »

SpringBoot+Mybatis配置多数据源

发表于 2017-10-09

开发企业级应用程序时常遇到要使用多个服务器上数据库的情况,此时需要配置多个数据源。查阅相关资料,遇到不少坑,亲测如下:

项目目录结构

multi_dataSource_dic

关键文件

pom文件配置:
阅读全文 »

Nginx介绍

发表于 2017-09-16

优劣势

Nginx是一款高性能的HTTP和反向代理服务器(安装),有如下优势:

  • 高并发量:官方给出的数据,可以支持高达50000个并发连接数的响应。
  • 轻量级,内存消耗小:起Web服务处理静态资源,相比Tomcat占用更少的内存和资源。
  • 简单稳定:配置简单,基本在一个conf文件中配置,性能稳定,可7*24不间断运行。
  • 模块化程度高、低成本、跨系统。

相比而言,劣势如下:

  • 动态处理差:Nginx处理静态文件消耗内存小,但是处理动态页面较弱,一般使用Nginx作为反向代理抗压力,Tomcat作为后端处理动态请求。
  • rewrite弱:虽然可以根据域名等不同可以将Http请求分发到不同的后端服务器组,但是相比Tomcat, Nginx的rewrite功能较弱。

配置

配置整体结构

阅读全文 »

Java 阻塞队列实现超时机制

发表于 2017-08-25

阻塞队列概念

定义:阻塞队列 (BlockingQueue)是Java util.concurrent包下重要的数据结构,BlockingQueue提供了线程安全的队列访问方式:当阻塞队列进行插入数据时,如果队列已满,线程将会阻塞等待直到队列非满;从阻塞队列取数据时,如果队列已空,线程将会阻塞等待直到队列非空。并发包下很多高级同步类的实现都是基于BlockingQueue实现的。
BlockingQueue是个接口,实现有ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue、PriorityBlockingQueue、SynchronousQueue。
本文使用ArrayBlockingQueue:有界的阻塞队列,内部实现是将对象放到一个数组中。jdk源码为:

1
2
3
4
5
6
7
8
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}

参数capacity为数组容量;fair为true,插入或删除对象时,按FIFO顺序处理。

阻塞队列工具实现

阅读全文 »

Java过滤Emoji字符

发表于 2017-08-25

微信昵称支持Emoji表情,存储微信昵称时,若线上mysql编码未采用utfmb4,依然使用utf-8,向数据库写数据时就要过滤掉昵称中的Emoji表情。
过滤emoji可选用以下方法:

方法一:

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
public class EmojiFilter {
/**
* 检测是否有emoji字符
* @param source
* @return 一旦含有就抛出
*/
public static boolean containsEmoji(String source)
{
if (source == null || "".equals(source))
{
return false;
}
int len = source.length();
for (int i = 0; i < len; i++ )
{
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint))
{
// do nothing,判断到了这里表明,确认有表情字符
return true;
}
}
return false;
}
private static boolean isEmojiCharacter(char codePoint)
{
return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA)
|| (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF))
|| ((codePoint >= 0xE000) && (codePoint <= 0xFFFD))
|| ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF));
}
/**
* 过滤emoji 或者 其他非文字类型的字符
* @param source
* @return
*/
public static String filterEmoji(String source)
{
if (!containsEmoji(source))
{
return source;// 如果不包含,直接返回
}
// 到这里铁定包含
StringBuilder buf = null;
int len = source.length();
for (int i = 0; i < len; i++ )
{
char codePoint = source.charAt(i);
if (isEmojiCharacter(codePoint))
{
if (buf == null)
{
buf = new StringBuilder(source.length());
}
buf.append(codePoint);
}
else
{}
}
if (buf == null)
{
return source;// 如果没有找到 emoji表情,则返回源字符串
}
else
{
if (buf.length() == len)
{// 这里的意义在于尽可能少的toString,因为会重新生成字符串
buf = null;
return source;
}
else
{
return buf.toString();
}
}
}
}
阅读全文 »

Hibernate Validator校验请求参数

发表于 2017-08-24

服务端收到通过拦截器的请求后,第一步,即为对请求参数的合法性校验。
合法性校验依次为三部分:

  • 参数是否存在
  • 参数类型是否合法
  • 其他复杂校验

请求校验

一般的参数校验举例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public HashMap<String, Object> roleInfo(HttpServletRequest request) {
HashMap<String, Object> result = new HashMap<String, Object>();
String roleId;
try {
roleId = request.getParameter("id");
} catch (Throwable t) {
logger.error("ProxyController roleInfo args error:" + t);
result.put("result", StatusCode.ILLEGAL_PARAM.getValue());
result.put("description", StatusCode.ILLEGAL_PARAM.getDescription());
return result;
}
if (!LegalUtil.isLegalRoleId(roleId)) {
logger.error("ProxyController roleinfo roleId error:" + roleId);
result.put("result", StatusCode.ILLEGAL_PARAM.getValue());
result.put("description", StatusCode.ILLEGAL_PARAM.getDescription());
return result;
}
return proxyService.getRoleInfo(Integer.valueOf(request.getSession().getAttribute("agencyID").toString()), Integer.valueOf(roleId));
}

如果采用如下方式接受传入参数,看起来好像可以解决校验臃肿的问题:

1
2
3
4
5
6
7
8
9
public HashMap<String, Object> roleInfo(@RequestParam(value = "id") int id) {
if (!LegalUtil.isLegalRoleId(roleId)) {
logger.error("ProxyController roleinfo roleId error:" + roleId);
result.put("result", StatusCode.ILLEGAL_PARAM.getValue());
result.put("description", StatusCode.ILLEGAL_PARAM.getDescription());
return result;
}
return proxyService.getRoleInfo(Integer.valueOf(request.getSession().getAttribute("agencyID").toString()), Integer.valueOf(roleId));
}

但是如果参数过多呢,列出所有传参,代码显示较多,而且耦合较高,无法复用。
统一提出为Model,使用validation校验,采用bean注入方式调用,使代码看起来不那么臃肿,层次分明,Model大多时候可以复用,如下所示:

阅读全文 »

Java中RSA的使用

发表于 2017-08-20

RSA为非对称加密,根据应用场景不同,分如下两种:
1.签名:使用私钥加密,公钥解密。用于验证私钥持有者的身份,防止私钥所有者发布的内容被篡改,但是公钥一般是公开的,所以无法保证加密内容不被其他人获得。
2.加密:公钥加密,私钥解密。加密内容无法被他人获得,但是信息可能被他人篡改伪造。
以下为Java中RSA算法的实现:

阅读全文 »

mysql分页

发表于 2017-08-17

mysql使用limit,offset实现分页。
limt 3 offset 5 效果同 limit 5,3
都表示偏移5,从第6条开始读,读3条记录。示例如下:

1
2
3
4
5
6
7
8
9
10
select
date_format(createTime, "%Y-%m-%d %T") as time,buyerId,commission
from
partner_commission_record
where
date_format(createTime, "%Y-%m-%d") between #{date_from} and #{date_to}
and agencyId = #{agencyId} and (rebateLV=1 or rebateLV=2) and isCancel=0
order by
createTime desc
limit #{limit} offset #{offset};

阅读全文 »

mysql插入测试数据示例

发表于 2017-08-08

在接口自测及联调阶段,需要伪造数据供测试使用,以如下简单程序示例循环插入数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
drop procedure if exists test_insert;
DELIMITER //
create procedure test_insert()
begin
declare i int;
set i = 1000;
while i<1050 do
insert into account(agencyID,userName,referrer,account,weixin,card,createTime,verification,status,payStatus,firstLoginStatus) values(i,concat("test",i),2002428,concat(18841000,i),concat("testweixin",i),1000,date(now()),1,3,1,1);
set i = i+1;
end while;
end //
DELIMITER ;
call test_insert();

注释:DELIMITER作用是告诉Mysql解释器,该段命令是否已经结束。默认情况下,delimiter是分号;mysql一遇到分号,就要自动执行,所以需要事先把delimiter换成其它符号,如//或$$,最后再重置为默认值。

简谈mysql中utf8mb4

发表于 2017-08-06

最近开发遇到当Mysql存储微信昵称,而微信昵称有emoji表情符号时,数据库存储数据出现乱码。原因是mysql支持的utf8编码最大长度为3字节,如果遇4字节的宽字符(emoji表情符号、不常用的汉字、新增的Unicode字符等)就会插入异常。
Mysql在5.5.3之后增加utf8mb4(most bytes 4)编码,专门用于兼容四字节的unicode,一般情况。为了节省空间,使用utf8即可,但是建议使用utf8mb4。

Linux下更改mysql编码方式

使用Linux的发行版本为Centos

首先查看mysql版本,是否支持utf8mb4(5.5.3版本及以上)。

查看版本简易方法:
终端下:mysql -V
mysql下:status; 或 select version();

修改mysql配置文件

修改my.conf(windows下为my.ini)
修改前后的配置文件及系统编码分别如下:

阅读全文 »
1…345…7
MonkeyBean

MonkeyBean

行云流水 天马行空

62 日志
16 标签
GitHub 微博
友情链接
  • 耀耀的博客
  • 7nocturnal的博客
  • xiaofeng的博客
  • Angus的博客
© 2016 - 2022 MonkeyBean
由 Hexo 强力驱动
主题 - NexT.Pisces