-2

私はユーザーログインページに取り組んでいます。管理者が管理者セクションにアクセスできるようにしたいのですが、通常のユーザーはアクセスできないようにします。次のコードは機能しますが、管理者以外のユーザーがログインすると、間違ったパスワード エラーが発生し、正しいページに送信されます。否定的な答えと肯定的な答えを許可する場合、ネストされたものに何か問題があると思います。この件に関して何かお役に立てば幸いです。

        try {
        String sql="Select * from Users1";
        String host ="jdbc:derby://localhost:1527/Nash";
        String uName = "CON";
        String uPass = "smokes";
        Connection con = DriverManager.getConnection(host, uName, uPass);
        Statement stmt=con.createStatement();
        ResultSet rs = stmt.executeQuery(sql);
        String user= userName.getText();
        String pwd= password.getText();
        while(rs.next()) {
        String uname=rs.getString("User_name");
        String pass=rs.getString("Password");
        String admin1=rs.getString("admin");

if ((user.equals(uname)) && (pwd.equals(pass)))
    { 
    mainPanel.setVisible(true);
    blankContent.setVisible(true);
    adminButton.setEnabled(false);
    receiptContent.setVisible(false);
    memberContent.setVisible(false);
    securityPanel.setVisible(false);
    }

        if ((user.equals(uname)) && (pwd.equals(pass))&&(admin1.equals("y")))
        {
            mainPanel.setVisible(true);
            blankContent.setVisible(true);
            adminButton.setEnabled(true);
            receiptContent.setVisible(false);
            memberContent.setVisible(false);
            securityPanel.setVisible(false);
         }
else 
    {
            JOptionPane.showMessageDialog(null,  "User name and password do"
                    + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 

            }   
}   } catch (SQLException ex) {
        Logger.getLogger(program.class.getName()).log(Level.SEVERE, null, ex);
    }

EDIT 以下は編集されたコードリンクです ---> 新しい改訂されたコードリンク

4

3 に答える 3

3

else1 レベル外に移動する必要があります。コードの書式設定により、何が起こっているのかわかりにくくなっていますが、それ以外は管理ifステートメントにあります。このような:

if ((user.equals(uname)) && (pwd.equals(pass)))
{ 
    if (admin1.equals("y"))
    {
        // ... admin
    }
    else
    {   
        // ... regular user
    }
}
else 
{
    JOptionPane.showMessageDialog(null,  "User name and password do"
           + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 
}
于 2012-12-31T20:04:02.727 に答える
0

好きですか

if ((user.equals(uname)) && (pwd.equals(pass)))
    { 
    mainPanel.setVisible(true);
    blankContent.setVisible(true);
    receiptContent.setVisible(false);
    memberContent.setVisible(false);
    securityPanel.setVisible(false);
    adminButton.setEnabled(false);
          if(admin1.equals("y"))
                 adminButton.setEnabled(true);

    }


else 
    {
            JOptionPane.showMessageDialog(null,  "User name and password do"
                    + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 

            }  
于 2012-12-31T20:21:50.663 に答える
0

この問題の原因となる小さなグリッチが1つ欠けていることを除いて、すべて正しく実行しています(From your pastebin code in your comment)

ここでは、whileステートメントが大きな問題になります。データベース内のすべてのレコードをチェックし続けます。

break目的のユーザーを取得したら使用します。

boolean isPass = false;
while(rs.next()){
    // Your user authentication 
    if ((user.equals(uname)) && (pwd.equals(pass)))
    { 
        if (admin1.equals("y")) {
            // do your admin operation
            break;
        } else {   
            // do your non admin operation
            break;
        }
    } else {
        // if authentication fails 
        isPass = true
    }
}
// Outside of while block
if(isPass){
    // if authentication fails show error message
    JOptionPane.showMessageDialog(null,  "User name and password do"
           + " not match!","ALERT!", JOptionPane.ERROR_MESSAGE); 
}

breakこのコードスニペットは、breakを使用する方法を示すために示しました。そうでない場合は、これを作成する必要があることに注意してください。そうしないと、一部else part認証でfalseになったすべてのレコードに対してエラーメッセージが表示され続けます。if

別の方法からDB値を取得し、別の方法で認証を行うことをお勧めします。

于 2012-12-31T21:27:13.037 に答える