发表于: 2017-11-17 20:50:45
1 672
今天完成的事
【了解了一下cookie和session】
cookie是由服务器生成,通过response将cookie写回浏览器(set-cookie),保留在浏览器上,
下一次访问,浏览器根据一定的规则携带不同的cookie(通过request的头 cookie),我们服务器就可以接受cookie
cookie的api:
new Cookie(String key,String value)
写回浏览器:
response.addCookie(Cookie c)
获取cookie:
Cookie[] request.getCookies()
cookie的常用方法:
getName():获取cookie的key(名称)
getValue:获取指定cookie的值
session:
服务器端会话技术.
当我们第一次访问的服务器的时候,服务器获取id,
能获取id
要拿着这个id去服务器中查找有无此session
若查找到了:直接拿过来时候,将数据保存,需要将当前sessin的id返回给浏览器
若查找不到:创建一个session,将你的数据保存到这个session中,将当前session的id返回给浏览器
不能获取id
创建一个session,将你的数据保存到这个session中,将当前session的id返回给浏览器
获取一个session:
HttpSession request.getSession()
域对象:
xxxAttribute
生命周期:
创建:第一次调用request.getsession()创建
销毁:
服务器非正常关闭
session超时
默认时间超时:30分钟 web.xml有配置
手动设置超时:setMaxInactiveInterval(int 秒) 了解
看了一下参考资料的EDS介绍。
大概理解了EDS的加密算法和揭密算法。
真的是大概理解,理解的相当的大概。
然后我就头疼,我以为这个东西要自己写。
后来实在憋不出来问了一下师兄,告诉我去找一个实例抄下来就行。
合计我搁着寻思的都是些个没用的。
找了半天找到一个经调试可以用的。
package com.xiuzhen.utils;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
/**
* Created by ${MIND-ZR} on 2017/11/17.
*/
public class DesEncryptDecrypt {
private static DesEncryptDecrypt ourInstance = new DesEncryptDecrypt();
public static DesEncryptDecrypt getInstance() {
return ourInstance;
}
private Cipher ecipher,dcipher;
private DesEncryptDecrypt(){
DESKeySpec dks;
try {
String key = "squirrel123";
//Constants.EncryptDecryptKEY是我一个常量类中的字符串而已,它就是加密解密的密钥。请自行替换。
dks = new DESKeySpec(key.getBytes());
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey desKey = skf.generateSecret(dks);
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, desKey);
dcipher.init(Cipher.DECRYPT_MODE, desKey);
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
}
public String encrypt(String str) throws Exception {
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
public String decrypt(String str) throws Exception {
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
}
写了一个登陆页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 style="text-align: center;">用户登陆</h1>
<center>
<form action='/StuDormManager2/LoginServlet' method='post'>
用户名: <input type='text' name='username'/><br/>
密 码: <input type='password' name='password'/><br/>
<input type='submit' value='登陆' /> <br/>
</form>
</center>
</body>
</html>
遇到的问题
理解DES有点跑偏,把任务想复杂了可能是。
收获
明天的计划
任务五
评论