亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

驗證用戶 Mysql Java?

驗證用戶 Mysql Java?

素胚勾勒不出你 2022-11-30 16:09:20
我用這個開發做了這個查詢但是我在接收 API 時遇到問題我嘗試驗證用戶登錄但總是 API RESPONSE LOGIN SUCCESS我用 Java Spring Boot 和 Mysql 做了這個這是我的代碼:@Overridepublic List<UserDto> getUsers() {    List<UserDto> list = new ArrayList<UserDto>();    try {        Connection con = getConnection();        PreparedStatement ps = con.prepareStatement("Select * from tableregister");        ResultSet rs = ps.executeQuery();        while (rs.next()) {            UserDto data = new UserDto();            data.setName(rs.getString("name"));            data.setLastname(rs.getString("lastname"));            data.setEmail(rs.getString("email"));            data.setUsername(rs.getString("username"));            data.setNumber(rs.getString("number"));            data.setPassword(rs.getString("password"));            list.add(data);        }    } catch (Exception e) {        e.printStackTrace();    }    return list;}我先做了這個@Overridepublic ApiResponseDto getlogin(UserDto usersLogin) {  try{       Connection con = getConnection();       Statement st=con.createStatement();       ResultSet rs=st.executeQuery("select * from tableregister where username='"+usersLogin.getUsername()+"' and password='"+usersLogin.getPassword()+"'");       if (usersLogin.getUsername() != null && usersLogin.getPassword() != null) {           return new ApiResponseDto("Success", "Login: ");       } else {           ApiResponseDto apiResponseDto = new ApiResponseDto("Error", "Error Login");           apiResponseDto.setErrorCode(2);           return apiResponseDto;       }   } catch (Exception e) {       e.printStackTrace();       return new ApiResponseDto("Error", "Error: " + e.toString());   }}
查看完整描述

2 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

您分享的兩個片段都是錯誤的。

在第一個片段中,您執行了一個查詢(順便說一句,由于字符串連接,該查詢容易受到 SQL 注入的攻擊),但忽略其結果并僅檢查作為參數傳遞的對象

在第二個片段中,您再次執行查詢,但忽略結果,從數據庫中獲取所有用戶,并檢查他們是否存在。

相反,您需要根據傳遞的參數進行查詢,并檢查查詢是否返回了任何結果:

@Override

public ApiResponseDto getlogin(UserDto usersLogin) {

    try {

        // Assumption: The connection is pooled, and doesn't require closing.

        Connection con = getConnection();

        try (Preparestament ps = con.preparestament("select * from tableregister where username = ? and password = ?") {

            ps.setString(1, usersLogin.getUsername());

            ps.setString(2, userLoging.getPassword());

            try (ResultSet rs = ps.executeQuery()) {

                if (rs.next()) {

                   return new ApiResponseDto("Success", "Login Success");

                } else {

                    ApiResponseDto obj = new ApiResponseDto("Error", "Error Login");

                    obj.setErrorCode(1);

                    return obj;

                }

            }

        }

    } catch (Exception e) {

        e.printStackTrace(); // Or log the error somehow

        return new ApiResponseDto("Error", "Error: " + e.toString());

    }

}

PS:

請注意,在您的兩個代碼段中,您都沒有正確關閉 JDBC 對象,從而導致泄漏。這可以使用 try-with-resource 語法相對巧妙地完成。


查看完整回答
反對 回復 2022-11-30
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

您需要檢查結果集。您可以這樣做。


ResutSet rs = st.executeQuery....

if(rs.next()) {

  String username = r.getUserName();

  ...

}


查看完整回答
反對 回復 2022-11-30
  • 2 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號