1.md5
三步:
1.獲取一個messageDigest加密對象,加密方式為md5
2.獲取一base64encoder對象矛双,用于最后輸出base64編碼
3.md5加密對象給str加密后渊抽,用base64輸出。完成加密
public static String encoderStrByMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest msgDigest = MessageDigest.getInstance("MD5");
BASE64Encoder base64En = new BASE64Encoder();
return base64En.encode(msgDigest.digest(str.getBytes("utf-8")));
}
2.Properties文件使用
用與記錄系統(tǒng)的配置议忽。數(shù)據(jù)庫名稱懒闷。路徑。密碼栈幸。文件路徑等愤估。配套寫個PropertiesUtil用于使用Properties文件
properites文件格式:
key1=value1
key2=value2
Util:1.為本類獲得資源文件,即將properties文件打成輸入流
2.創(chuàng)建一個Properties類對象 加載資源輸入流
3.調(diào)用Properties對象的get()方法速址,傳入key正取到value
public static String getValueForKey(String key){
Properties properties = new Properties();
InputStream input = new PropertiesUtil().getClass().getResourceAsStream("/diary.properties");
try {
properties.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (String)properties.get(key);
}
3.Cookies 用于記住密碼操作
cookies用戶記住密碼玩焰。在用戶登錄成功之后將cookies通過響應(yīng)response.addcookie存放到瀏覽器中
// 登錄成功
// 如果選擇了記住密碼
if (remember.equals("remember-me")) {
this.rememberMe(userName, password, response);
}
//記住密碼
private void rememberMe(String username,String password,HttpServletResponse response) {
Cookie cookie = new Cookie("user", username+"-"+password);
cookie.setMaxAge(1*60*60*24*7);//cookie有效期一周
response.addCookie(cookie);
}
在jsp頁面中嵌入java代碼
一定判斷用戶是不是第一次登錄,是的話從cookies中取得記住的用戶名密碼芍锚,不是的話就有服務(wù)器轉(zhuǎn)發(fā)的昔园,因為服務(wù)器轉(zhuǎn)發(fā)一般通過request,session并炮,所以吧cookies中的用戶名密碼放到pageContext中默刚,讓el表達式優(yōu)先取得cookies中的用戶名密碼
<%
if(request.getAttribute("user")==null){//第一次用戶登錄,不是后臺回調(diào)轉(zhuǎn)發(fā)的
String userName = null;
String password = null;
Cookie[] cookies = request.getCookies();
for(int i = 0 ; cookies!=null && i<cookies.length ; i++){
if(cookies[i].getName().equals("user")){
userName = cookies[i].getValue().split("-")[0];
password = cookies[i].getValue().split("-")[1];
}
}
if(userName == null){
userName = "";
}
if(password == null){
password = "";
}
//放到pageContext 讓EL表達式優(yōu)先獲取
pageContext.setAttribute("user", new User(userName,password));
}%>
4.一個關(guān)于jdbc鏈接的bug
WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
首先恭喜逃魄,出現(xiàn)這個的時候MySQL說明已經(jīng)安裝成功了荤西,這是警告不是錯誤,以后使用是不影響的嗅钻。大概的意思就是說建立ssl連接皂冰,但是服務(wù)器沒有身份認(rèn)證,這種方式不推薦使用养篓。
解決辦法:
原來的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "letmein");
現(xiàn)在的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false","root", "letmein");