决策平台http认证服务器。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.4 KiB

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import helper.KeyReader;
import helper.RSAUtils;
import helper.StringUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
/**
* @author richie
* @version 10.0
* Created by richie on 2018-12-03
* http认证demo服务器
*/
public class Http {
public static void main(String... args) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0);
server.createContext("/demo", new AuthHandler());
server.start();
System.out.println("Server is started, please visit:http://localhost:8001/demo");
}
private static class AuthHandler implements HttpHandler {
@Override
public void handle(HttpExchange exchange) throws IOException {
exchange.sendResponseHeaders(200, 0);
URI uri = exchange.getRequestURI();
Map<String, String> parameters = parserQueryText(uri.getQuery());
String data = parameters.get("data");
// (必须)http认证的地方填的是公钥,则这里需要用私钥进行解密
String text = RSAUtils.decrypt(data, KeyReader.getPrivateKey());
System.out.println("data:" + text);
Map<String, String> map = parserText(text);
String responseText;
// username参数是从报表登录界面输入的地方获取的
String username = map.get("username");
// password参数是从报表登录界面输入的地方获取的
String password = map.get("password");
// uuid参数是报表发送http认证请求的时候生成的随机数
String uuid = map.get("uuid");
if (isValidUser(username, password)) {
// (必须)认证成功时返回的文本格式{"success":"true","uuid":"xxx-yyy-zzz-dddd"}
responseText = String.format("{\"success\":\"true\",\"uuid\":\"%s\"}", uuid);
} else {
responseText = "{\"success\":\"false\"}";
}
System.out.println("responseText:" + responseText);
// (必须)这里需要把返回值用私钥进行加密,在报表服务器中,会自动使用公钥进行解密
String returnValue = RSAUtils.encrypt(responseText, KeyReader.getPrivateKey());
OutputStream os = exchange.getResponseBody();
os.write(returnValue.getBytes());
os.close();
}
}
/**
* 判断username和password是否可以正确的登录
* @param username 用户名
* @param password 密码
* @return 如果能正确登录,则这里返回true表示,如果不能正确登录,则这里返回false表示
*/
private static boolean isValidUser(String username, String password) {
if (username == null || password == null) {
return false;
}
// 这里只是一个示例,当用户名和密码输入一样的时候,我们假设认证成功,允许登录
return username.equals(password);
}
private static Map<String, String> parserQueryText(String query) throws UnsupportedEncodingException {
Map<String, String> map = new HashMap<String, String>();
if (query == null) {
return map;
}
String[] pairs = query.split("&");
for (String pair : pairs) {
int idx = pair.indexOf("=");
map.put(URLDecoder.decode(pair.substring(0, idx), "UTF-8"), URLDecoder.decode(pair.substring(idx + 1), "UTF-8"));
}
return map;
}
private static Map<String, String> parserText(String text) {
Map<String, String> map = new HashMap<>();
if (StringUtils.isEmpty(text)) {
return map;
}
if (text.startsWith("{") && text.endsWith("}")) {
String[] arr = text.substring(1, text.length() - 1).split(",");
for (String child : arr) {
String[] pair = child.split(":");
String key = pair[0];
String value = pair[1];
map.put(key.substring(1, key.length() - 1), value.substring(1, value.length() - 1));
}
return map;
} else {
return map;
}
}
}