发表于: 2020-06-27 23:45:10

2 1854


今天完成的事情:

对注册接口加入了一个判断账户名是否已存在:

@RequestMapping(value = "register",method = RequestMethod.POST)
public String register(Account account,Model model){
if(accountService.selectAccount(account.getUsername()) == null) {
account.setCreateat(1L);
       account.setCreateby("管理员");
       account.setUpdateat(1L);
       account.setUpdateby("管理员");
       accountService.insert(account);
       model.addAttribute("msg", "注册成功");
       return "login";
   }else{
model.addAttribute("msg","该账号已存在!");
       return "register";
   }
}


昨天那个登录之后动态数据都不能显示是因为登录请求最后直接返回了myView页面,并没有发送请求,应该返回一个重定向的页面

将返回值修改一下就解决了:

return "redirect:u/profession";

通过拦截器拦截/u/profession请求:

public class MyInterceptor1 implements HandlerInterceptor {

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
       if (!requestURI.contains("/login")){
String username = (String) request.getSession().getAttribute("username");
           //判断session是否存在
           if(username == null) {
response.sendRedirect(request.getContextPath()+"/login");
               return false;
           }
}
return true;
   }
}

如果未登录访问/u/profession则直接跳转到登录页面:


接下来对用户ID和登陆时间进行加密。

数字签名、信息加密 是前后端开发都经常需要使用到的技术,应用场景包括了用户登入、交易、信息通讯、oauth 等等,不同的应用场景也会需要使用到不同的签名加密算法,或者需要搭配不一样的 签名加密算法 来达到业务目标。这里简单的给大家介绍几种常见的签名加密算法和一些典型场景下的应用。

1. 数字签名

数字签名,简单来说就是通过提供可鉴别的数字信息验证自身身份的一种方式。一套数字签名通常定义两种互补的运算,一个用于签名,另一个用于验证。分别由发送者持有能够代表自己身份的私钥(私钥不可泄露),由接受者持有与私钥对应的公钥,能够在接受到来自发送者信息时用于验证其身份。


2. 加密和解密

2.1. 加密

数据加密的基本过程,就是对原来为明文的文件或数据按某种算法进行处理,使其成为不可读的一段代码,通常称为“密文”。通过这样的途径,来达到 保护数据不被非法人窃取、阅读的目的。

2.2. 解密

加密的逆过程为解密,即将该编码信息转化为其原来数据的过程。


3. 对称加密和非对称加密

加密算法分对称加密和非对称加密,其中对称加密算法的加密与解密密钥相同,非对称加密算法的加密密钥与解密密钥不同,此外还有一类不需要密钥的散列算法。

常见的对称加密算法主要有DES、3DES、AES 等,常见的非对称算法主要有 RSA、DSA 等,散列算法主要有 SHA-1、MD5 等。


本任务要求使用DES加密

建了一个类试验一下:

package com.jnshu.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

public class DESUtils {
private static Key key;
   //设置秘钥
   private static String KEY_STR="mykey";

   static {
try {
//生成des算法对象
           KeyGenerator generator = KeyGenerator.getInstance("DES");
           //采用SHA1安全策略
           SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
           //设置密匙种子
           secureRandom.setSeed(KEY_STR.getBytes());
           generator.init(secureRandom);
           //生成密匙
           key = generator.generateKey();
           generator = null;
       } catch (Exception e) {
throw new RuntimeException(e);
       }
}

/**
    * 对字符串进行加密,返回BASE64的加密字符串
    * <功能详细描述>
    *
    * @param str
    * @return
    * @see [类、类#方法、类#成员]
    */
   public static String getEncryptString(String str) {
BASE64Encoder base64Encoder = new BASE64Encoder();
       System.out.println(key);
       try {
byte[] strBytes = str.getBytes("UTF-8");
           Cipher cipher = Cipher.getInstance("DES");
           cipher.init(Cipher.ENCRYPT_MODE, key);
           byte[] encryptStrBytes = cipher.doFinal(strBytes);
           return base64Encoder.encode(encryptStrBytes);
       } catch (Exception e) {
throw new RuntimeException(e);
       }

}

/**
    * 对BASE64加密字符串进行解密
    * <功能详细描述>
    *
    * @param str
    * @return
    * @see [类、类#方法、类#成员]
    */
   public static String getDecryptString(String str) {
BASE64Decoder base64Decoder = new BASE64Decoder();
       try {
byte[] strBytes = base64Decoder.decodeBuffer(str);
           Cipher cipher = Cipher.getInstance("DES");
           cipher.init(Cipher.DECRYPT_MODE, key);
           byte[] encryptStrBytes = cipher.doFinal(strBytes);
           return new String(encryptStrBytes, "UTF-8");
       } catch (Exception e) {
throw new RuntimeException(e);
       }

}

public static String getDecryptStringByKey(String str) {
BASE64Decoder base64Decoder = new BASE64Decoder();
       try {
byte[] strBytes = base64Decoder.decodeBuffer(str);
           Cipher cipher = Cipher.getInstance("DES");
           cipher.init(Cipher.DECRYPT_MODE, key);
           byte[] encryptStrBytes = cipher.doFinal(strBytes);
           return new String(encryptStrBytes, "UTF-8");
       } catch (Exception e) {
throw new RuntimeException(e);
       }

}


public static void main(String[] args) {
String name = "root";
       String password = "123456";
       String encryname = getEncryptString(name);
       String encrypassword = getEncryptString(password);
       System.out.println(encryname);
       System.out.println(encrypassword);

       System.out.println(getDecryptString(encryname));
       System.out.println(getDecryptString(encrypassword));
   }
}

输出结果:


不过还不懂具体原理,明天再研究研究。


明天计划完成的事情:

完成对用户ID和登录时间加密,生成Token,放入Cookie中,拦截器里通过Cookie中判断Token的有效性来判断用户是否登录。

完成任务五


返回列表 返回列表
评论

    分享到