用户模块之用户登录相关(有sql注入攻击)

在这里关系到异常的处理,一种是声明式异常,一种是编程式异常,在这里用了编程式异常

首先要建立两个类,都继承自RuntimeException

/**
  * 用户登录
  * @param userId
  * @param password
  * @return
  */
 public User login(String userId, String password) {
  User user = findUserById(userId);
  if (user == null) {
   throw new UserNotFoundException("用户代码不正确,代码=【" + userId + "】");
  }
  if (!user.getPassword().equals(password)) {
   throw new PasswordNotCorrentException("密码错误");
  }
  return user;
 }

 

注意,当你用字符串拼接的方式用Statement来对登录进行处理的话就有可能会发生sql注入式的攻击


// /**
//  * 用户登录
//  *
//  * 演示sql注入,输入如下数据:
//  * 用户名称:sdfsdfdsf
//  * 密码:' or '1'='1
//  *
//  * 生成的sql如下:
//  * select * from t_user where user_id='sdfsdfdsf' and password='' or '1'='1 '
//  *
//  * @param userId
//  * @param password
//  * @return
//  */
// public boolean login(String userId, String password) {
//  String sql = "select * from t_user where user_id='" + userId + "' and password='" + password + "'";
//  System.out.println(sql);
//  Connection conn = null;
//  PreparedStatement pstmt = null;
//  ResultSet rs = null;
//  boolean success = false;
//  try {
//   conn = DbUtil.getConnection();
//   pstmt = conn.prepareStatement(sql);
//   rs = pstmt.executeQuery();
//   if (rs.next()) {
//    success = true;
//    System.out.println("用户和密码正确!");
//   }
//  }catch(SQLException e) {
//   e.printStackTrace();
//  }finally {
//   DbUtil.close(rs);
//   DbUtil.close(pstmt);
//   DbUtil.close(conn);
//  }
//  return success;
// }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yjsuge

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值