diff --git a/代码库/mall-admin/src/main/java/com/sock/mall/component/RestAuthenticationEntryPoint.java b/代码库/mall-admin/src/main/java/com/sock/mall/component/RestAuthenticationEntryPoint.java new file mode 100644 index 0000000..00deb30 --- /dev/null +++ b/代码库/mall-admin/src/main/java/com/sock/mall/component/RestAuthenticationEntryPoint.java @@ -0,0 +1,27 @@ +package com.macro.mall.component; + +import com.macro.mall.dto.CommonResult; +import com.macro.mall.util.JsonUtil; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.AuthenticationEntryPoint; +import org.springframework.stereotype.Component; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * δ¼tokenʧЧʽӿʱԶķؽ + * Created by macro on 2018/5/14. + */ +@Component +public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {// + @Override + public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + response.getWriter().println(JsonUtil.objectToJson(new CommonResult().unauthorized(authException.getMessage()))); + response.getWriter().flush(); + } +} \ No newline at end of file diff --git a/代码库/mall-admin/src/main/java/com/sock/mall/component/RestfulAccessDeniedHandler.java b/代码库/mall-admin/src/main/java/com/sock/mall/component/RestfulAccessDeniedHandler.java new file mode 100644 index 0000000..262b7a6 --- /dev/null +++ b/代码库/mall-admin/src/main/java/com/sock/mall/component/RestfulAccessDeniedHandler.java @@ -0,0 +1,29 @@ +package com.macro.mall.component; + +import com.macro.mall.dto.CommonResult; +import com.macro.mall.util.JsonUtil; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.web.access.AccessDeniedHandler; +import org.springframework.stereotype.Component; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * ʽӿûȨʱԶķؽ + * Created by macro on 2018/4/26. + */ +@Component +public class RestfulAccessDeniedHandler implements AccessDeniedHandler{//ûзȨ + @Override + public void handle(HttpServletRequest request, + HttpServletResponse response, + AccessDeniedException e) throws IOException, ServletException { + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/json"); + response.getWriter().println(JsonUtil.objectToJson(new CommonResult().forbidden(e.getMessage()))); + response.getWriter().flush(); + } +} \ No newline at end of file diff --git a/代码库/mall-admin/src/main/java/com/sock/mall/component/WebLogAspect.java b/代码库/mall-admin/src/main/java/com/sock/mall/component/WebLogAspect.java new file mode 100644 index 0000000..5d33496 --- /dev/null +++ b/代码库/mall-admin/src/main/java/com/sock/mall/component/WebLogAspect.java @@ -0,0 +1,128 @@ +package com.macro.mall.component; + +import com.macro.mall.bo.WebLog; +import com.macro.mall.util.JsonUtil; +import com.macro.mall.util.RequestUtil; +import io.swagger.annotations.ApiOperation; +import net.logstash.logback.marker.Markers; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.Signature; +import org.aspectj.lang.annotation.*; +import org.aspectj.lang.reflect.MethodSignature; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * ͳһ־ + * Created by macro on 2018/4/26. + */ +@Aspect//javaඨΪ +@Component +@Order(1) +public class WebLogAspect { + private static final Logger LOGGER = LoggerFactory.getLogger(WebLogAspect.class); + private ThreadLocal startTime = new ThreadLocal<>(); + /** + * һ + * ~һ*ηֵⷵ + * ~ڶ*controllerӰ + * ~*ⷽ + * ~..ƥIJ + */ + + @Pointcut("execution(public * com.macro.mall.controller.*.*(..))") + public void webLog() { + } + + @Before("webLog()") + public void doBefore(JoinPoint joinPoint) throws Throwable { + startTime.set(System.currentTimeMillis()); + } + + @AfterReturning(value = "webLog()", returning = "ret") + public void doAfterReturning(Object ret) throws Throwable { + } + + @Around("webLog()") + public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { + //ȡǰ + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + HttpServletRequest request = attributes.getRequest(); + //¼Ϣ(ͨlogstashelasticsearch) + WebLog webLog = new WebLog(); + Object result = joinPoint.proceed(); + Signature signature = joinPoint.getSignature(); + MethodSignature methodSignature = (MethodSignature) signature; + Method method = methodSignature.getMethod(); + if (method.isAnnotationPresent(ApiOperation.class)) { + ApiOperation log = method.getAnnotation(ApiOperation.class); + webLog.setDescription(log.value()); + } + long endTime = System.currentTimeMillis(); + webLog.setBasePath(RequestUtil.getBasePath(request)); + webLog.setIp(request.getRemoteUser()); + webLog.setMethod(request.getMethod()); + webLog.setParameter(getParameter(method, joinPoint.getArgs())); + webLog.setResult(result); + webLog.setSpendTime((int) (endTime - startTime.get())); + webLog.setStartTime(startTime.get()); + webLog.setUri(request.getRequestURI()); + webLog.setUrl(request.getRequestURL().toString()); + Map logMap = new HashMap<>(); + logMap.put("url",webLog.getUrl()); + logMap.put("method",webLog.getMethod()); + logMap.put("parameter",webLog.getParameter()); + logMap.put("spendTime",webLog.getSpendTime()); + logMap.put("description",webLog.getDescription()); + // LOGGER.info("{}", JsonUtil.objectToJson(webLog)); + LOGGER.info(Markers.appendEntries(logMap),JsonUtil.objectToJson(webLog)); + return result;//󣬷 + } + + /** + * ݷʹIJȡ + */ + private Object getParameter(Method method, Object[] args) { + List argList = new ArrayList<>();//ArrayList[]LIst[Object] + Parameter[] parameters = method.getParameters(); + for (int i = 0; i < parameters.length; i++) { + RequestBody requestBody = parameters[i].getAnnotation(RequestBody.class);//getAnnotationڷԪصָ͵ע + if (requestBody != null) {//ӦHttpbodyΪ + argList.add(args[i]);// + } + RequestParam requestParam = parameters[i].getAnnotation(RequestParam.class); + if (requestParam != null) {//Ϊ + Map map = new HashMap<>(); //map + String key = parameters[i].getName();//ȡ + if (!StringUtils.isEmpty(requestParam.value())) {//ֵգkeyزֵ + key = requestParam.value(); + } + map.put(key, args[i]);//argue[i]key + argList.add(map);//һmapmapзi=nʱargֵ + } + } + if (argList.size() == 0) {//Ϊ0ʱؿ + return null; + } else if (argList.size() == 1) {//Ϊ1ʱűֵ + return argList.get(0); + } else { + return argList; + } + } +} \ No newline at end of file