当前位置: 技术问答>java相关
大家来谈谈SSL吧!
来源: 互联网 发布时间:2015-04-16
本文导语: 都说,SSL能实现网络的安全通信。现在我想在服务器端,有安全性,保证非允许的客户端连接,看了一些文章,想用SSL,但还不知怎么实现? | 一个普通的webserver,只支持GET方法: code: import ja...
都说,SSL能实现网络的安全通信。现在我想在服务器端,有安全性,保证非允许的客户端连接,看了一些文章,想用SSL,但还不知怎么实现?
|
一个普通的webserver,只支持GET方法:
code:
code:
import java.net.*;
import java.io.*;
import java.util.*;
// Small, simple HTTP server
public class HTTPServer {
String NAME;
String VERSION;
int serverPort;
// No command line parameters are required
public static void main(String args[]){
HTTPServer server = new HTTPServer("HTTPServer", "1.0", 80);
server.run();
}
// Create an HTTPServer for a particular TCP port
public HTTPServer(String name, String version, int port) {
this.NAME = name;
this.VERSION = version;
this.serverPort = port;
}
// Display name and version number
public void displayVersionInfo(){
System.out.println(NAME+" version "+VERSION);
}
// Run until interrupted
public void run() {
displayVersionInfo();
try {
// Get a server socket
ServerSocket server = getServerSocket();
int localPort = server.getLocalPort();
// Let us know that you're listening
System.out.println(NAME+" is listening on port "+localPort+".");
do {
// Accept a connection
Socket client = server.accept();
// Handle the connection with a separate thread
(new HTTPServerThread(client)).start();
} while(true);
} catch(Exception ex) {
System.out.println("Unable to listen on "+serverPort+".");
ex.printStackTrace();
System.exit(1);
}
}
// Get a server socket on the hard-wired server port
ServerSocket getServerSocket() throws Exception {
return new ServerSocket(serverPort);
}
}
// Handle a single server connection
class HTTPServerThread extends Thread {
Socket client;
// Keep track of the client socket
public HTTPServerThread(Socket client) {
this.client = client;
}
// Thread entry point
public void run() {
try {
// Display info about the connection
describeConnection(client);
// Create a stream to send data to the client
BufferedOutputStream outStream = new
BufferedOutputStream(client.getOutputStream());
HTTPInputStream inStream = new
HTTPInputStream(client.getInputStream());
// Get the client's request
HTTPRequest request = inStream.getRequest();
// Display info about it
request.log();
// Sorry, we only handle gets
if(request.isGetRequest())
processGetRequest(request,outStream);
System.out.println("Request completed. Closing connection.");
}catch(IOException ex) {
System.out.println("IOException occurred when processing request.");
}
try {
client.close();
}catch(IOException ex) {
System.out.println("IOException occurred when closing socket.");
}
}
// Display info about the connection
void describeConnection(Socket client) {
String destName = client.getInetAddress().getHostName();
String destAddr = client.getInetAddress().getHostAddress();
int destPort = client.getPort();
System.out.println("Accepted connection to "+destName+" ("
+destAddr+")"+" on port "+destPort+".");
}
// Process an HTTP GET
void processGetRequest(HTTPRequest request,BufferedOutputStream
outStream)
throws IOException {
/* If you want to use this in a secure environment then you should
place some
restrictions on the requested file name */
String fileName = request.getFileName();
File file = new File(fileName);
// Give them the requested file
if(file.exists()) sendFile(outStream,file);
else System.out.println("File "+file.getCanonicalPath()+" does not
exist.");
}
// A simple HTTP 1.0 response
void sendFile(BufferedOutputStream out,File file) {
try {
DataInputStream in = new DataInputStream(new FileInputStream(file));
int len = (int) file.length();
byte buffer[] = new byte[len];
in.readFully(buffer);
in.close();
out.write("HTTP/1.0 200 OKrn".getBytes());
out.write(("Content-Length: " + buffer.length + "rn").getBytes());
out.write("Content-Type: text/htmlrnrn".getBytes());
out.write(buffer);
out.flush();
out.close();
System.out.println("File sent: "+file.getCanonicalPath());
System.out.println("Number of bytes: "+len);
}catch(Exception ex){
try {
out.write(("HTTP/1.0 400 " + "No can do" + "rn").getBytes());
out.write("Content-Type: text/htmlrnrn".getBytes());
}catch(IOException ioe) {
}
System.out.println("Error retrieving "+file);
}
}
}
// Convenience class for reading client requests
class HTTPInputStream extends FilterInputStream {
public HTTPInputStream(InputStream in) {
super(in);
}
// Get a line
public String readLine() throws IOException {
StringBuffer result=new StringBuffer();
boolean finished = false;
boolean cr = false;
do {
int ch = -1;
ch = read();
if(ch==-1) return result.toString();
result.append((char) ch);
if(cr && ch==10){
result.setLength(result.length()-2);
return result.toString();
}
if(ch==13) cr = true;
else cr=false;
} while (!finished);
return result.toString();
}
// Get the whole request
public HTTPRequest getRequest() throws IOException {
HTTPRequest request = new HTTPRequest();
String line;
do {
line = readLine();
if(line.length()>0) request.addLine(line);
else break;
}while(true);
return request;
}
}
// Used to process GET requests
class HTTPRequest {
Vector lines = new Vector();
public HTTPRequest() {
}
public void addLine(String line) {
lines.addElement(line);
}
// Is this a GET or isn't it?
boolean isGetRequest() {
if(lines.size() > 0) {
String firstLine = (String) lines.elementAt(0);
if(firstLine.length() > 0)
if(firstLine.substring(0,3).equalsIgnoreCase("GET"))
return true;
}
return false;
}
// What do they want to get?
String getFileName() {
if(lines.size()>0) {
String firstLine = (String) lines.elementAt(0);
String fileName = firstLine.substring(firstLine.indexOf(" ")+1);
int n = fileName.indexOf(" ");
if(n!=-1) fileName = fileName.substring(0,n);
try {
if(fileName.charAt(0) == '/') fileName = fileName.substring(1);
} catch(StringIndexOutOfBoundsException ex) {}
if(fileName.equals("")) fileName = "index.htm";
if(fileName.charAt(fileName.length()-1)=='/')
fileName+="index.htm";
return fileName;
}else return "";
}
// Display some info so we know what's going on
void log() {
System.out.println("Received the following request:");
for(int i=0;i<lines.size();++i)
System.out.println((String) lines.elementAt(i));
}
}
|
不需要实现,只需要配置一下服务器,具体的可参见Tomcat SSL Howto。
|
如果是tomcat的话,你可以到Apache的网站看看。很简单的
如果用JDK1.4就很简单的,删掉server.xml里面的一段注释就可以了
如果用JDK1.4就很简单的,删掉server.xml里面的一段注释就可以了
|
我决的他和你的程序无关,关键是服务器的配置和安装证书,证书如果要verisign的正式的肯定要话银子,可以自己颁发或着下个测试用的,就是不被信任。个个服务器的ssl培植不一样,你必须参考服务器的手册说明,比如apache可以下在ssl_mol和ssl_apache,是免费的,也有负费的,这些东西都有相关的安装配置说明,主要是 你的服务器种类。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。