发表于: 2017-09-11 20:00:36

1 792


今天完成的事情:

先写了一点逻辑判断登陆,没写注册

public interface UserService {
public boolean isRightUser(String user, String password);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
   private UserMapper userMapper;

   public boolean isRightUser(String user,String password){
System.out.println("输入的账号:" + user + "输入的密码:" + password);
       User user1=userMapper.selectByName(user);
       if (user1 !=null){
System.out.println("查询出来的账户:"+user1.getUser()+"密码:"+user1.getPassword());
           System.out.println("---------");
           if (user1.getUser().equals(user)&&user1.getPassword().equals(password))
return true;
       }
return false;
   }
}

controller也很简单

    @RequestMapping(value = "/login", method = RequestMethod.GET)
public String getLog() {
return "/views/login";
   }

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(HttpServletRequest request, HttpServletResponse response, User user, Model model) throws ServletException, IOException {
boolean loginType=userService.isRightUser(user.getUser(),user.getPassword());
       if (loginType){
//如果验证通过,则将用户信息传到前台
//            request.setAttribute("user",user);
           //并跳转到对应网页
           return "redirect:/home";
       }else {
//            request.setAttribute("message","用户名密码错误");
           return "redirect:/error";
       }
}

@RequestMapping(value = "/error",method = RequestMethod.GET)
public String error(){
return "error";
   }

实现了输入用户名密码然后对比数据里的用户名,通过重定向实现登陆成功或者错误

然后发现了之前弄错的地方,设计表少了登陆时间这项,加上

DES和MD5

不管那么多贴代码

public class MD5Util {
public static String stringToMD5(String string){
MessageDigest md = null;
       try {
md = MessageDigest.getInstance("MD5");
       } catch (NoSuchAlgorithmException e) {
e.printStackTrace();
       }
//计算md5函数
       md.update(string.getBytes());
       //digest()最后确定返回md5 hash值,返回值为8位zifuc,
       // 因为md5 hash值是16位的hex值,实际上就是8位的字符
       //BigInteger函数则将8位的字符串转换成16位的hex值,用字符串来表示:得到字符串形式的hash值
       return new BigInteger(1,md.digest()).toString(16);
   }
}
public class DES {
//加密
   public static byte[] encrype(byte[] datasource,String password){
try{
           SecureRandom random = new SecureRandom();
           DESKeySpec desKey = new DESKeySpec(password.getBytes());
           //创建一个秘钥工厂,然后用它把DESKeySpec转换成
           SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
           SecretKey securekry = keyFactory.generateSecret(desKey);
           //Cipher对象实际完成加密操作
           Cipher cipher = Cipher.getInstance("DES");
           //用秘钥初始化Cipher对象
           cipher.init(Cipher.ENCRYPT_MODE,securekry,random);
           //现在,获取数据并加密
           //正在执行加密操作
           return cipher.doFinal(datasource);
       }catch(Throwable e){
           e.printStackTrace();
       }
return null;
   }
//解密
   public static byte[] decrypt(byte[] src,String password) throws Exception{
//DES算法要求有一个可信任的随机数源
       SecureRandom random = new SecureRandom();
       //创建一个DESKeySpec对象
       DESKeySpec desKey = new DESKeySpec(password.getBytes());
       //创建一个密钥工厂
       SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
       //将DESKeySpec对象装换成SecretKey对象
       SecretKey secretKey = keyFactory.generateSecret(desKey);
       //Cipher对象实际完成解密操作
       Cipher cipher = Cipher.getInstance("DES");
       //用密钥初始化Cipher对象
       cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
       // 真正开始解密操作
       return cipher.doFinal(src);

   }

测试一下des加密

//测试
public static void main(String[] args){
//待加密内容
   String str = "p5no1"+ new Date().getTime();
   //密码。长度要是8的倍数
   String password = "12346578";
   byte[] result = DES.encrype(str.getBytes(),password);
   System.out.println("加密后:"+new String(result));

   //直接将如上内容解密
   try{
byte[] decryResult = DES.decrypt(result,password);
       System.out.println("解密后:"+new String(decryResult));
   }catch (Exception e){
e.printStackTrace();
   }
}

哇!都加密成乱码了,厉害

然后这个加密好了的东东就是token吧,任务要求是加密id和登陆时间,方法应该一样

学一下cookie和session

cookie和session都是会话跟踪技术,cookie通过在客户端记录信息确定用户身份,session通过在服务器端记录信息确定用户身份。

cookie

由于http协议是无状态的协议,每次数据交换完毕后,客户端和服务器的连接就会断开,再次交换数据需要建立新的连接,所以服务器无法从连接上跟踪会话,这时引入cookie,就相当于给每个客户端发一个通行证,访问的时候要携带自己的通行证才能在服务器上验证。

向客户端写入cookie,可以用HttpServletResponse.addCookie方法

服务器如何获取cookie,用HttpServletRequest.getCookies()

区分cookie要根据name属性

本来想写个cookie类来着,但没来得及就先写在controller里吧

@RequestMapping(value = "/login",method = RequestMethod.POST)
public void loginaction(@RequestParam("user") String user, @RequestParam("password") String password,
                       HttpServletResponse httpServletResponse) {
String md5= MD5Util.stringToMD5(user+password);
   if(userService.isRightUser(user,md5)){
//待加密内容
       long id=userService.selectByName(user).getId();
       long creatDate=new Date().getTime();
       String str = id+"="+creatDate;
       //加密操作
       byte[] result = DES.encrype(str.getBytes() ,"12345678");
       //把加密的字节转换为16进制
       String resules= TypeUtil.bytesToHexString(result);
       Cookie cookie = new Cookie("token",resules);
       cookie.setMaxAge(60*10);//保留10分钟
       httpServletResponse.addCookie(cookie);
       try {
httpServletResponse.sendRedirect("home");
       } catch (IOException e) {
e.printStackTrace();
       }
}else {
try {
httpServletResponse.sendRedirect("error");
       } catch (IOException e) {
e.printStackTrace();
       }
}
}

看看结果..

额,密码输对了也跳到error了,为什么呢?

额,我这个先加密后进行的逻辑判断,尴尬

再改



明天计划的事情:

cookie还没搞懂,其实加密也没怎么仔细看_(:з」∠)_,先搞懂再说

遇到的问题:

脑子有点乱,有问题再说

收获:

以上


返回列表 返回列表
评论

    分享到