* login process
* register page * post query * get query
This commit is contained in:
parent
a495798aee
commit
b78f31670f
@ -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,20 +1,10 @@
|
||||
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() {
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
89
wwwroot/css/register.css
Normal file
89
wwwroot/css/register.css
Normal file
@ -0,0 +1,89 @@
|
||||
/* Made with love by Mutiullah Samim*/
|
||||
|
||||
@import url('https://fonts.googleapis.com/css?family=Numans');
|
||||
|
||||
html,body{
|
||||
background-image: url('../rsc/login2.jpg');
|
||||
background-size: cover;
|
||||
background-repeat: no-repeat;
|
||||
height: 100%;
|
||||
font-family: 'Numans', sans-serif;
|
||||
}
|
||||
|
||||
.container{
|
||||
height: 100%;
|
||||
align-content: center;
|
||||
}
|
||||
|
||||
.card{
|
||||
height: 470px;
|
||||
margin-top: auto;
|
||||
margin-bottom: auto;
|
||||
width: 400px;
|
||||
background-color: rgba(0,0,0,0.5) !important;
|
||||
}
|
||||
|
||||
.social_icon span{
|
||||
font-size: 60px;
|
||||
margin-left: 10px;
|
||||
color: #FFC312;
|
||||
}
|
||||
|
||||
.social_icon span:hover{
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.card-header h3{
|
||||
color: white;
|
||||
}
|
||||
|
||||
.social_icon{
|
||||
position: absolute;
|
||||
right: 20px;
|
||||
top: -45px;
|
||||
}
|
||||
|
||||
.input-group-prepend span{
|
||||
width: 50px;
|
||||
background-color: #FFC312;
|
||||
color: black;
|
||||
border:0 !important;
|
||||
}
|
||||
|
||||
input:focus{
|
||||
outline: 0 0 0 0 !important;
|
||||
box-shadow: 0 0 0 0 !important;
|
||||
|
||||
}
|
||||
|
||||
.remember{
|
||||
color: white;
|
||||
}
|
||||
|
||||
.remember input
|
||||
{
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
margin-left: 15px;
|
||||
margin-right: 5px;
|
||||
}
|
||||
|
||||
.login_btn{
|
||||
color: black;
|
||||
background-color: #FFC312;
|
||||
width: 100px;
|
||||
}
|
||||
|
||||
.login_btn:hover{
|
||||
color: black;
|
||||
background-color: white;
|
||||
}
|
||||
|
||||
.links{
|
||||
color: white;
|
||||
}
|
||||
|
||||
.links a{
|
||||
margin-left: 4px;
|
||||
}
|
@ -57,7 +57,7 @@
|
||||
</div>
|
||||
<div class="card-footer">
|
||||
<div class="d-flex justify-content-center links">
|
||||
Don't have an account?<a href="#">Sign Up</a>
|
||||
Don't have an account?<a id="signupbtn" href="#">Sign Up</a>
|
||||
</div>
|
||||
<div class="d-flex justify-content-center">
|
||||
<a href="#">Forgot your password?</a>
|
||||
|
@ -5,7 +5,7 @@ $(document).ready(function() {
|
||||
var username = $("#userfield")[0].value;
|
||||
var password = $("#passfield")[0].value;
|
||||
|
||||
$.get('/senddata/loginget?username='+username+'&password='+password,function(data){
|
||||
$.post('/senddata/loginget','username='+username+'&password='+password,function(data){
|
||||
|
||||
console.log(data);
|
||||
if (data.accept == true) {
|
||||
@ -15,4 +15,13 @@ $(document).ready(function() {
|
||||
}
|
||||
},'json');
|
||||
});
|
||||
|
||||
$('#signupbtn').click(function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
$.post('/senddata/loginpost','username=luki&password=test',function(data){
|
||||
console.log(data);
|
||||
},'json');
|
||||
|
||||
});
|
||||
});
|
||||
|
3
wwwroot/js/register.js
Normal file
3
wwwroot/js/register.js
Normal file
@ -0,0 +1,3 @@
|
||||
$(document).ready(function() {
|
||||
|
||||
});
|
88
wwwroot/register.html
Normal file
88
wwwroot/register.html
Normal file
@ -0,0 +1,88 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Login Page</title>
|
||||
<!--Made with love by Mutiullah Samim -->
|
||||
|
||||
<!-- Latest compiled and minified CSS -->
|
||||
<link rel="stylesheet" href="lib/bootstrap.min.css">
|
||||
|
||||
<!-- jQuery library -->
|
||||
<script src="lib/jquery.min.js"></script>
|
||||
|
||||
<!-- Popper JS -->
|
||||
<script src="lib/popper.min.js"></script>
|
||||
|
||||
<!-- Latest compiled JavaScript -->
|
||||
<script src="lib/bootstrap.min.js"></script>
|
||||
|
||||
<!--Fontawesome CDN-->
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
|
||||
|
||||
<!--Custom styles-->
|
||||
<link rel="stylesheet" type="text/css" href="css/register.css">
|
||||
|
||||
<script type="text/javascript" src="js/register.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="d-flex justify-content-center h-100">
|
||||
<div class="card">
|
||||
<div class="card-header">
|
||||
<h3>Register</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form>
|
||||
<div class="input-group form-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fas fa-user"></i></span>
|
||||
</div>
|
||||
<input type="text" class="form-control" placeholder="username" id="usernamefield">
|
||||
</div>
|
||||
|
||||
<div class="input-group form-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fas fa-user"></i></span>
|
||||
</div>
|
||||
<input type="text" class="form-control" placeholder="first name" id="firstnamefield">
|
||||
</div>
|
||||
|
||||
<div class="input-group form-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fas fa-user"></i></span>
|
||||
</div>
|
||||
<input type="text" class="form-control" placeholder="last name" id="lastnamefield">
|
||||
</div>
|
||||
|
||||
<div class="input-group form-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fas fa-at"></i></span>
|
||||
</div>
|
||||
<input type="text" class="form-control" placeholder="E-Mail" id="emailfield">
|
||||
</div>
|
||||
|
||||
<div class="input-group form-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fas fa-key"></i></span>
|
||||
</div>
|
||||
<input type="password" class="form-control" placeholder="password" id="passfield">
|
||||
</div>
|
||||
|
||||
<div class="input-group form-group">
|
||||
<div class="input-group-prepend">
|
||||
<span class="input-group-text"><i class="fas fa-key"></i></span>
|
||||
</div>
|
||||
<input type="password" class="form-control" placeholder="Reply password" id="replpassfield">
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<input type="submit" value="Register" class="btn float-right login_btn" id="loginbtn">
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user