Commit d32a3558 by fsn

日志记录添加业务日志记录,并改为线程池记录方式

parent 4904f5a5
package com.stylefeng.guns.common.annotion.log;
import java.lang.annotation.*;
/**
* 标记需要做业务日志的方法
*
* @author fengshuonan
* @date 2017-03-31 12:46
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface BussinessLog {
String value() default "";
}
package com.stylefeng.guns.common.annotion.log;
import java.lang.annotation.*;
/**
* 标记保存编辑操作之前的controller方法
*
* @author fengshuonan
* @date 2017-03-31 9:37
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface LogBefore {
}
...@@ -4,8 +4,8 @@ import com.stylefeng.guns.common.constant.tips.ErrorTip; ...@@ -4,8 +4,8 @@ import com.stylefeng.guns.common.constant.tips.ErrorTip;
import com.stylefeng.guns.common.exception.BizExceptionEnum; import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException; import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.core.log.LogManager; import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.support.HttpKit;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.CredentialsException; import org.apache.shiro.authc.CredentialsException;
...@@ -16,8 +16,9 @@ import org.springframework.web.bind.annotation.ControllerAdvice; ...@@ -16,8 +16,9 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.ResponseStatus;
import javax.naming.NoPermissionException; import javax.naming.NoPermissionException;
import static com.stylefeng.guns.core.support.HttpKit.getIp;
import static com.stylefeng.guns.core.support.HttpKit.getRequest;
/** /**
* 全局的的异常拦截器(拦截所有的控制器)(带有@RequestMapping注解的方法上都会拦截) * 全局的的异常拦截器(拦截所有的控制器)(带有@RequestMapping注解的方法上都会拦截)
...@@ -39,8 +40,8 @@ public class GlobalExceptionHandler { ...@@ -39,8 +40,8 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody @ResponseBody
public ErrorTip notFount(BussinessException e) { public ErrorTip notFount(BussinessException e) {
LogManager.exceptionLog(ShiroKit.getUser().getId(), e); LogManager.me().executeLog(LogTaskFactory.exceptionLog(ShiroKit.getUser().getId(), e));
HttpKit.getRequest().setAttribute("tip", e.getMessage()); getRequest().setAttribute("tip", e.getMessage());
return new ErrorTip(e.getCode(), e.getMessage()); return new ErrorTip(e.getCode(), e.getMessage());
} }
...@@ -53,8 +54,8 @@ public class GlobalExceptionHandler { ...@@ -53,8 +54,8 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody @ResponseBody
public ErrorTip notFount(RuntimeException e) { public ErrorTip notFount(RuntimeException e) {
LogManager.exceptionLog(ShiroKit.getUser().getId(), e); LogManager.me().executeLog(LogTaskFactory.exceptionLog(ShiroKit.getUser().getId(), e));
HttpKit.getRequest().setAttribute("tip", "服务器未知运行时异常"); getRequest().setAttribute("tip", "服务器未知运行时异常");
return new ErrorTip(BizExceptionEnum.SERVER_ERROR); return new ErrorTip(BizExceptionEnum.SERVER_ERROR);
} }
...@@ -78,8 +79,8 @@ public class GlobalExceptionHandler { ...@@ -78,8 +79,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler(DisabledAccountException.class) @ExceptionHandler(DisabledAccountException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseStatus(HttpStatus.UNAUTHORIZED)
public String accountLocked(DisabledAccountException e, Model model) { public String accountLocked(DisabledAccountException e, Model model) {
String username = HttpKit.getRequest().getParameter("username"); String username = getRequest().getParameter("username");
LogManager.loginLog(username,"账号被冻结"); LogManager.me().executeLog(LogTaskFactory.loginLog(username, "账号被冻结", getIp()));
model.addAttribute("tips", "账号被冻结"); model.addAttribute("tips", "账号被冻结");
return "/login.html"; return "/login.html";
} }
...@@ -92,8 +93,8 @@ public class GlobalExceptionHandler { ...@@ -92,8 +93,8 @@ public class GlobalExceptionHandler {
@ExceptionHandler(CredentialsException.class) @ExceptionHandler(CredentialsException.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseStatus(HttpStatus.UNAUTHORIZED)
public String credentials(CredentialsException e, Model model) { public String credentials(CredentialsException e, Model model) {
String username = HttpKit.getRequest().getParameter("username"); String username = getRequest().getParameter("username");
LogManager.loginLog(username,"账号密码错误"); LogManager.me().executeLog(LogTaskFactory.loginLog(username, "账号密码错误", getIp()));
model.addAttribute("tips", "账号密码错误"); model.addAttribute("tips", "账号密码错误");
return "/login.html"; return "/login.html";
} }
...@@ -107,7 +108,7 @@ public class GlobalExceptionHandler { ...@@ -107,7 +108,7 @@ public class GlobalExceptionHandler {
@ResponseStatus(HttpStatus.UNAUTHORIZED) @ResponseStatus(HttpStatus.UNAUTHORIZED)
@ResponseBody @ResponseBody
public ErrorTip credentials(NoPermissionException e, Model model) { public ErrorTip credentials(NoPermissionException e, Model model) {
HttpKit.getRequest().setAttribute("tip", "权限异常"); getRequest().setAttribute("tip", "权限异常");
return new ErrorTip(BizExceptionEnum.NO_PERMITION); return new ErrorTip(BizExceptionEnum.NO_PERMITION);
} }
......
package com.stylefeng.guns.core.aop; package com.stylefeng.guns.core.aop;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.core.log.LogManager; import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser; import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.core.support.StrKit; import com.stylefeng.guns.core.support.StrKit;
...@@ -28,7 +30,7 @@ public class LogAop { ...@@ -28,7 +30,7 @@ public class LogAop {
private Logger log = Logger.getLogger(this.getClass()); private Logger log = Logger.getLogger(this.getClass());
@Pointcut("execution(* com.stylefeng.guns..service.*.*(..))") @Pointcut(value = "@annotation(com.stylefeng.guns.common.annotion.log.BussinessLog)")
public void cutService() { public void cutService() {
} }
...@@ -50,6 +52,10 @@ public class LogAop { ...@@ -50,6 +52,10 @@ public class LogAop {
String className = point.getTarget().getClass().getName(); String className = point.getTarget().getClass().getName();
Object[] params = point.getArgs(); Object[] params = point.getArgs();
//获取操作名称
BussinessLog annotation = method.getAnnotation(BussinessLog.class);
String bussinessName = annotation.value();
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Object param : params) { for (Object param : params) {
sb.append(param); sb.append(param);
...@@ -59,7 +65,7 @@ public class LogAop { ...@@ -59,7 +65,7 @@ public class LogAop {
String msg = ToolUtil.format("[时间]:{} [类名]:{} [方法]:{} [参数]:{}", DateUtil.getTime(), className, methodName, sb.toString()); String msg = ToolUtil.format("[时间]:{} [类名]:{} [方法]:{} [参数]:{}", DateUtil.getTime(), className, methodName, sb.toString());
msg = StrKit.removeSuffix(msg, "& "); msg = StrKit.removeSuffix(msg, "& ");
log.info(msg); log.info(msg);
LogManager.bussinessLog(user.getId(),className,methodName,msg); LogManager.me().executeLog(LogTaskFactory.bussinessLog(user.getId(),bussinessName,className,methodName,msg));
return point.proceed(); return point.proceed();
} }
} }
\ No newline at end of file
package com.stylefeng.guns.core.log;
import java.util.HashMap;
/**
* 方法名和具体日志记录名称的映射管理
*
* @author fengshuonan
* @date 2017-03-30 19:22
*/
public class ConcreteLogNameHolder {
private HashMap<String, String> mapping = new HashMap<>();
private void init(){
mapping.put("setAuthority","分配角色权限");
}
private ConcreteLogNameHolder() {
init();
}
private static ConcreteLogNameHolder holder = new ConcreteLogNameHolder();
public static ConcreteLogNameHolder me(){
return holder;
}
public String get(String key){
return mapping.get(key);
}
}
package com.stylefeng.guns.core.log; package com.stylefeng.guns.core.log;
import com.stylefeng.guns.common.constant.state.LogType; import java.util.TimerTask;
import com.stylefeng.guns.core.db.Db; import java.util.concurrent.ScheduledThreadPoolExecutor;
import com.stylefeng.guns.core.util.ToolUtil; import java.util.concurrent.TimeUnit;
import com.stylefeng.guns.persistence.dao.LoginLogMapper;
import com.stylefeng.guns.persistence.dao.OperationLogMapper;
import com.stylefeng.guns.persistence.model.LoginLog;
import com.stylefeng.guns.persistence.model.OperationLog;
import org.apache.log4j.Logger;
/** /**
* 日志管理器 * 日志管理器
...@@ -17,54 +12,22 @@ import org.apache.log4j.Logger; ...@@ -17,54 +12,22 @@ import org.apache.log4j.Logger;
*/ */
public class LogManager { public class LogManager {
private static Logger logger = Logger.getLogger(LogManager.class); //日志记录操作延时
private static LoginLogMapper loginLogMapper = Db.getMapper(LoginLogMapper.class); private final int OPERATE_DELAY_TIME = 10;
private static OperationLogMapper operationLogMapper = Db.getMapper(OperationLogMapper.class);
public static void loginLog(Integer userId) { //异步操作记录日志的线程池
LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null); private ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);
try {
loginLogMapper.insert(loginLog);
} catch (Exception e) {
logger.error("创建登录日志异常!", e);
}
}
public static void loginLog(String username, String msg) { private LogManager() {
LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN_FAIL, null, "账号:" + username + "," + msg);
try {
loginLogMapper.insert(loginLog);
} catch (Exception e) {
logger.error("创建登录失败异常!", e);
}
} }
public static void exitLog(Integer userId) { public static LogManager logManager = new LogManager();
LoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null);
try {
loginLogMapper.insert(loginLog);
} catch (Exception e) {
logger.error("创建退出日志异常!", e);
}
}
public static void bussinessLog(Integer userId, String clazzName, String methodName, String msg) { public static LogManager me() {
OperationLog operationLog = LogFactory.createOperationLog(LogType.BUSSINESS, userId, clazzName, methodName, msg); return logManager;
try {
operationLogMapper.insert(operationLog);
} catch (Exception e) {
logger.error("创建业务日志异常!", e);
}
} }
public static void exceptionLog(Integer userId, Exception exception) { public void executeLog(TimerTask task) {
String msg = ToolUtil.getExceptionMsg(exception); executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
OperationLog operationLog = LogFactory.createOperationLog(LogType.EXCEPTION, userId, null, null, msg);
try {
operationLogMapper.insert(operationLog);
} catch (Exception e) {
logger.error("创建异常日志异常!", e);
}
} }
} }
package com.stylefeng.guns.core.log;
import com.stylefeng.guns.core.util.SpringContextHolder;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
/**
* 被修改的bean临时存放的地方
*
* @author fengshuonan
* @date 2017-03-31 11:19
*/
@Component
@Scope(scopeName = WebApplicationContext.SCOPE_SESSION)
public class LogObjectHolder {
private Object object = null;
public void set(Object obj) {
this.object = obj;
}
public Object get() {
return object;
}
public static LogObjectHolder me(){
LogObjectHolder bean = SpringContextHolder.getBean(LogObjectHolder.class);
return bean;
}
}
package com.stylefeng.guns.core.log; package com.stylefeng.guns.core.log.factory;
import com.stylefeng.guns.common.constant.state.LogSucceed; import com.stylefeng.guns.common.constant.state.LogSucceed;
import com.stylefeng.guns.common.constant.state.LogType; import com.stylefeng.guns.common.constant.state.LogType;
import com.stylefeng.guns.core.support.HttpKit;
import com.stylefeng.guns.persistence.model.LoginLog; import com.stylefeng.guns.persistence.model.LoginLog;
import com.stylefeng.guns.persistence.model.OperationLog; import com.stylefeng.guns.persistence.model.OperationLog;
...@@ -22,15 +21,15 @@ public class LogFactory { ...@@ -22,15 +21,15 @@ public class LogFactory {
* @author fengshuonan * @author fengshuonan
* @Date 2017/3/30 18:45 * @Date 2017/3/30 18:45
*/ */
public static OperationLog createOperationLog(LogType logType, Integer userId, String clazzName, String methodName, String msg) { public static OperationLog createOperationLog(LogType logType, Integer userId, String bussinessName, String clazzName, String methodName, String msg, LogSucceed succeed) {
OperationLog operationLog = new OperationLog(); OperationLog operationLog = new OperationLog();
operationLog.setLogtype(logType.getMessage()); operationLog.setLogtype(logType.getMessage());
operationLog.setLogname(ConcreteLogNameHolder.me().get(methodName)); operationLog.setLogname(bussinessName);
operationLog.setUserid(userId); operationLog.setUserid(userId);
operationLog.setClassname(clazzName); operationLog.setClassname(clazzName);
operationLog.setMethod(methodName); operationLog.setMethod(methodName);
operationLog.setCreatetime(new Date()); operationLog.setCreatetime(new Date());
operationLog.setSucceed(LogSucceed.SUCCESS.getMessage()); operationLog.setSucceed(succeed.getMessage());
operationLog.setMessage(msg); operationLog.setMessage(msg);
return operationLog; return operationLog;
} }
...@@ -41,13 +40,13 @@ public class LogFactory { ...@@ -41,13 +40,13 @@ public class LogFactory {
* @author fengshuonan * @author fengshuonan
* @Date 2017/3/30 18:46 * @Date 2017/3/30 18:46
*/ */
public static LoginLog createLoginLog(LogType logType, Integer userId, String msg) { public static LoginLog createLoginLog(LogType logType, Integer userId, String msg,String ip) {
LoginLog loginLog = new LoginLog(); LoginLog loginLog = new LoginLog();
loginLog.setLogname(logType.getMessage()); loginLog.setLogname(logType.getMessage());
loginLog.setUserid(userId); loginLog.setUserid(userId);
loginLog.setCreatetime(new Date()); loginLog.setCreatetime(new Date());
loginLog.setSucceed(LogSucceed.SUCCESS.getMessage()); loginLog.setSucceed(LogSucceed.SUCCESS.getMessage());
loginLog.setIp(HttpKit.getRequest().getRemoteHost()); loginLog.setIp(ip);
loginLog.setMessage(msg); loginLog.setMessage(msg);
return loginLog; return loginLog;
} }
......
package com.stylefeng.guns.core.log.factory;
import com.stylefeng.guns.common.constant.state.LogSucceed;
import com.stylefeng.guns.common.constant.state.LogType;
import com.stylefeng.guns.core.db.Db;
import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.persistence.dao.LoginLogMapper;
import com.stylefeng.guns.persistence.dao.OperationLogMapper;
import com.stylefeng.guns.persistence.model.LoginLog;
import com.stylefeng.guns.persistence.model.OperationLog;
import org.apache.log4j.Logger;
import java.util.TimerTask;
/**
* 日志操作任务创建工厂
*
* @author fengshuonan
* @date 2016年12月6日 下午9:18:27
*/
public class LogTaskFactory {
private static Logger logger = Logger.getLogger(LogManager.class);
private static LoginLogMapper loginLogMapper = Db.getMapper(LoginLogMapper.class);
private static OperationLogMapper operationLogMapper = Db.getMapper(OperationLogMapper.class);
public static TimerTask loginLog(Integer userId, String ip) {
return new TimerTask() {
@Override
public void run() {
try {
LoginLog loginLog = LogFactory.createLoginLog(LogType.LOGIN, userId, null, ip);
loginLogMapper.insert(loginLog);
} catch (Exception e) {
logger.error("创建登录日志异常!", e);
}
}
};
}
public static TimerTask loginLog(String username, String msg, String ip) {
return new TimerTask() {
@Override
public void run() {
LoginLog loginLog = LogFactory.createLoginLog(
LogType.LOGIN_FAIL, null, "账号:" + username + "," + msg, ip);
try {
loginLogMapper.insert(loginLog);
} catch (Exception e) {
logger.error("创建登录失败异常!", e);
}
}
};
}
public static TimerTask exitLog(Integer userId,String ip) {
return new TimerTask() {
@Override
public void run() {
LoginLog loginLog = LogFactory.createLoginLog(LogType.EXIT, userId, null,ip);
try {
loginLogMapper.insert(loginLog);
} catch (Exception e) {
logger.error("创建退出日志异常!", e);
}
}
};
}
public static TimerTask bussinessLog(Integer userId, String bussinessName, String clazzName, String methodName, String msg) {
return new TimerTask() {
@Override
public void run() {
OperationLog operationLog = LogFactory.createOperationLog(
LogType.BUSSINESS, userId, bussinessName, clazzName, methodName, msg, LogSucceed.SUCCESS);
try {
operationLogMapper.insert(operationLog);
} catch (Exception e) {
logger.error("创建业务日志异常!", e);
}
}
};
}
public static TimerTask exceptionLog(Integer userId, Exception exception) {
return new TimerTask() {
@Override
public void run() {
String msg = ToolUtil.getExceptionMsg(exception);
OperationLog operationLog = LogFactory.createOperationLog(
LogType.EXCEPTION, userId, "", null, null, msg, LogSucceed.FAIL);
try {
operationLogMapper.insert(operationLog);
} catch (Exception e) {
logger.error("创建异常日志异常!", e);
}
}
};
}
}
/** /**
* Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com). * Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com).
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
...@@ -15,157 +15,177 @@ ...@@ -15,157 +15,177 @@
*/ */
package com.stylefeng.guns.core.support; package com.stylefeng.guns.core.support;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.servlet.http.HttpServletRequest; public class HttpKit {
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder; public static String getIp(){
import org.springframework.web.context.request.ServletRequestAttributes; return HttpKit.getRequest().getRemoteHost();
}
public class HttpKit { /**
* 获取所有请求的值
/** */
* 获取 HttpServletRequest public static Map<String, String> getRequestParameters() {
*/ HashMap<String, String> values = new HashMap<>();
public static HttpServletResponse getResponse() { HttpServletRequest request = HttpKit.getRequest();
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); Enumeration enums = request.getParameterNames();
return response; while ( enums.hasMoreElements()){
} String paramName = (String) enums.nextElement();
String paramValue = request.getParameter(paramName);
values.put(paramName, paramValue);
}
return values;
}
/**
* 获取 HttpServletRequest
*/
public static HttpServletResponse getResponse() {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
return response;
}
/**
* 获取 包装防Xss Sql注入的 HttpServletRequest
* @return request
*/
public static HttpServletRequest getRequest() {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return new WafRequestWrapper(request);
}
/** /**
* 获取 包装防Xss Sql注入的 HttpServletRequest * 向指定URL发送GET方法的请求
* @return request *
*/ * @param url 发送请求的URL
public static HttpServletRequest getRequest() { * @param param 请求参数
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); * @return URL 所代表远程资源的响应结果
return new WafRequestWrapper(request); */
} public static String sendGet(String url, Map<String, String> param) {
String result = "";
BufferedReader in = null;
try {
String para = "";
for (String key : param.keySet()) {
para += (key + "=" + param.get(key) + "&");
}
if (para.lastIndexOf("&") > 0) {
para = para.substring(0, para.length() - 1);
}
String urlNameString = url + "?" + para;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
/** /**
* 向指定URL发送GET方法的请求 * 向指定 URL 发送POST方法的请求
* *
* @param url 发送请求的URL * @param url 发送请求的 URL
* @param param 请求参数 * @param param 请求参数
* @return URL 所代表远程资源的响应结果 * @return 所代表远程资源的响应结果
*/ */
public static String sendGet(String url, Map<String, String> param) { public static String sendPost(String url, Map<String, String> param) {
String result = ""; PrintWriter out = null;
BufferedReader in = null; BufferedReader in = null;
try { String result = "";
String para = ""; try {
for (String key : param.keySet()) { String para = "";
para += (key + "=" + param.get(key) + "&"); for (String key : param.keySet()) {
} para += (key + "=" + param.get(key) + "&");
if (para.lastIndexOf("&") > 0) { }
para = para.substring(0, para.length() - 1); if (para.lastIndexOf("&") > 0) {
} para = para.substring(0, para.length() - 1);
String urlNameString = url + "?" + para; }
URL realUrl = new URL(urlNameString); String urlNameString = url + "?" + para;
// 打开和URL之间的连接 URL realUrl = new URL(urlNameString);
URLConnection connection = realUrl.openConnection(); // 打开和URL之间的连接
// 设置通用的请求属性 URLConnection conn = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*"); // 设置通用的请求属性
connection.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("accept", "*/*");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); conn.setRequestProperty("connection", "Keep-Alive");
// 建立实际的连接 conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.connect(); // 发送POST请求必须设置如下两行
// 获取所有响应头字段 conn.setDoOutput(true);
Map<String, List<String>> map = connection.getHeaderFields(); conn.setDoInput(true);
// 遍历所有的响应头字段 // 获取URLConnection对象对应的输出流
for (String key : map.keySet()) { out = new PrintWriter(conn.getOutputStream());
System.out.println(key + "--->" + map.get(key)); // 发送请求参数
} out.print(param);
// 定义 BufferedReader输入流来读取URL的响应 // flush输出流的缓冲
in = new BufferedReader(new InputStreamReader(connection.getInputStream())); out.flush();
String line; // 定义BufferedReader输入流来读取URL的响应
while ((line = in.readLine()) != null) { in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
result += line; String line;
} while ((line = in.readLine()) != null) {
} catch (Exception e) { result += line;
System.out.println("发送GET请求出现异常!" + e); }
e.printStackTrace(); } catch (Exception e) {
} System.out.println("发送 POST 请求出现异常!" + e);
// 使用finally块来关闭输入流 e.printStackTrace();
finally { }
try { // 使用finally块来关闭输出流、输入流
if (in != null) { finally {
in.close(); try {
} if (out != null) {
} catch (Exception e2) { out.close();
e2.printStackTrace(); }
} if (in != null) {
} in.close();
return result; }
} } catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, Map<String, String> param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
String para = "";
for (String key : param.keySet()) {
para += (key + "=" + param.get(key) + "&");
}
if (para.lastIndexOf("&") > 0) {
para = para.substring(0, para.length() - 1);
}
String urlNameString = url + "?" + para;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
} }
package com.stylefeng.guns.core.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 对比两个对象的变化的工具类
*
* @author fengshuonan
* @Date 2017/3/31 10:36
*/
public class Contrast {
//记录每个修改字段的分隔符
public static final String separator = ";;;";
public static String contrastObj(Object pojo1, Object pojo2) {
String str = "";
try {
Class clazz = pojo1.getClass();
Field[] fields = pojo1.getClass().getDeclaredFields();
int i = 1;
for (Field field : fields) {
if ("serialVersionUID".equals(field.getName())) {
continue;
}
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
Method getMethod = pd.getReadMethod();
Object o1 = getMethod.invoke(pojo1);
Object o2 = getMethod.invoke(pojo2);
if (o1 == null || o2 == null) {
continue;
}
if (!o1.toString().equals(o2.toString())) {
if (i != 1) {
str += separator;
}
str += "字段名称" + field.getName() + ",旧值:" + o1 + ",新值:" + o2;
i++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
\ No newline at end of file
...@@ -3,6 +3,7 @@ package com.stylefeng.guns.modular.system.controller; ...@@ -3,6 +3,7 @@ package com.stylefeng.guns.modular.system.controller;
import com.stylefeng.guns.common.controller.BaseController; import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.node.MenuNode; import com.stylefeng.guns.common.node.MenuNode;
import com.stylefeng.guns.core.log.LogManager; import com.stylefeng.guns.core.log.LogManager;
import com.stylefeng.guns.core.log.factory.LogTaskFactory;
import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.shiro.ShiroUser; import com.stylefeng.guns.core.shiro.ShiroUser;
import com.stylefeng.guns.modular.system.dao.MenuDao; import com.stylefeng.guns.modular.system.dao.MenuDao;
...@@ -16,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMethod; ...@@ -16,6 +17,8 @@ import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List; import java.util.List;
import static com.stylefeng.guns.core.support.HttpKit.getIp;
/** /**
* 登录控制器 * 登录控制器
* *
...@@ -71,7 +74,7 @@ public class LoginController extends BaseController { ...@@ -71,7 +74,7 @@ public class LoginController extends BaseController {
ShiroUser shiroUser = ShiroKit.getUser(); ShiroUser shiroUser = ShiroKit.getUser();
super.getSession().setAttribute("shiroUser", shiroUser); super.getSession().setAttribute("shiroUser", shiroUser);
LogManager.loginLog(shiroUser.getId()); LogManager.me().executeLog(LogTaskFactory.loginLog(shiroUser.getId(), getIp()));
return REDIRECT + "/"; return REDIRECT + "/";
} }
...@@ -81,7 +84,7 @@ public class LoginController extends BaseController { ...@@ -81,7 +84,7 @@ public class LoginController extends BaseController {
*/ */
@RequestMapping(value = "/logout", method = RequestMethod.GET) @RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logOut() { public String logOut() {
LogManager.exitLog(ShiroKit.getUser().getId()); LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), getIp()));
ShiroKit.getSubject().logout(); ShiroKit.getSubject().logout();
super.getSession().invalidate(); super.getSession().invalidate();
return REDIRECT + "/login"; return REDIRECT + "/login";
......
...@@ -8,6 +8,8 @@ import com.stylefeng.guns.common.controller.BaseController; ...@@ -8,6 +8,8 @@ import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.exception.BizExceptionEnum; import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException; import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.node.ZTreeNode; import com.stylefeng.guns.common.node.ZTreeNode;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.Contrast;
import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.MenuDao; import com.stylefeng.guns.modular.system.dao.MenuDao;
import com.stylefeng.guns.modular.system.warpper.MenuWarpper; import com.stylefeng.guns.modular.system.warpper.MenuWarpper;
...@@ -48,7 +50,6 @@ public class MenuController extends BaseController { ...@@ -48,7 +50,6 @@ public class MenuController extends BaseController {
*/ */
@RequestMapping("") @RequestMapping("")
public String index() { public String index() {
return PREFIX + "menu.html"; return PREFIX + "menu.html";
} }
...@@ -61,7 +62,7 @@ public class MenuController extends BaseController { ...@@ -61,7 +62,7 @@ public class MenuController extends BaseController {
} }
/** /**
* 跳转到菜单列表列表页面 * 跳转到菜单详情列表页面
*/ */
@RequestMapping(value = "/menu_edit/{menuId}") @RequestMapping(value = "/menu_edit/{menuId}")
public String menuEdit(@PathVariable Integer menuId, Model model) { public String menuEdit(@PathVariable Integer menuId, Model model) {
...@@ -69,6 +70,9 @@ public class MenuController extends BaseController { ...@@ -69,6 +70,9 @@ public class MenuController extends BaseController {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL); throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
} }
Menu menu = this.menuMapper.selectById(menuId); Menu menu = this.menuMapper.selectById(menuId);
LogObjectHolder.me().set(menu);
model.addAttribute(menu); model.addAttribute(menu);
return PREFIX + "menu_edit.html"; return PREFIX + "menu_edit.html";
} }
...@@ -121,6 +125,11 @@ public class MenuController extends BaseController { ...@@ -121,6 +125,11 @@ public class MenuController extends BaseController {
if (result.hasErrors()) { if (result.hasErrors()) {
throw new BussinessException(BizExceptionEnum.REQUEST_NULL); throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
} }
Object obj1 = LogObjectHolder.me().get();
Object obj2 = menu;
System.out.println(Contrast.contrastObj(obj1,obj2));
this.menuMapper.updateById(menu); this.menuMapper.updateById(menu);
return SUCCESS_TIP; return SUCCESS_TIP;
} }
......
package com.stylefeng.guns.modular.system.service.impl; package com.stylefeng.guns.modular.system.service.impl;
import com.stylefeng.guns.common.annotion.log.BussinessLog;
import com.stylefeng.guns.core.util.Convert; import com.stylefeng.guns.core.util.Convert;
import com.stylefeng.guns.modular.system.dao.RoleDao; import com.stylefeng.guns.modular.system.dao.RoleDao;
import com.stylefeng.guns.modular.system.service.IRoleService; import com.stylefeng.guns.modular.system.service.IRoleService;
...@@ -20,6 +21,7 @@ public class RoleServiceImpl implements IRoleService { ...@@ -20,6 +21,7 @@ public class RoleServiceImpl implements IRoleService {
RelationMapper relationMapper; RelationMapper relationMapper;
@Override @Override
@BussinessLog("分配角色权限")
@Transactional(readOnly = false) @Transactional(readOnly = false)
public void setAuthority(Integer roleId, String ids) { public void setAuthority(Integer roleId, String ids) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment