优化代码

This commit is contained in:
jiangzeyin 2018-09-28 20:11:31 +08:00
parent 403bc5653d
commit 373305d82a
13 changed files with 216 additions and 150 deletions

View File

@ -15,7 +15,6 @@
<java.version>1.8</java.version>
<common-boot.version>1.2.5</common-boot.version>
</properties>
<dependencies>
<dependency>

View File

@ -0,0 +1,19 @@
package cn.jiangzeyin.controller;
import cn.jiangzeyin.common.interceptor.LoginInterceptor;
import cn.jiangzeyin.controller.base.AbstractBaseControl;
/**
* @author jiangzeyin
* @date 2018/9/28
*/
public abstract class BaseController extends AbstractBaseControl {
protected String userName;
protected String userPwd;
@Override
public void resetInfo() {
userName = getSessionAttribute(LoginInterceptor.SESSION_NAME);
userPwd = getSessionAttribute(LoginInterceptor.SESSION_PWD);
}
}

View File

@ -1,31 +1,20 @@
package cn.jiangzeyin.controller;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.common.interceptor.LoginInterceptor;
import cn.jiangzeyin.controller.base.AbstractBaseControl;
import cn.jiangzeyin.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
@Controller
@RequestMapping(value = "/")
public class IndexControl extends AbstractBaseControl {
@Resource
private UserService userService;
@RequestMapping(value = "error", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
public String error() {
return "error";
}
/**
* 加载首页
*
@ -51,33 +40,4 @@ public class IndexControl extends AbstractBaseControl {
getSession().invalidate();
return "login";
}
/**
* 修改密码
*
* @param oldPwd 旧密码
* @param newPwd 新密码
* @return
*/
@RequestMapping(value = "updatePwd")
@ResponseBody
public String updatePwd(String oldPwd, String newPwd) {
try {
String result = userService.updatePwd(getSession().getAttribute(LoginInterceptor.SESSION_NAME).toString(), oldPwd, newPwd);
// 用户不存在
if ("notexist".equals(result)) {
return JsonMessage.getString(500, "用户不存在!");
}
// 旧密码不正确
if ("olderror".equals(result)) {
return JsonMessage.getString(500, "旧密码不正确!");
}
// 如果修改成功则销毁会话
getSession().invalidate();
return JsonMessage.getString(200, "修改密码成功!");
} catch (Exception e) {
DefaultSystemLog.ERROR().error(e.getMessage(), e);
return JsonMessage.getString(500, e.getMessage());
}
}
}

View File

@ -37,8 +37,8 @@ public class LoginControl extends AbstractBaseControl {
boolean flag = userService.login(userName, userPwd);
if (flag) {
stringBuffer.append(",结果:").append("OK");
getSession().setAttribute(LoginInterceptor.SESSION_NAME, userName);
getSession().setAttribute(LoginInterceptor.SESSION_PWD, userPwd);
setSessionAttribute(LoginInterceptor.SESSION_NAME, userName);
setSessionAttribute(LoginInterceptor.SESSION_PWD, userPwd);
return JsonMessage.getString(200, "登录成功");
} else {
stringBuffer.append(",结果:").append("faild");

View File

@ -0,0 +1,44 @@
package cn.jiangzeyin.controller.manage;
import cn.hutool.crypto.SecureUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.controller.BaseController;
import cn.jiangzeyin.model.ProjectInfoModel;
import cn.jiangzeyin.service.manage.ManageService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.annotation.Resource;
import java.io.IOException;
/**
* Created by jiangzeyin on 2018/9/28.
*/
@Controller
@RequestMapping(value = "/manage/")
public class ConsoleController extends BaseController {
@Resource
private ManageService manageService;
/**
* 管理项目
*
* @return page
*/
@RequestMapping(value = "console", method = RequestMethod.GET)
public String console(String id) {
ProjectInfoModel pim = null;
try {
pim = manageService.getProjectInfo(id);
} catch (IOException e) {
DefaultSystemLog.LOG().error(e.getMessage(), e);
}
setAttribute("projectInfo", JSONObject.toJSONString(pim));
String md5 = SecureUtil.md5(String.format("%s:%s", userName, userPwd));
setAttribute("userInfo", md5);
return "manage/console";
}
}

View File

@ -39,26 +39,6 @@ public class ManageControl extends AbstractBaseControl {
return "manage/projectInfo";
}
/**
* 管理项目
*
* @return
*/
@RequestMapping(value = "console")
public String console(String id) {
ProjectInfoModel pim = null;
try {
pim = manageService.getProjectInfo(id);
} catch (IOException e) {
e.printStackTrace();
DefaultSystemLog.LOG().error(e.getMessage(), e);
}
setAttribute("projectInfo", JSONObject.toJSONString(pim));
setAttribute("userInfo", SecureUtil.md5(String.format("%s:%s", getSession().getAttribute(LoginInterceptor.SESSION_NAME), getSession().getAttribute(LoginInterceptor.SESSION_PWD))));
return "manage/console";
}
/**
* 查询所有项目
*

View File

@ -0,0 +1,51 @@
package cn.jiangzeyin.controller.user;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.controller.BaseController;
import cn.jiangzeyin.service.UserService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
/**
* @author jiangzeyin
* @date 2018/9/28
*/
@RestController
@RequestMapping(value = "/")
public class UpdatePwdController extends BaseController {
@Resource
private UserService userService;
/**
* 修改密码
*
* @param oldPwd 旧密码
* @param newPwd 新密码
* @return json
*/
@RequestMapping(value = "updatePwd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String updatePwd(String oldPwd, String newPwd) {
try {
String result = userService.updatePwd(userName, oldPwd, newPwd);
// 用户不存在
if ("notexist".equals(result)) {
return JsonMessage.getString(500, "用户不存在!");
}
// 旧密码不正确
if ("olderror".equals(result)) {
return JsonMessage.getString(500, "旧密码不正确!");
}
// 如果修改成功则销毁会话
getSession().invalidate();
return JsonMessage.getString(200, "修改密码成功!");
} catch (Exception e) {
DefaultSystemLog.ERROR().error(e.getMessage(), e);
return JsonMessage.getString(500, e.getMessage());
}
}
}

View File

@ -1,9 +1,15 @@
package cn.jiangzeyin.service;
import cn.hutool.crypto.SecureUtil;
import cn.jiangzeyin.util.JsonUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* @author Administrator
*/
@Service
public class UserService extends BaseService {
@ -24,6 +30,25 @@ public class UserService extends BaseService {
return pwd.equals(userInfo.getString("password"));
}
public boolean checkUser(String userMd5) throws IOException {
JSONObject jsonData = getJsonObject(FILENAME);
if (jsonData == null) {
return false;
}
for (String strKey : jsonData.keySet()) {
JSONObject jsonUser = jsonData.getJSONObject(strKey);
String id = jsonUser.getString("id");
String pwd = jsonUser.getString("password");
String strUsermd5 = SecureUtil.md5(String.format("%s:%s", id, pwd));
if (strUsermd5.equals(userMd5)) {
return true;
}
}
return false;
}
/**
* 修改密码
*

View File

@ -3,14 +3,12 @@ package cn.jiangzeyin.socket;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.jiangzeyin.common.DefaultSystemLog;
import cn.jiangzeyin.common.JsonMessage;
import cn.jiangzeyin.common.spring.SpringUtil;
import cn.jiangzeyin.model.ProjectInfoModel;
import cn.jiangzeyin.pool.ThreadPoolService;
import cn.jiangzeyin.service.BaseService;
import cn.jiangzeyin.service.UserService;
import cn.jiangzeyin.service.manage.CommandService;
import cn.jiangzeyin.service.manage.ManageService;
import com.alibaba.fastjson.JSONObject;
@ -21,19 +19,22 @@ import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;
import java.util.concurrent.ExecutorService;
/**
* socket
* Created by jiangzeyin on 2017/9/8.
*
* @author jiangzeyin
* @date 2017/9/8
*/
@ServerEndpoint(value = "/console/{userInfo}")
@Component
public class LogWebSocketHandle implements TailLogThread.Evn {
private static ExecutorService EXECUTOR_SERVICE = null;
private static final String RUNING_TAG = "running";
private static final String STOP_TAG = "stopped";
private static ExecutorService EXECUTOR_SERVICE = null;
private Process process;
private InputStream inputStream;
private TailLogThread thread;
@ -48,26 +49,8 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
}
// 通过用户名和密码的Md5值判断是否是登录的
try {
boolean flag = false;
if (!StrUtil.isEmpty(userInfo)) {
BaseService service = new BaseService();
JSONObject obj = service.getJsonObject("user.json");
Set<String> set_key = obj.keySet();
for (String str_key : set_key) {
JSONObject json_user = obj.getJSONObject(str_key);
String str_userMd5 = SecureUtil.md5(String.format("%s:%s", json_user.getString("id"), json_user.getString("password")));
if (str_userMd5.equals(userInfo)) {
flag = true;
break;
}
}
} else {
sendMsg(session, JsonMessage.getString(500, "用户名或密码错误!"));
session.close();
}
if (!flag) {
UserService userService = SpringUtil.getBean(UserService.class);
if (!userService.checkUser(userInfo)) {
sendMsg(session, JsonMessage.getString(500, "用户名或密码错误!"));
session.close();
}
@ -86,14 +69,10 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
@OnMessage
public void onMessage(String message, Session session) {
DefaultSystemLog.LOG().info("客户端消息:" + message);
JSONObject json = JSONObject.parseObject(message);
JSONObject projectInfo = json.getJSONObject("projectInfo");
String str_result;
String id = projectInfo.getString("id");
ManageService manageService = SpringUtil.getBean(ManageService.class);
ProjectInfoModel projectInfoModel = null;
try {
@ -107,50 +86,63 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
}
String op = json.getString("op");
JSONObject resultData = null;
String strResult;
// 执行相应命令
switch (op) {
case "start":
// 启动项目
str_result = execCommand(session, "start", projectInfoModel);
if (str_result.contains("running")) {
strResult = execCommand(session, "start", projectInfoModel);
if (strResult.contains(RUNING_TAG)) {
resultData = JsonMessage.toJson(200, "启动成功", json);
} else {
resultData = JsonMessage.toJson(400, str_result, json);
resultData = JsonMessage.toJson(400, strResult, json);
}
break;
case "restart":
// 重启项目
str_result = execCommand(session, "restart", projectInfoModel);
if (str_result.contains("running")) {
strResult = execCommand(session, "restart", projectInfoModel);
if (strResult.contains(RUNING_TAG)) {
resultData = JsonMessage.toJson(200, "重启成功", json);
} else {
resultData = JsonMessage.toJson(400, str_result, json);
resultData = JsonMessage.toJson(400, strResult, json);
}
break;
case "stop":
// 停止项目
str_result = execCommand(session, "stop", projectInfoModel);
if (str_result.contains("stopped")) {
strResult = execCommand(session, "stop", projectInfoModel);
if (strResult.contains(STOP_TAG)) {
resultData = JsonMessage.toJson(200, "已停止", json);
if (thread != null) {
thread.stop();
}
} else {
resultData = JsonMessage.toJson(500, str_result, json);
resultData = JsonMessage.toJson(500, strResult, json);
}
break;
case "status":
// 获取项目状态
str_result = execCommand(session, "status", projectInfoModel);
json.put("result", str_result);
if (str_result.contains("running")) {
strResult = execCommand(session, "status", projectInfoModel);
json.put("result", strResult);
if (strResult.contains(RUNING_TAG)) {
resultData = JsonMessage.toJson(200, "运行中", json);
} else {
resultData = JsonMessage.toJson(404, "未运行", json);
}
break;
case "showlog":
showLog(session, projectInfoModel);
break;
default:
resultData = JsonMessage.toJson(404, "不支持的方式:" + op);
break;
}
if (resultData != null) {
resultData.put("op", op);
sendMsg(session, resultData.toString());
}
}
private void showLog(Session session, ProjectInfoModel projectInfoModel) {
// 进入管理页面后需要实时加载日志
String log = projectInfoModel.getLog();
try {
@ -172,16 +164,9 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
} catch (IOException e) {
DefaultSystemLog.ERROR().error("打开日志异常", e);
}
break;
default:
break;
}
if (resultData != null) {
resultData.put("op", op);
sendMsg(session, resultData.toString());
}
}
/**
* 执行shell命令
*
@ -252,6 +237,9 @@ public class LogWebSocketHandle implements TailLogThread.Evn {
* @param msg 消息
*/
private synchronized void sendMsg(Session session, String msg) {
if (session == null) {
return;
}
try {
DefaultSystemLog.LOG().info(msg);
session.getBasicRemote().sendText(msg);

View File

@ -9,7 +9,9 @@ import java.io.InputStreamReader;
/**
* 线程处理
* Created by jiangzeyin on 2017/9/8.
*
* @author jiangzeyin
* @date 2017/9/8
*/
public class TailLogThread implements Runnable {
@ -28,10 +30,6 @@ public class TailLogThread implements Runnable {
run = false;
}
public boolean isRun() {
return run;
}
@Override
public void run() {
String line;
@ -63,6 +61,9 @@ public class TailLogThread implements Runnable {
}
public interface Evn {
/**
* 尝试次数过多
*/
void onError();
}
}

View File

@ -4,6 +4,9 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @author jiangzeyin
*/
@Configuration
public class WebSocketConfig {
@Bean

View File

@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2017/5/11.
* Created by jiangzeyin on 2017/5/11.
*/
@Aspect
@Component
@ -25,7 +25,7 @@ public class WebAopLog {
}
@Before("webLog()")
public void doBefore(JoinPoint joinPoint) throws Throwable {
public void doBefore(JoinPoint joinPoint) {
// 接收到请求记录请求内容
IS_LOG.set(true);
Signature signature = joinPoint.getSignature();
@ -42,13 +42,13 @@ public class WebAopLog {
}
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求返回内容
Boolean isLog_ = IS_LOG.get();
if (isLog_ != null && !isLog_) {
public void doAfterReturning(Object ret) {
if (ret == null) {
return;
}
if (ret == null) {
// 处理完请求返回内容
Boolean isLog = IS_LOG.get();
if (isLog != null && !isLog) {
return;
}
DefaultSystemLog.LOG().info(" :" + ret.toString());

View File

@ -23,15 +23,11 @@ public class JsonUtil {
if (null == obj) {
flag = true;
} else if (obj instanceof JSONObject) {
JSONObject jsonobj = (JSONObject) obj;
if (0 == jsonobj.keySet().size()) {
flag = true;
}
JSONObject jsonObject = (JSONObject) obj;
flag = jsonObject.isEmpty();
} else if (obj instanceof JSONArray) {
JSONArray jsonarr = (JSONArray) obj;
if (0 == jsonarr.size()) {
flag = true;
}
JSONArray jsonArray = (JSONArray) obj;
flag = jsonArray.isEmpty();
}
return flag;
}