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,973评论 19 139
  • 一、概念(载录于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434阅读 8,440评论 6 152
  • Http协议详解 标签(空格分隔): Linux 声明:本片文章非原创,内容来源于博客园作者MIN飞翔的HTTP协...
    Sivin阅读 5,252评论 3 82
  • 最近又有一篇爆款文章刷遍朋友圈,文章出自当红励志作家,叫做《20出头的贫穷,恰恰是你最好的增值期》,标题给力,观点...
    老苹果2阅读 963评论 10 12
  • “拼多多版今日头条”趣头条在纳斯达克上市,前几天,中国第二大搜索引擎搜狗推出搜狗号。 不只是趣头条,百度百家号、阿...
    meijia678阅读 287评论 0 0