* login process
* register page * post query * get query
This commit is contained in:
@ -7,27 +7,33 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class jdcb {
|
||||
public void executeQuery(String sql) {
|
||||
String username;
|
||||
String password;
|
||||
String dbName;
|
||||
|
||||
public jdcb(String username, String password, String dbName) {
|
||||
this.username = username;
|
||||
this.password=password;
|
||||
this.dbName = dbName;
|
||||
}
|
||||
|
||||
public ResultSet executeQuery(String sql) {
|
||||
Database db = new MySQLConnector(
|
||||
"users",
|
||||
"kOpaIJUjkgb9ur6S",
|
||||
"127.0.0.1",
|
||||
username,
|
||||
password,
|
||||
"192.168.65.15",
|
||||
3306,
|
||||
"wasteinformation");
|
||||
dbName);
|
||||
|
||||
Connection c = db.getConnection();
|
||||
try {
|
||||
PreparedStatement stmt =
|
||||
c.prepareStatement(sql);
|
||||
|
||||
ResultSet r = stmt.executeQuery();
|
||||
|
||||
DefaultTableModel model = Database.logToTable(r);
|
||||
|
||||
// this.resultTable.setModel(model);
|
||||
|
||||
return stmt.executeQuery();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,8 @@
|
||||
import db.jdcb;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class main {
|
||||
public static void main(String[] args) {
|
||||
/*
|
||||
@ -7,12 +10,14 @@ public class main {
|
||||
D.getdata();
|
||||
D.printList();
|
||||
*/
|
||||
|
||||
Thread mythread = new Thread(() -> new website.Webserver().startserver());
|
||||
mythread.start();
|
||||
|
||||
|
||||
System.out.println("thread started");
|
||||
|
||||
//new jdcb().executeQuery("select * from user");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
49
src/website/GetRequest.java
Normal file
49
src/website/GetRequest.java
Normal file
@ -0,0 +1,49 @@
|
||||
package website;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class GetRequest implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange httpExchange) throws IOException {
|
||||
if (httpExchange.getRequestMethod().equals("GET")) {
|
||||
String query = httpExchange.getRequestURI().getQuery();
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
|
||||
String[] res = query.split("&");
|
||||
for (String str : res) {
|
||||
String[] values = str.split("=");
|
||||
params.put(values[0], values[1]);
|
||||
|
||||
}
|
||||
|
||||
String response = myrequest(params);
|
||||
|
||||
|
||||
Headers h = httpExchange.getResponseHeaders();
|
||||
h.set("Content-Type", "application/json");
|
||||
httpExchange.sendResponseHeaders(200, 0);
|
||||
|
||||
OutputStream os = httpExchange.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param params received get params from website
|
||||
* @return json reply to website
|
||||
*/
|
||||
public abstract String myrequest(HashMap<String, String> params);
|
||||
|
||||
}
|
19
src/website/HttpTools.java
Normal file
19
src/website/HttpTools.java
Normal file
@ -0,0 +1,19 @@
|
||||
package website;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class HttpTools {
|
||||
public static String StringToMD5(String value) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] messageDigest = md.digest(value.getBytes());
|
||||
BigInteger no = new BigInteger(1, messageDigest);
|
||||
return no.toString(16);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
41
src/website/LoginRequest.java
Normal file
41
src/website/LoginRequest.java
Normal file
@ -0,0 +1,41 @@
|
||||
package website;
|
||||
|
||||
import db.jdcb;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class LoginRequest extends PostRequest {
|
||||
@Override
|
||||
public String request(HashMap<String, String> params) {
|
||||
|
||||
String password = params.get("password");
|
||||
String username = params.get("username");
|
||||
|
||||
ResultSet s = new jdcb("users", "kOpaIJUjkgb9ur6S", "wasteinformation").executeQuery("select * from user where username ='" + username + "'");
|
||||
|
||||
String response = "{\"accept\": false}";
|
||||
try {
|
||||
s.last();
|
||||
if (s.getRow() == 1) {
|
||||
//success
|
||||
if (HttpTools.StringToMD5(password).equals(s.getString("password"))) {
|
||||
System.out.println("login success");
|
||||
response = "{\"accept\": true}";
|
||||
} else {
|
||||
System.out.println("wrong password");
|
||||
}
|
||||
} else if (s.getRow() == 0) {
|
||||
//user not found
|
||||
System.out.println("user not found");
|
||||
} else {
|
||||
//internal error two users with same name...?
|
||||
}
|
||||
System.out.println("rowcount: " + s.getRow());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
57
src/website/MainPage.java
Normal file
57
src/website/MainPage.java
Normal file
@ -0,0 +1,57 @@
|
||||
package website;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URI;
|
||||
|
||||
public class MainPage implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange t) throws IOException {
|
||||
String root = "./wwwroot";
|
||||
URI uri = t.getRequestURI();
|
||||
String path;
|
||||
|
||||
if (uri.getPath().equals("/")) {
|
||||
path = "/index.html";
|
||||
} else {
|
||||
path = uri.getPath();
|
||||
}
|
||||
System.out.println("looking for: " + root + path);
|
||||
|
||||
File file = new File(root + path).getCanonicalFile();
|
||||
|
||||
if (!file.isFile()) {
|
||||
// Object does not exist or is not a file: reject with 404 error.
|
||||
String response = "404 (Not Found)\n";
|
||||
t.sendResponseHeaders(404, response.length());
|
||||
OutputStream os = t.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
} else {
|
||||
// Object exists and is a file: accept with response code 200.
|
||||
String mime = "text/html";
|
||||
if (path.substring(path.length() - 3).equals(".js")) mime = "application/javascript";
|
||||
if (path.substring(path.length() - 3).equals("css")) mime = "text/css";
|
||||
|
||||
Headers h = t.getResponseHeaders();
|
||||
h.set("Content-Type", mime);
|
||||
t.sendResponseHeaders(200, 0);
|
||||
|
||||
OutputStream os = t.getResponseBody();
|
||||
FileInputStream fs = new FileInputStream(file);
|
||||
final byte[] buffer = new byte[0x10000];
|
||||
int count;
|
||||
while ((count = fs.read(buffer)) >= 0) {
|
||||
os.write(buffer, 0, count);
|
||||
}
|
||||
fs.close();
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
}
|
48
src/website/PostRequest.java
Normal file
48
src/website/PostRequest.java
Normal file
@ -0,0 +1,48 @@
|
||||
package website;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.HashMap;
|
||||
|
||||
public abstract class PostRequest implements HttpHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange httpExchange) throws IOException {
|
||||
if (httpExchange.getRequestMethod().equals("POST")) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
InputStream ios = httpExchange.getRequestBody();
|
||||
int i;
|
||||
while ((i = ios.read()) != -1) {
|
||||
sb.append((char) i);
|
||||
}
|
||||
String query = sb.toString();
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
|
||||
String[] res = query.split("&");
|
||||
for (String str : res) {
|
||||
String[] values = str.split("=");
|
||||
params.put(values[0], values[1]);
|
||||
}
|
||||
|
||||
String response = request(params);
|
||||
|
||||
|
||||
Headers h = httpExchange.getResponseHeaders();
|
||||
h.set("Content-Type", "application/json");
|
||||
httpExchange.sendResponseHeaders(200, 0);
|
||||
|
||||
OutputStream os = httpExchange.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract String request(HashMap<String, String> params);
|
||||
|
||||
}
|
@ -1,23 +1,13 @@
|
||||
package website;
|
||||
|
||||
import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.math.BigInteger;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Webserver{
|
||||
public void startserver(){
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class Webserver {
|
||||
public void startserver() {
|
||||
System.out.println("starting server");
|
||||
HttpServer server = null;
|
||||
try {
|
||||
@ -27,100 +17,11 @@ public class Webserver{
|
||||
}
|
||||
|
||||
server.createContext("/", new MainPage());
|
||||
|
||||
server.createContext("/senddata/loginget", httpExchange -> {
|
||||
if (httpExchange.getRequestMethod().equals("GET")){
|
||||
String query = httpExchange.getRequestURI().getQuery();
|
||||
System.out.println(query);
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
|
||||
String[] res = query.split("&");
|
||||
for (String str : res){
|
||||
String[] values = str.split("=");
|
||||
params.put(values[0],values[1]);
|
||||
|
||||
}
|
||||
String password = params.get("password");
|
||||
String username = params.get("username");
|
||||
|
||||
System.out.println(StringToMD5(password));
|
||||
//TODO check if user exists in database
|
||||
|
||||
|
||||
//send response
|
||||
String response = "{\"accept\": true}";
|
||||
|
||||
Headers h = httpExchange.getResponseHeaders();
|
||||
h.set("Content-Type", "application/json");
|
||||
httpExchange.sendResponseHeaders(200, 0);
|
||||
|
||||
OutputStream os = httpExchange.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
}
|
||||
});
|
||||
server.createContext("/senddata/loginget", new LoginRequest());
|
||||
|
||||
server.setExecutor(null); // creates a default executor
|
||||
server.start();
|
||||
}
|
||||
|
||||
public String StringToMD5(String value){
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
byte[] messageDigest = md.digest(value.getBytes());
|
||||
BigInteger no = new BigInteger(1, messageDigest);
|
||||
return no.toString(16);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static class MainPage implements HttpHandler {
|
||||
@Override
|
||||
public void handle(HttpExchange t) throws IOException {
|
||||
System.out.println("a new request...");
|
||||
String root = "./wwwroot";
|
||||
URI uri = t.getRequestURI();
|
||||
String path;
|
||||
|
||||
if (uri.getPath().equals("/")){
|
||||
path = "/index.html";
|
||||
}else{
|
||||
path = uri.getPath();
|
||||
}
|
||||
System.out.println("looking for: "+ root + path);
|
||||
|
||||
File file = new File(root + path).getCanonicalFile();
|
||||
|
||||
if (!file.isFile()) {
|
||||
// Object does not exist or is not a file: reject with 404 error.
|
||||
String response = "404 (Not Found)\n";
|
||||
t.sendResponseHeaders(404, response.length());
|
||||
OutputStream os = t.getResponseBody();
|
||||
os.write(response.getBytes());
|
||||
os.close();
|
||||
} else {
|
||||
// Object exists and is a file: accept with response code 200.
|
||||
String mime = "text/html";
|
||||
if(path.substring(path.length()-3).equals(".js")) mime = "application/javascript";
|
||||
if(path.substring(path.length()-3).equals("css")) mime = "text/css";
|
||||
|
||||
Headers h = t.getResponseHeaders();
|
||||
h.set("Content-Type", mime);
|
||||
t.sendResponseHeaders(200, 0);
|
||||
|
||||
OutputStream os = t.getResponseBody();
|
||||
FileInputStream fs = new FileInputStream(file);
|
||||
final byte[] buffer = new byte[0x10000];
|
||||
int count = 0;
|
||||
while ((count = fs.read(buffer)) >= 0) {
|
||||
os.write(buffer,0,count);
|
||||
}
|
||||
fs.close();
|
||||
os.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user