Java 客户端/服务器用户名/密码认证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31345462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Client/server username/password authentication
提问by javanewb
I am working on a Client/server authentication program, but I ran into a problem. The client makes the server connection fine, but once I type in my password and username it doesn't return whether it's a valid username/password. If the user logins in with right username/password server is supposed to return "Welcome, username" and if it's invalid it returns "failed to login". I have looked at the printwriter and bufferedreader docs to make sure that I am using the right methods to pass the text between server/client properly. I tried debugging by printing the username and password on both the server and client to make sure that they are both listening/writing, which they seem to be, because it does print out the proper username/password. Can someone give me some insight on where I am going wrong?
我正在开发客户端/服务器身份验证程序,但遇到了问题。客户端使服务器连接正常,但是一旦我输入密码和用户名,它就不会返回它是否是有效的用户名/密码。如果用户使用正确的用户名/密码登录服务器应该返回“欢迎,用户名”,如果无效则返回“登录失败”。我查看了 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();
}
}
}
采纳答案by Codebender
You also need to flush the printWriter of the server just like you are doing on the client side.
您还需要像在客户端一样刷新服务器的 printWriter。
At the end of your loginfo()
method,
在你的loginfo()
方法结束时,
if(username.equals(c.getUsername()) &&password.equals(c.getPassword())){
output.println("Welcome, " + username);
}else{
output.println("Login Failed");
}
output.flush();
output.close();
回答by losethegame
To authenticate against plaintext String within the code, use a conditional to check if the username and password are valid.
要针对代码中的纯文本字符串进行身份验证,请使用条件检查用户名和密码是否有效。
However, if you want to pretend we're in the real world, authenticate against LDAP or at least an encrypted properties file...
但是,如果您想假装我们在现实世界中,请针对 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();
}
}
Courtesy of oracle docs...
由 oracle 文档提供...