2016.8.26日-學習java中的md5加密,properties文件使用,cokies

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文件使用

用與記錄系統的配置。數據庫名稱。路徑。密碼。文件路徑等。配套寫個PropertiesUtil用于使用Properties文件
properites文件格式:
key1=value1
key2=value2
Util:1.為本類獲得資源文件,即將properties文件打成輸入流
2.創建一個Properties類對象 加載資源輸入流
3.調用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通過響應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中取得記住的用戶名密碼,不是的話就有服務器轉發的,因為服務器轉發一般通過request,session,所以吧cookies中的用戶名密碼放到pageContext中,讓el表達式優先取得cookies中的用戶名密碼

 <%
    if(request.getAttribute("user")==null){//第一次用戶登錄,不是后臺回調轉發的
        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表達式優先獲取
        pageContext.setAttribute("user", new User(userName,password));
    }%>


4.一個關于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.

首先恭喜,出現這個的時候MySQL說明已經安裝成功了,這是警告不是錯誤,以后使用是不影響的。大概的意思就是說建立ssl連接,但是服務器沒有身份認證,這種方式不推薦使用。

解決辦法:

  原來的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "letmein");

  現在的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false","root", "letmein");
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • 一、概念(載錄于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434閱讀 8,413評論 6 152
  • Http協議詳解 標簽(空格分隔): Linux 聲明:本片文章非原創,內容來源于博客園作者MIN飛翔的HTTP協...
    Sivin閱讀 5,251評論 3 82
  • 最近又有一篇爆款文章刷遍朋友圈,文章出自當紅勵志作家,叫做《20出頭的貧窮,恰恰是你最好的增值期》,標題給力,觀點...
    老蘋果2閱讀 952評論 10 12
  • “拼多多版今日頭條”趣頭條在納斯達克上市,前幾天,中國第二大搜索引擎搜狗推出搜狗號。 不只是趣頭條,百度百家號、阿...
    meijia678閱讀 275評論 0 0