2015-07-10 132 views
2

我正在處理客戶端/服務器認證程序,但遇到了問題。客戶端使服務器連接正常,但是一旦我輸入密碼和用戶名,它就不會返回它是否是有效的用戶名/密碼。如果用戶用正確的用戶名/密碼服務器登錄應該返回「歡迎,用戶名」,如果它無效,則返回「登錄失敗」。我查看了printwriter和bufferedreader文檔,以確保我正確使用正確的方法在服務器/客戶端之間傳遞文本。我嘗試通過在服務器和客戶端上打印用戶名和密碼來進行調試,以確保它們都是監聽/寫入,這似乎是因爲它打印出正確的用戶名/密碼。有人能給我一些見解我的錯在哪裏嗎?客戶端/服務器用戶名/密碼認證

public class Connect { 
    private String USERNAME = "java"; 
    private String PASSWORD = "java"; 
    private int PORT = 9090; 
    private String HOSTNAME = "localhost"; 

    public String getUsername(){ 
     return this.USERNAME; 
    } 

    public String getPassword(){ 

     return this.PASSWORD; 
    } 

    public int getPort(){ 
     return this.PORT; 
    } 

    public String gethostName(){ 
     return this.HOSTNAME; 
    } 
} 


import java.io.*; 
import java.io.net.*; 
public class Client { 
    private final String FILENAME = null; 
    Connect c = new Connect(); 
    Socket socket; 
    BufferedReader read; 
    PrintWriter output; 

    public void startClient() throws UnknownHostException, IOException{ 
     //Create socket connection 
     socket = new Socket(c.gethostName(), c.getPort()); 

     //create printwriter for sending login to server 
     output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 

     //prompt for user name 
     String username = JOptionPane.showInputDialog(null, "Enter User Name:"); 

     //send user name to server 
     output.println(username); 

     //prompt for password 
     String password = JOptionPane.showInputDialog(null, "Enter Password"); 

     //send password to server 
     output.println(password); 
     output.flush(); 

     //create Buffered reader for reading response from server 
     read = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

     //read response from server 
     String response = read.readLine(); 
     System.out.println("This is the response: " + response); 

     //display response 
     JOptionPane.showMessageDialog(null, response); 
    } 

    public void fileInfo(){ 

    } 

    public static void main(String args[]){ 
     Client client = new Client(); 
     try { 
      client.startClient(); 
     } catch (UnknownHostException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 


import java.io.*; 
import java.io.net.*; 
public class Server { 
    private int currentTot; 
    ServerSocket serversocket; 
    Socket client; 
    int bytesRead; 
    Connect c = new Connect(); 
    BufferedReader input; 
    PrintWriter output; 

    public void start() throws IOException{ 
     System.out.println("Connection Starting on port:" + c.getPort()); 
     //make connection to client on port specified 
     serversocket = new ServerSocket(c.getPort()); 

     //accept connection from client 
     client = serversocket.accept(); 

     System.out.println("Waiting for connection from client"); 

     try { 
      logInfo(); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public void logInfo() throws Exception{ 
     //open buffered reader for reading data from client 
     input = new BufferedReader(new InputStreamReader(client.getInputStream())); 

     String username = input.readLine(); 
     System.out.println("SERVER SIDE" + username); 
     String password = input.readLine(); 
     System.out.println("SERVER SIDE" + password); 

     //open printwriter for writing data to client 
     output = new PrintWriter(new OutputStreamWriter(client.getOutputStream())); 

     if(username.equals(c.getUsername()) &&password.equals(c.getPassword())){ 
      output.println("Welcome, " + username); 
     }else{ 
      output.println("Login Failed"); 
     } 

    } 
    public static void main(String[] args){ 
     Server server = new Server(); 
     try { 
      server.start(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    }  
} 
+1

你能發佈服務器和客戶端代碼嗎?沒有看到它們是如何實施的,我們無法真正做到。 – DeadChex

+0

哦,我沒有看到滾動條!我的不好 – DeadChex

+0

服務器端在接收到用戶名和密碼時是否打印出來? – Jmrapp

回答

4

您還需要刷新服務器的PrintWriter就像你正在做在客戶端。

在你loginfo()方法的末尾,

if(username.equals(c.getUsername()) &&password.equals(c.getPassword())){ 
    output.println("Welcome, " + username); 
}else{ 
    output.println("Login Failed"); 
} 
output.flush(); 
output.close(); 
+0

好極了!謝謝你解決了這個問題。 – javanewb

0

在代碼中對明文字符串進行身份驗證,使用條件來檢查用戶名和密碼是有效的。

但是,如果你想假裝我們是在現實世界中,對LDAP或至少一個加密屬性文件驗證...

Hashtable ldap = new Hashtable(11); 
ldap.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
ldap.put(Context.PROVIDER_URL, "ldap://ldapserver:389/o=OU"); 
input = new BufferedReader(new InputStreamReader(client.getInputStream())); 
String username = input.readLine(); 
String password = input.readLine(); 
ldap.put(Context.SECURITY_AUTHENTICATION, "tls"); 
ldap.put(Context.SECURITY_PRINCIPAL, "cn=" + username + ", ou=OU1, o=OU2"); 
ldap.put(Context.SECURITY_CREDENTIALS, password); 

try { 
    DirContext context = new InitialDirContext(ldap); 
    context.addToEnvironment(Context.SECURITY_AUTHENTICATION, "none"); 
    context.close(); 
    } catch (Exception e) { 
     e.toString(); 
    } 
} 

禮貌的Oracle文檔的...

相關問題