Search in sources :

Example 6 with Log

use of vip.mate.core.log.annotation.Log in project matecloud by matevip.

the class AuthController method getUser.

@Log(value = "用户信息", exception = "用户信息请求异常")
@GetMapping("/get/user")
@ApiImplicitParams({ @ApiImplicitParam(name = "Mate-Auth", required = true, value = "授权类型", paramType = "header") })
@ApiOperation(value = "用户信息", notes = "用户信息")
public Result<?> getUser(HttpServletRequest request) {
    LoginUser loginUser = SecurityUtil.getUsername(request);
    UserInfo userInfo = null;
    /**
     * 根据type来判断调用哪个接口登录,待扩展社交登录模式
     * type 1:用户名和密码登录 2:手机号码登录
     */
    if (loginUser.getType() == LoginType.MOBILE.getType()) {
        userInfo = sysUserProvider.getUserByMobile(loginUser.getAccount()).getData();
    } else {
        userInfo = sysUserProvider.getUserByUserName(loginUser.getAccount()).getData();
    }
    Map<String, Object> data = new HashMap<>(7);
    data.put("username", loginUser.getAccount());
    data.put("avatar", userInfo.getSysUser().getAvatar());
    data.put("roleId", userInfo.getSysUser().getRoleId());
    data.put("departId", userInfo.getSysUser().getDepartId());
    data.put("tenantId", userInfo.getSysUser().getTenantId());
    data.put("realName", userInfo.getSysUser().getRealName());
    data.put("nickName", userInfo.getSysUser().getName());
    List<String> stringList = sysRolePermissionProvider.getMenuIdByRoleId(String.valueOf(userInfo.getSysUser().getRoleId()));
    data.put("permissions", stringList);
    // 存入redis,以用于mate-starter-auth的PreAuthAspect查询权限使用
    redisService.set(Oauth2Constant.MATE_PERMISSION_PREFIX + loginUser.getAccount() + StringPool.DOT + userInfo.getSysUser().getRoleId(), data);
    return Result.data(data);
}
Also used : HashMap(java.util.HashMap) UserInfo(vip.mate.system.dto.UserInfo) LoginUser(vip.mate.core.common.entity.LoginUser) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) Log(vip.mate.core.log.annotation.Log) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with Log

use of vip.mate.core.log.annotation.Log in project matecloud by matevip.

the class SysCodeController method execute.

/**
 * 代码生成
 * @param packageName 包名
 * @param prefix 前缀
 * @param modelName 模块名
 * @param datasourceId 数据源名
 * @param tableName 表名
 */
@PreAuth
@Log(value = "代码生成", exception = "代码生成请求异常")
@ApiOperation(value = "代码生成", notes = "代码生成")
@PostMapping("/generator-code")
public void execute(@RequestParam String packageName, @RequestParam String prefix, @RequestParam String modelName, @RequestParam String datasourceId, @RequestParam String tableName, HttpServletRequest request, HttpServletResponse response) {
    SysDataSource sysDataSource = sysDataSourceService.getById(datasourceId);
    String outputDir = System.getProperty("user.dir") + File.separator + "temp" + File.separator + "generator" + File.separator + UUID.randomUUID().toString();
    CodeConfig config = new CodeConfig();
    config.setDbType(DbType.getDbType(sysDataSource.getName()));
    config.setUrl(sysDataSource.getUrl());
    config.setUsername(sysDataSource.getUsername());
    config.setPassword(sysDataSource.getPassword());
    config.setDriverName(sysDataSource.getDriverClass());
    config.setModelName(modelName);
    config.setOutputDir(outputDir);
    config.setPackageName(packageName);
    config.setTableName(tableName);
    config.setPrefix(prefix);
    config.setOutputDir(outputDir);
    GeneratorUtil.execute(config);
    String fileName = "code" + UUID.randomUUID().toString() + ".zip";
    String filePath = outputDir + File.separator + fileName;
    // 压缩目录
    String[] srcDir = { outputDir + File.separator + (config.getPackageName().substring(0, config.getPackageName().indexOf("."))) };
    try {
        ZipUtil.toZip(srcDir, filePath, true);
        FileUtil.download(filePath, fileName, true, response);
    } catch (Exception e) {
        log.error(e.getMessage());
    }
}
Also used : SysDataSource(vip.mate.code.entity.SysDataSource) CodeConfig(vip.mate.code.dto.CodeConfig) PreAuth(vip.mate.core.auth.annotation.PreAuth) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(vip.mate.core.log.annotation.Log) ApiOperation(io.swagger.annotations.ApiOperation)

Example 8 with Log

use of vip.mate.core.log.annotation.Log in project matecloud by matevip.

the class LogAspect method logAfterThrowing.

/**
 * 配置异常通知
 *
 * @param point join point for advice
 * @param e exception
 */
@AfterThrowing(pointcut = "pointcut()", throwing = "e")
public void logAfterThrowing(JoinPoint point, Throwable e) {
    // 打印执行时间
    long startTime = System.nanoTime();
    CommonLog commonLog = new CommonLog();
    // 获取IP和地区
    String ip = RequestHolder.getHttpServletRequestIpAddress();
    String region = IPUtil.getCityInfo(ip);
    //  获取request
    HttpServletRequest request = RequestHolder.getHttpServletRequest();
    // 请求方法
    String method = request.getMethod();
    String url = request.getRequestURI();
    //  获取注解里的value值
    Method targetMethod = resolveMethod((ProceedingJoinPoint) point);
    Log logAnn = targetMethod.getAnnotation(Log.class);
    commonLog.setExecuteTime(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)).setIp(ip).setLocation(region).setMethod(method).setUrl(url).setTraceId(TraceUtil.getTraceId(request)).setType("2").setTitle(logAnn.value()).setException(ThrowableUtil.getStackTrace(e));
    // 设置MDC
    TraceUtil.mdcTraceId(TraceUtil.getTraceId(request));
    // 发布事件
    applicationContext.publishEvent(new LogEvent(commonLog));
    log.info("Error Result: {}", commonLog);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Log(vip.mate.core.log.annotation.Log) CommonLog(vip.mate.core.common.dto.CommonLog) LogEvent(vip.mate.core.log.event.LogEvent) CommonLog(vip.mate.core.common.dto.CommonLog) Method(java.lang.reflect.Method) AfterThrowing(org.aspectj.lang.annotation.AfterThrowing)

Example 9 with Log

use of vip.mate.core.log.annotation.Log in project matecloud by matevip.

the class LogAspect method recordLog.

/**
 * 配置环绕通知,使用在方法logPointcut()上注册的切入点
 * @param point
 * @return
 * @throws Throwable
 */
@Around("pointcut()")
public Object recordLog(ProceedingJoinPoint point) throws Throwable {
    Object result = new Object();
    //  获取request
    HttpServletRequest request = RequestHolder.getHttpServletRequest();
    // 判断为空则直接跳过执行
    if (ObjectUtils.isEmpty(request)) {
        return point.proceed();
    }
    //  获取注解里的value值
    Method targetMethod = resolveMethod(point);
    Log logAnn = targetMethod.getAnnotation(Log.class);
    // 打印执行时间
    long startTime = System.nanoTime();
    // 请求方法
    String method = request.getMethod();
    String url = request.getRequestURI();
    // 获取IP和地区
    String ip = RequestHolder.getHttpServletRequestIpAddress();
    String region = IPUtil.getCityInfo(ip);
    // 获取请求参数
    // Map<String, Object> paramMap = logIngArgs(point);
    // 参数
    Object[] args = point.getArgs();
    String requestParam = getArgs(args, request);
    // 计算耗时
    long tookTime = 0L;
    try {
        result = point.proceed();
    } finally {
        tookTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
    }
    //  如果是登录请求,则不获取用户信息
    String userName = null;
    if (!url.contains("oauth") && !(url.contains("code"))) {
        // 判断header是否存在,存在则获取用户名
        if (StringUtil.isNotBlank(SecurityUtil.getHeaderToken(request))) {
            userName = SecurityUtil.getUsername(request).getAccount();
        }
    }
    //  封装SysLog
    CommonLog commonLog = new CommonLog();
    commonLog.setIp(ip).setCreateBy(userName).setMethod(method).setUrl(url).setOperation(String.valueOf(result)).setLocation(StringUtils.isEmpty(region) ? "本地" : region).setTraceId(request.getHeader(MateConstant.MATE_TRACE_ID)).setExecuteTime(tookTime).setTitle(logAnn.value()).setParams(JSON.toJSONString(requestParam));
    log.info("Http Request: {}", JSONObject.toJSONString(commonLog));
    // 发布事件
    applicationContext.publishEvent(new LogEvent(commonLog));
    return result;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Log(vip.mate.core.log.annotation.Log) CommonLog(vip.mate.core.common.dto.CommonLog) LogEvent(vip.mate.core.log.event.LogEvent) JSONObject(com.alibaba.fastjson.JSONObject) Method(java.lang.reflect.Method) CommonLog(vip.mate.core.common.dto.CommonLog) Around(org.aspectj.lang.annotation.Around)

Example 10 with Log

use of vip.mate.core.log.annotation.Log in project matecloud by matevip.

the class SysCodeController method tableList.

/**
 * 数据库表信息
 * @param dataSourceId 数据源Id
 * @return Result
 */
@PreAuth
@Log(value = "数据库表信息", exception = "数据库表信息请求异常")
@ApiOperation(value = "数据库表信息", notes = "数据库表信息")
@PostMapping("/table-list")
public Result<List<TableInfo>> tableList(@RequestParam String dataSourceId) {
    SysDataSource sysDataSource = sysDataSourceService.getById(dataSourceId);
    GlobalConfig gc = new GlobalConfig();
    DataSourceConfig dsc = new DataSourceConfig();
    dsc.setDbType(DbType.getDbType(sysDataSource.getDbType()));
    dsc.setDriverName(sysDataSource.getDriverClass());
    dsc.setUrl(sysDataSource.getUrl());
    dsc.setUsername(sysDataSource.getUsername());
    dsc.setPassword(sysDataSource.getPassword());
    StrategyConfig strategyConfig = new StrategyConfig();
    TemplateConfig templateConfig = new TemplateConfig();
    ConfigBuilder config = new ConfigBuilder(new PackageConfig(), dsc, strategyConfig, templateConfig, gc);
    List<TableInfo> list = config.getTableInfoList();
    return Result.data(list);
}
Also used : ConfigBuilder(com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder) SysDataSource(vip.mate.code.entity.SysDataSource) TableInfo(com.baomidou.mybatisplus.generator.config.po.TableInfo) PreAuth(vip.mate.core.auth.annotation.PreAuth) PostMapping(org.springframework.web.bind.annotation.PostMapping) Log(vip.mate.core.log.annotation.Log) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

Log (vip.mate.core.log.annotation.Log)13 ApiOperation (io.swagger.annotations.ApiOperation)11 PreAuth (vip.mate.core.auth.annotation.PreAuth)8 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)4 Collection (java.util.Collection)3 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)2 Method (java.lang.reflect.Method)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 SysDataSource (vip.mate.code.entity.SysDataSource)2 CommonLog (vip.mate.core.common.dto.CommonLog)2 LogEvent (vip.mate.core.log.event.LogEvent)2 BlackList (vip.mate.core.rule.entity.BlackList)2 SysBlacklist (vip.mate.system.entity.SysBlacklist)2 JSONObject (com.alibaba.fastjson.JSONObject)1 ConfigBuilder (com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder)1 TableInfo (com.baomidou.mybatisplus.generator.config.po.TableInfo)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1