0%

假设你想让用户名为test的用户可以在使用sudo的时候不输入密码, 你需要修改文件/etc/sudoers.d/test(没有则创建),其中test为你的用户名。文件内容为:

1
test ALL=(ALL) NOPASSWD:ALL

安装apache的htpasswd工具

使用htpasswd工具生成密码文件

1
htpasswd -bc .htpasswd user userpasswd

nginx配置

1
2
3
4
5
6
location /xxx {
root /usr/local/example;
index index.html index.htm;
auth_basic ""; # 显示给用户的提示
uth_basic_user_file /etc/nginx/.htpasswd; # 密码文件的位置
}

注意

上面配置可能造成php无法解析,此时需要在location里面再配置一层location:

1
2
3
4
5
6
7
8
9
10
location /xxx {
root /usr/local/example;
index index.html index.htm;
auth_basic ""; # 显示给用户的提示
uth_basic_user_file /etc/nginx/.htpasswd; # 密码文件的位置

location ~ .*\.(php|php5)?$ {
# ...
}
}

1
2
3
4
5
location ~ .*\.(gif|jpg|jpeg|bmp|png|mp3|wma|mp4|swf|txt)$ {
if ($query_string ~ "download=true") {
add_header Content-Disposition "attachment; filename=$request_filename";
}
}

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
String.prototype.format = function(args) {
if (arguments.length == 0) {
return this;
}
var result = this;
if (arguments.length == 1 && typeof(args) == "object") {
for (var key in args) {
if (args[key] != undefined) {
var reg = new RegExp("({" + key + "})", "g");
result = result.replace(reg, args[key]);
}
}
} else {
for (var i = 0; i < arguments.length; i++) {
if (arguments[i] != undefined) {
var reg = new RegExp("({)" + i + "(})", "g");
result = result.replace(reg, arguments[i]);
}
}
}
return result;
}

var template1 = "Hello, {0}!";
var template2 = "Hello, {name}!";
console.log(template1.format("Tom"));
console.log(template2.format({name: "Tom"}));

打开文件.vimrc_vimrc,在开头处加上:

1
2
3
4
let $LANG="zh_CN.UTF-8"
set fileencodings=utf-8,chinese,latin-1
set termencoding=utf8
set encoding=utf-8

直接使用cron启动

Dockerfile:

1
2
3
4
5
6
7
8
9
10
FROM ubuntu:14.04

RUN apt-get update && apt-get install -y cron

ADD . /app
WORKDIW /app

RUN echo '* * * * * root bash -c "/app/cron.sh"' >> /etc/crontab

CMD ["cron", "-f"]

注意

有时候在docker容器中启动定时任务的时候可能取不到环境变量,此时可以这样启动定时任务:

1
env >> /etc/environment && cron -f

使用supervisor管理

cron.conf:

1
2
3
4
5
6
[program:cron]
command=bash -c "cron -f"
user=root
autostart=true
autorestart=true
redirect_stderr=true

Dockerfile:

1
2
3
4
5
6
7
8
9
10
11
FROM ubuntu:14.04

RUN apt-get update && apt-get install -y supervisor cron

ADD . /app
WORKDIW /app

RUN echo '* * * * * root bash -c "/app/cron.sh"' >> /etc/crontab
RUN cat cron.conf > /etc/supervisor/conf.d/cron.conf

CMD ["supervisor", "-n"]

依赖Apache的commons-codec-1.9.jar

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
package utils;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.digest.DigestUtils;

/**
* MD5签名
*
* @date 2014-11-14
* @version v1.0
*/
public class MD5 {

/**
* 签名字符串
*
* @param text
* 需要签名的字符串
* @param key
* 密钥
* @return 签名结果
* @throws UnsupportedEncodingException
*/
public static String sign(String text, String key) throws UnsupportedEncodingException {
text = text + key;
return DigestUtils.md5Hex(text.getBytes("utf-8"));
}

/**
* 签名字符串
*
* @param text
* 需要签名的字符串
* @param sign
* 签名结果
* @param key
* 密钥
* @return 验证结果
* @throws UnsupportedEncodingException
*/
public static boolean verify(String text, String sign, String key) throws UnsupportedEncodingException {
text = text + key;
String mysign = DigestUtils.md5Hex(text.getBytes("utf-8"));
return mysign.equals(sign);
}
}

依赖Apache的commons-codec-1.9.jar

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
package utils;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
* AES
*
* @date 2014-11-13
* @version v1.0
*/
public class AES {

/**
* 加密
*
* @param content
* 明文
* @param password
* 密码
* @return base64编码密文
* @throws GeneralSecurityException
* @throws UnsupportedEncodingException
*/
public static String encrypt(String content, String password) throws GeneralSecurityException,
UnsupportedEncodingException {
byte[] byteContent = content.getBytes("utf-8");
return Base64.encodeBase64String(doFinal(byteContent, password, Cipher.ENCRYPT_MODE));
}

/**
* 解密
*
* @param content
* base64编码的密文
* @param password
* 密码
* @return 明文
* @throws GeneralSecurityException
*/
public static String decrypt(String content, String password) throws GeneralSecurityException {
byte[] byteContent = Base64.decodeBase64(content);
return new String(doFinal(byteContent, password, Cipher.DECRYPT_MODE));
}

private static byte[] doFinal(byte[] byteContent, String password, int mode) throws GeneralSecurityException {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key);
return cipher.doFinal(byteContent);
}
}

依赖Apache的commons-codec-1.9.jar

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
package utils;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;

/**
* RSA
*
* @date 2014-10-15
* @version v1.1
*/
public class RSA {

/** 签名算法 */
public static final String SIGN_ALGORITHM = "SHA1WithRSA";

/**
* 私钥签名
*
* @param content
* 要签名的数据
* @param privateKey
* Base64格式的私钥
* @return Base64格式的签名
* @throws GeneralSecurityException
*/
public static String sign(String content, String privateKey) throws GeneralSecurityException {
PrivateKey priKey = getPrivateKey(privateKey);
Signature signature = Signature.getInstance(SIGN_ALGORITHM);
signature.initSign(priKey);
signature.update(content.getBytes());
return Base64.encodeBase64String(signature.sign());
}

/**
* 验证签名
*
* @param content
* 数据
* @param sign
* Base64格式的签名
* @param publicKey
* Base64格式的公钥
* @return
* @throws GeneralSecurityException
*/
public static boolean verify(String content, String sign, String publicKey) throws GeneralSecurityException {
PublicKey pubKey = getPublicKey(publicKey);
Signature signature = Signature.getInstance(SIGN_ALGORITHM);
signature.initVerify(pubKey);
signature.update(content.getBytes());
return signature.verify(Base64.decodeBase64(sign));
}

/**
* 用公钥进行加密
*
* @param content
* 明文
* @param publicKey
* base64编码的公钥字符串
* @return base64编码的密文
* @throws UnsupportedEncodingException
* @throws GeneralSecurityException
*/
public static String encrypt(String content, String publicKey) throws UnsupportedEncodingException,
GeneralSecurityException {
PublicKey pubKey = getPublicKey(publicKey);
Cipher cipher = Cipher.getInstance("RSA");
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] result = cipher.doFinal(byteContent);
return Base64.encodeBase64String(result);
}

/**
* 用私钥进行解密
*
* @param content
* base64编码的密文
* @param privateKey
* base64编码的私钥字符串
* @return 明文
* @throws GeneralSecurityException
*/
public static String decrypt(String content, String privateKey) throws GeneralSecurityException {
PrivateKey priKey = getPrivateKey(privateKey);
Cipher cipher = Cipher.getInstance("RSA");
byte[] byteContent = Base64.decodeBase64(content);
cipher.init(Cipher.DECRYPT_MODE, priKey);
byte[] result = cipher.doFinal(byteContent);
return new String(result);
}

/**
* 将公钥字符串转换为PublicKey实例
*
* @param publicKey
* 公钥base64编码的字符串
* @return PublicKey实例
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
private static PublicKey getPublicKey(String publicKey) throws GeneralSecurityException {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] encodedKey = Base64.decodeBase64(publicKey);
return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
}

/**
* 将私钥字符串转换为PrivateKey实例
*
* @param privateKey
* 私钥base64编码的字符串
* @return PrivateKey实例
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
private static PrivateKey getPrivateKey(String privateKey) throws GeneralSecurityException {
PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
KeyFactory keyf = KeyFactory.getInstance("RSA");
return keyf.generatePrivate(priPKCS8);
}
}