diff --git a/README.md b/README.md index fadeb11d..f97dacc3 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,3 @@ git.osc地址:http://git.oschina.net/xuxueli0323/xxl-job # 其他说明 清楚僵尸任务:qrtz_cron_triggers、qrtz_triggers、qrtz_job_details顺序删除 V1.2新增任务日志,记得执行“tables_xxl_log.sql”生成表结构 - -# 规划中 - 1、登陆系统 - 2、内嵌数据库 》 内嵌服务器 - \ No newline at end of file diff --git a/doc/db/tables_xxl_log.sql b/doc/db/tables_xxl_log.sql index a0454b5b..703e2d0e 100644 --- a/doc/db/tables_xxl_log.sql +++ b/doc/db/tables_xxl_log.sql @@ -2,15 +2,6 @@ # DROP TABLE IF EXISTS XXL_JOB_QRTZ_PAUSED_TRIGGER_GRPS; # DROP TABLE IF EXISTS XXL_JOB_QRTZ_SCHEDULER_STATE; # DROP TABLE IF EXISTS XXL_JOB_QRTZ_LOCKS; -# DROP TABLE IF EXISTS XXL_JOB_QRTZ_SIMPLE_TRIGGERS; -# DROP TABLE IF EXISTS XXL_JOB_QRTZ_SIMPROP_TRIGGERS; -# DROP TABLE IF EXISTS XXL_JOB_QRTZ_CRON_TRIGGERS; -# DROP TABLE IF EXISTS XXL_JOB_QRTZ_BLOB_TRIGGERS; -# DROP TABLE IF EXISTS XXL_JOB_QRTZ_TRIGGERS; -# DROP TABLE IF EXISTS XXL_JOB_QRTZ_JOB_DETAILS; -# DROP TABLE IF EXISTS XXL_JOB_QRTZ_CALENDARS; -# DROP TABLE IF EXISTS `xxl_job_qrtz_trigger_info`; -# DROP TABLE IF EXISTS `xxl_job_qrtz_trigger_log`; CREATE TABLE XXL_JOB_QRTZ_JOB_DETAILS ( @@ -156,8 +147,6 @@ CREATE TABLE XXL_JOB_QRTZ_LOCKS PRIMARY KEY (SCHED_NAME,LOCK_NAME) ); - -DROP TABLE IF EXISTS `xxl_job_qrtz_trigger_info`; CREATE TABLE `xxl_job_qrtz_trigger_info` ( `id` int(11) NOT NULL AUTO_INCREMENT, `job_group` varchar(255) NOT NULL COMMENT '任务组', @@ -174,7 +163,6 @@ CREATE TABLE `xxl_job_qrtz_trigger_info` ( PRIMARY KEY (`id`) ); -DROP TABLE IF EXISTS `xxl_job_qrtz_trigger_log`; CREATE TABLE `xxl_job_qrtz_trigger_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `job_group` varchar(255) NOT NULL COMMENT '任务组', diff --git a/xxl-job-admin/src/main/java/com/xxl/job/controller/IndexController.java b/xxl-job-admin/src/main/java/com/xxl/job/controller/IndexController.java index d73c4ca4..a53e0466 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/controller/IndexController.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/controller/IndexController.java @@ -1,8 +1,18 @@ package com.xxl.job.controller; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.xxl.job.controller.annotation.PermessionLimit; +import com.xxl.job.controller.interceptor.PermissionInterceptor; +import com.xxl.job.core.model.ReturnT; /** * index controller @@ -12,8 +22,46 @@ import org.springframework.web.bind.annotation.RequestMapping; public class IndexController { @RequestMapping("/") - public String index(Model model) { - return "redirect:jobinfo"; + @PermessionLimit(limit=false) + public String index(Model model, HttpServletRequest request) { + if (!PermissionInterceptor.ifLogin(request)) { + return "redirect:/toLogin"; + } + return "redirect:/jobinfo"; + } + + @RequestMapping("/toLogin") + @PermessionLimit(limit=false) + public String toLogin(Model model, HttpServletRequest request) { + if (PermissionInterceptor.ifLogin(request)) { + return "redirect:/"; + } + return "login"; + } + + @RequestMapping(value="login", method=RequestMethod.POST) + @ResponseBody + @PermessionLimit(limit=false) + public ReturnT loginDo(HttpServletRequest request, HttpServletResponse response, String userName, String password){ + if (!PermissionInterceptor.ifLogin(request)) { + if (StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(password) + && "admin".equals(userName) && "123456".equals(password)) { + PermissionInterceptor.login(response); + } else { + return new ReturnT(500, "账号或密码错误"); + } + } + return ReturnT.SUCCESS; + } + + @RequestMapping(value="logout", method=RequestMethod.POST) + @ResponseBody + @PermessionLimit(limit=false) + public ReturnT logout(HttpServletRequest request, HttpServletResponse response){ + if (PermissionInterceptor.ifLogin(request)) { + PermissionInterceptor.logout(request, response); + } + return ReturnT.SUCCESS; } @RequestMapping("/help") diff --git a/xxl-job-admin/src/main/java/com/xxl/job/controller/JobLogController.java b/xxl-job-admin/src/main/java/com/xxl/job/controller/JobLogController.java index 5a840c6a..49b1c80b 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/controller/JobLogController.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/controller/JobLogController.java @@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import com.xxl.job.client.handler.HandlerRepository; import com.xxl.job.client.util.HttpUtil; import com.xxl.job.client.util.HttpUtil.RemoteCallBack; +import com.xxl.job.controller.annotation.PermessionLimit; import com.xxl.job.client.util.JacksonUtil; import com.xxl.job.core.constant.Constants.JobGroupEnum; import com.xxl.job.core.model.ReturnT; @@ -77,6 +78,7 @@ public class JobLogController { @RequestMapping("/save") @ResponseBody + @PermessionLimit(limit=false) public RemoteCallBack triggerLog(int trigger_log_id, String status, String msg) { RemoteCallBack callBack = new RemoteCallBack(); callBack.setStatus(RemoteCallBack.FAIL); diff --git a/xxl-job-admin/src/main/java/com/xxl/job/controller/annotation/PermessionLimit.java b/xxl-job-admin/src/main/java/com/xxl/job/controller/annotation/PermessionLimit.java new file mode 100644 index 00000000..7869844a --- /dev/null +++ b/xxl-job-admin/src/main/java/com/xxl/job/controller/annotation/PermessionLimit.java @@ -0,0 +1,22 @@ +package com.xxl.job.controller.annotation; + + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 权限限制 + * @author xuxueli 2015-12-12 18:29:02 + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface PermessionLimit { + + /** + * 登陆拦截 (默认拦截) + */ + boolean limit() default true; + +} \ No newline at end of file diff --git a/xxl-job-admin/src/main/java/com/xxl/job/controller/interceptor/PermissionInterceptor.java b/xxl-job-admin/src/main/java/com/xxl/job/controller/interceptor/PermissionInterceptor.java new file mode 100644 index 00000000..05b0c47d --- /dev/null +++ b/xxl-job-admin/src/main/java/com/xxl/job/controller/interceptor/PermissionInterceptor.java @@ -0,0 +1,54 @@ +package com.xxl.job.controller.interceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import com.xxl.job.controller.annotation.PermessionLimit; +import com.xxl.job.core.util.CookieUtil; + +/** + * 权限拦截, 简易版 + * @author xuxueli 2015-12-12 18:09:04 + */ +public class PermissionInterceptor extends HandlerInterceptorAdapter { + + public static final String LOGIN_IDENTITY_KEY = "LOGIN_IDENTITY"; + public static final String LOGIN_IDENTITY_VAL = "sdf!121sdf$78sd!8"; + + public static boolean login(HttpServletResponse response){ + CookieUtil.set(response, LOGIN_IDENTITY_KEY, LOGIN_IDENTITY_VAL); + return true; + } + public static void logout(HttpServletRequest request, HttpServletResponse response){ + CookieUtil.remove(request, response, LOGIN_IDENTITY_KEY); + } + public static boolean ifLogin(HttpServletRequest request){ + String indentityInfo = CookieUtil.getValue(request, LOGIN_IDENTITY_KEY); + if (indentityInfo==null || !LOGIN_IDENTITY_VAL.equals(indentityInfo.trim())) { + return false; + } + return true; + } + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + + if (!(handler instanceof HandlerMethod)) { + return super.preHandle(request, response, handler); + } + + if (!ifLogin(request)) { + HandlerMethod method = (HandlerMethod)handler; + PermessionLimit permission = method.getMethodAnnotation(PermessionLimit.class); + if (permission == null || permission.limit()) { + throw new Exception("登陆实效"); + } + } + + return super.preHandle(request, response, handler); + } + +} diff --git a/xxl-job-admin/src/main/java/com/xxl/job/core/util/CookieUtil.java b/xxl-job-admin/src/main/java/com/xxl/job/core/util/CookieUtil.java new file mode 100644 index 00000000..fc023719 --- /dev/null +++ b/xxl-job-admin/src/main/java/com/xxl/job/core/util/CookieUtil.java @@ -0,0 +1,92 @@ +package com.xxl.job.core.util; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Cookie.Util + * @author xuxueli 2015-12-12 18:01:06 + */ +public class CookieUtil { + // 默认缓存时间,单位/秒, 2H + private static final int COOKIE_MAX_AGE = 60 * 60 * 2; + // 保存路径,根路径 + private static final String COOKIE_PATH = "/"; + + /** + * 保存 + * @param response + * @param key + * @param value + */ + public static void set(HttpServletResponse response, String key, String value) { + Cookie cookie = new Cookie(key, value); + cookie.setMaxAge(COOKIE_MAX_AGE); // Cookie过期时间,单位/秒 + cookie.setPath(COOKIE_PATH); // Cookie适用的路径 + response.addCookie(cookie); + } + + /** + * 保存 + * @param request + * @param response + * @param key + * @param value + * @param maxAge + * @param domain + */ + private static void set(HttpServletResponse response, + String key, String value, int maxAge, String path) { + Cookie cookie = new Cookie(key, value); + cookie.setMaxAge(maxAge); // Cookie过期时间,单位/秒 + cookie.setPath(path); // Cookie适用的路径 + response.addCookie(cookie); + } + + /** + * 查询value + * @param request + * @param key + * @return + */ + public static String getValue(HttpServletRequest request, String key) { + Cookie cookie = get(request, key); + if (cookie != null) { + return cookie.getValue(); + } + return null; + } + + /** + * 查询Cookie + * @param request + * @param key + */ + private static Cookie get(HttpServletRequest request, String key) { + Cookie[] arr_cookie = request.getCookies(); + if (arr_cookie != null && arr_cookie.length > 0) { + for (Cookie cookie : arr_cookie) { + if (cookie.getName().equals(key)) { + return cookie; + } + } + } + return null; + } + + /** + * 删除Cookie + * @param request + * @param response + * @param key + * @param domainName + */ + public static void remove(HttpServletRequest request, HttpServletResponse response, String key) { + Cookie cookie = get(request, key); + if (cookie != null) { + set(response, key, "", 0, COOKIE_PATH); + } + } + +} \ No newline at end of file diff --git a/xxl-job-admin/src/main/resources/springmvc-context.xml b/xxl-job-admin/src/main/resources/springmvc-context.xml index 21076803..b0a795b5 100644 --- a/xxl-job-admin/src/main/resources/springmvc-context.xml +++ b/xxl-job-admin/src/main/resources/springmvc-context.xml @@ -38,16 +38,12 @@ - - - + \ No newline at end of file diff --git a/xxl-job-admin/src/main/webapp/500.html b/xxl-job-admin/src/main/webapp/500.html index eb1f4948..ed32d54f 100644 --- a/xxl-job-admin/src/main/webapp/500.html +++ b/xxl-job-admin/src/main/webapp/500.html @@ -1 +1,29 @@ -500 \ No newline at end of file + + + + + 应用程序异常 (500) + + + + + + +
+

应用程序异常

+

抱歉!您访问的页面出现异常,请稍后重试或联系管理员。

+
+ + + \ No newline at end of file diff --git a/xxl-job-admin/src/main/webapp/WEB-INF/template/common/common.exception.ftl b/xxl-job-admin/src/main/webapp/WEB-INF/template/common/common.exception.ftl index 016f78b0..89b9fee7 100644 --- a/xxl-job-admin/src/main/webapp/WEB-INF/template/common/common.exception.ftl +++ b/xxl-job-admin/src/main/webapp/WEB-INF/template/common/common.exception.ftl @@ -2,7 +2,7 @@ - 应用程序异常 (500) + 应用程序异常 (error)