This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 34aeadeac [type:feature] admin support print log (#3670)
34aeadeac is described below
commit 34aeadeac61f9359bd25426873eff38c35352358
Author: likeguo <[email protected]>
AuthorDate: Wed Jul 6 22:08:24 2022 +0800
[type:feature] admin support print log (#3670)
* a little modification
* Rollback
* rollback
* [refactor: admin]Use static access with
"com.github.pagehelper.page.PageMethod" (#2900)
* fix : misspelled token
* perf : Use static access with "com.github.pagehelper.page.PageMethod" for
"startPage".
Co-authored-by: zhangjiangnan8 <[email protected]>
* [refactor: admin]Use static access with
"com.github.pagehelper.page.PageMethod" (#2900)
* fix : misspelled token
* perf : Use static access with "com.github.pagehelper.page.PageMethod" for
"startPage".
Co-authored-by: zhangjiangnan8 <[email protected]>
* [refactor: admin]Use static access with
"com.github.pagehelper.page.PageMethod" (#2900)
* fix : misspelled token
* perf : Use static access with "com.github.pagehelper.page.PageMethod" for
"startPage".
Co-authored-by: zhangjiangnan8 <[email protected]>
* [refactor: admin]Use static access with
"com.github.pagehelper.page.PageMethod" (#2900)
* fix : misspelled token
* perf : Use static access with "com.github.pagehelper.page.PageMethod" for
"startPage".
Co-authored-by: zhangjiangnan8 <[email protected]>
* feature/print api log & record log support user isolation
* feature/print api log & record log support user isolation
* feature/print api log & record log support user isolation
* feature/print api log & record log support user isolation
* feature/print api log & record log support user isolation
Co-authored-by: likeguo <[email protected]>
Co-authored-by: 190coder <[email protected]>
Co-authored-by: zhangjiangnan8 <[email protected]>
---
.../shenyu/admin/aspect/PrintApiLogAspect.java | 86 ++++++++++++++++++++++
.../config/properties/DashboardProperties.java | 26 +++++++
.../admin/mapper/OperationRecordLogMapper.java | 6 +-
.../apache/shenyu/admin/mapper/PluginMapper.java | 3 +-
.../admin/model/query/RecordLogQueryCondition.java | 25 +++++++
.../impl/OperationRecordLogServiceImpl.java | 10 ++-
.../admin/service/impl/PluginServiceImpl.java | 3 +-
.../org/apache/shenyu/admin/utils/SessionUtil.java | 9 ++-
.../mappers/operation-record-log-sqlmap.xml | 11 +++
.../src/main/resources/mappers/plugin-sqlmap.xml | 24 ++++++
10 files changed, 196 insertions(+), 7 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/aspect/PrintApiLogAspect.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/aspect/PrintApiLogAspect.java
new file mode 100644
index 000000000..460aa07e6
--- /dev/null
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/aspect/PrintApiLogAspect.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.aspect;
+
+import org.apache.shenyu.admin.config.properties.DashboardProperties;
+import org.apache.shenyu.admin.utils.SessionUtil;
+import org.apache.shenyu.common.exception.ShenyuException;
+import org.aspectj.lang.ProceedingJoinPoint;
+import org.aspectj.lang.annotation.Around;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+
+/**
+ * print log aspect.
+ */
+@Aspect
+@Component
+public class PrintApiLogAspect {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(PrintApiLogAspect.class);
+
+ private final DashboardProperties properties;
+
+ public PrintApiLogAspect(final DashboardProperties properties) {
+ this.properties = properties;
+ }
+
+ /**
+ * cut.
+ */
+
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
+ public void pointCut() {
+ }
+
+ /**
+ * log around.
+ *
+ * @param point point {@link ProceedingJoinPoint}
+ * @return result {@link Object}
+ */
+ @Around("pointCut()")
+ public Object logAround(final ProceedingJoinPoint point) {
+ final long start = System.currentTimeMillis();
+ try {
+ preLog(point);
+ return point.proceed();
+ } catch (final Throwable throwable) {
+ throw new ShenyuException(throwable);
+ } finally {
+ postLog(point, start);
+ SessionUtil.clean();
+ }
+ }
+
+ private void postLog(final ProceedingJoinPoint point, final long start) {
+ if (Boolean.TRUE.equals(properties.getEnablePrintApiLog())) {
+ LOG.info("{} exec: method [{}.{}] over, time cost: {}",
SessionUtil.visitorName(),
+ point.getTarget().getClass().getSimpleName(),
point.getSignature().getName(), System.currentTimeMillis() - start);
+ }
+ }
+
+ private void preLog(final ProceedingJoinPoint point) {
+ if (Boolean.TRUE.equals(properties.getEnablePrintApiLog())) {
+ LOG.info("{} exec: method [{}.{}]", SessionUtil.visitorName(),
point.getTarget().getClass().getSimpleName(), point.getSignature().getName());
+ }
+ }
+
+}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java
index 4c465970f..9d2e8d084 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/properties/DashboardProperties.java
@@ -42,6 +42,14 @@ public class DashboardProperties {
@Value("${shenyu.dashboard.core.record-log-only-clean-days:3}")
private Integer onlyCleanDays;
+
+ /**
+ * enable print api log..
+ * default is false.
+ */
+ @Value("${shenyu.dashboard.core.enable-print-api-log:false}")
+ private Boolean enablePrintApiLog;
+
/**
* get recordLogLimit.
*
@@ -77,4 +85,22 @@ public class DashboardProperties {
public void setOnlyCleanDays(final Integer onlyCleanDays) {
this.onlyCleanDays = onlyCleanDays;
}
+
+ /**
+ * get enablePrintApiLog.
+ *
+ * @return enablePrintApiLog
+ */
+ public Boolean getEnablePrintApiLog() {
+ return enablePrintApiLog;
+ }
+
+ /**
+ * set enablePrintApiLog.
+ *
+ * @param enablePrintApiLog enablePrintApiLog
+ */
+ public void setEnablePrintApiLog(final Boolean enablePrintApiLog) {
+ this.enablePrintApiLog = enablePrintApiLog;
+ }
}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/OperationRecordLogMapper.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/OperationRecordLogMapper.java
index dd449349f..7a0b59a03 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/OperationRecordLogMapper.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/OperationRecordLogMapper.java
@@ -21,6 +21,7 @@ import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.shenyu.admin.model.entity.OperationRecordLog;
import org.apache.shenyu.admin.model.query.RecordLogQueryCondition;
+import org.springframework.lang.Nullable;
import java.util.Date;
import java.util.List;
@@ -34,10 +35,11 @@ public interface OperationRecordLogMapper {
/**
* select limit.
*
- * @param limit limit
+ * @param username username
+ * @param limit limit
* @return list
*/
- List<OperationRecordLog> selectLimit(@Param("limit") Integer limit);
+ List<OperationRecordLog> selectLimit(@Nullable @Param("username") String
username, @Param("limit") Integer limit);
/**
* insert.
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/PluginMapper.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/PluginMapper.java
index eb06f2383..a91e37e28 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/PluginMapper.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/PluginMapper.java
@@ -192,9 +192,10 @@ public interface PluginMapper extends ExistProvider {
/**
* active plugin snapshot.
*
+ * @param userId user Id
* @return plugin list
*/
- List<PluginSnapshotVO> activePluginSnapshot();
+ List<PluginSnapshotVO> activePluginSnapshot(@Param("userId")String userId);
/**
* search by condition.
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/RecordLogQueryCondition.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/RecordLogQueryCondition.java
index 1e1c64c75..42dff9c6f 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/RecordLogQueryCondition.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/query/RecordLogQueryCondition.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.admin.model.query;
import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonIgnore;
import
org.apache.shenyu.admin.model.page.condition.BaseExcludedSearchCondition;
import org.apache.shenyu.admin.model.page.condition.SearchCondition;
import org.apache.shenyu.common.utils.DateUtils;
@@ -55,6 +56,12 @@ public class RecordLogQueryCondition extends
BaseExcludedSearchCondition impleme
@JsonFormat(pattern = DateUtils.DATE_FORMAT_DATETIME)
private Date endTime;
+ /**
+ * username.
+ */
+ @JsonIgnore
+ private String username;
+
@Override
public void setKeyword(final String keyword) {
this.keyword = keyword;
@@ -119,4 +126,22 @@ public class RecordLogQueryCondition extends
BaseExcludedSearchCondition impleme
public void setType(final String type) {
this.type = type;
}
+
+ /**
+ * get username.
+ *
+ * @return username
+ */
+ public String getUsername() {
+ return username;
+ }
+
+ /**
+ * set username.
+ *
+ * @param username username
+ */
+ public void setUsername(final String username) {
+ this.username = username;
+ }
}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java
index 376a5bff0..7c5de5855 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/OperationRecordLogServiceImpl.java
@@ -23,6 +23,8 @@ import
org.apache.shenyu.admin.model.entity.OperationRecordLog;
import org.apache.shenyu.admin.model.query.RecordLogQueryCondition;
import org.apache.shenyu.admin.service.OperationRecordLogService;
import org.apache.shenyu.admin.utils.Assert;
+import org.apache.shenyu.admin.utils.SessionUtil;
+import org.apache.shenyu.common.constant.AdminConstants;
import org.springframework.stereotype.Service;
import java.util.Date;
@@ -48,6 +50,9 @@ public class OperationRecordLogServiceImpl implements
OperationRecordLogService
public void doConditionPreProcessing(final RecordLogQueryCondition
condition) {
condition.init();
Assert.isTrue(condition.getEndTime().getTime() >
condition.getStartTime().getTime(), "end time must be greater than start time");
+ if (!AdminConstants.ADMIN_NAME.equals(SessionUtil.visitorName())) {
+ condition.setUsername(SessionUtil.visitorName());
+ }
}
@Override
@@ -57,7 +62,10 @@ public class OperationRecordLogServiceImpl implements
OperationRecordLogService
@Override
public List<OperationRecordLog> list() {
- return
recordLogMapper.selectLimit(dashboardProperties.getRecordLogLimit());
+ if (AdminConstants.ADMIN_NAME.equals(SessionUtil.visitorName())) {
+ return recordLogMapper.selectLimit(null,
dashboardProperties.getRecordLogLimit());
+ }
+ return recordLogMapper.selectLimit(SessionUtil.visitorName(),
dashboardProperties.getRecordLogLimit());
}
@Override
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java
index f28cd91fb..d2f058089 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java
@@ -35,6 +35,7 @@ import
org.apache.shenyu.admin.service.publish.PluginEventPublisher;
import org.apache.shenyu.admin.transfer.PluginTransfer;
import org.apache.shenyu.admin.utils.Assert;
import org.apache.shenyu.admin.utils.ListUtil;
+import org.apache.shenyu.admin.utils.SessionUtil;
import org.apache.shenyu.admin.utils.ShenyuResultMessage;
import org.apache.shenyu.common.constant.AdminConstants;
import org.apache.shenyu.common.dto.PluginData;
@@ -184,7 +185,7 @@ public class PluginServiceImpl implements PluginService {
@Override
public List<PluginSnapshotVO> activePluginSnapshot() {
- return pluginMapper.activePluginSnapshot();
+ return
pluginMapper.activePluginSnapshot(AdminConstants.ADMIN_NAME.equals(SessionUtil.visitorName())
? null : SessionUtil.visitor().getUserId());
}
/**
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/SessionUtil.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/SessionUtil.java
index 922b09e1c..a9af02a9c 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/SessionUtil.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/SessionUtil.java
@@ -62,7 +62,7 @@ public final class SessionUtil {
} catch (Exception e) {
LOG.warn("get user info error ,not found, used default user ,it
unknown");
}
- return UserInfo.builder().userId("-1").userName("unknown").build();
+ return defaultUser();
}
/**
@@ -79,7 +79,8 @@ public final class SessionUtil {
*/
public static void setLocalVisitorFromAuth() {
// featureToDo:Adapting app access
- LOCAL_VISITOR.set(JwtUtils.getUserInfo());
+ final UserInfo userInfo = JwtUtils.getUserInfo();
+ LOCAL_VISITOR.set(Objects.isNull(userInfo) ? defaultUser() : userInfo);
}
/**
@@ -88,5 +89,9 @@ public final class SessionUtil {
public static void clean() {
LOCAL_VISITOR.remove();
}
+
+ private static UserInfo defaultUser() {
+ return UserInfo.builder().userId("-1").userName("unknown").build();
+ }
}
diff --git
a/shenyu-admin/src/main/resources/mappers/operation-record-log-sqlmap.xml
b/shenyu-admin/src/main/resources/mappers/operation-record-log-sqlmap.xml
index c93046a18..6617839b3 100644
--- a/shenyu-admin/src/main/resources/mappers/operation-record-log-sqlmap.xml
+++ b/shenyu-admin/src/main/resources/mappers/operation-record-log-sqlmap.xml
@@ -47,6 +47,11 @@
SELECT
<include refid="Base_Column_List"/>
FROM operation_record_log
+ <where>
+ <if test="username != null">
+ operator = #{username}
+ </if>
+ </where>
order by operation_time desc
limit #{limit}
</select>
@@ -56,6 +61,9 @@
<include refid="Base_Column_List"/>
FROM operation_record_log
where rownum <![CDATA[<=]]> #{limit}
+ <if test="username != null">
+ and operator = #{username}
+ </if>
order by operation_time desc
</select>
@@ -73,6 +81,9 @@
<if test="condition.type != null">
and operation_type = #{condition.type}
</if>
+ <if test="condition.username != null">
+ and operator = #{condition.username}
+ </if>
order by operation_time desc
</select>
diff --git a/shenyu-admin/src/main/resources/mappers/plugin-sqlmap.xml
b/shenyu-admin/src/main/resources/mappers/plugin-sqlmap.xml
index 9f65d75bf..ae98bf380 100644
--- a/shenyu-admin/src/main/resources/mappers/plugin-sqlmap.xml
+++ b/shenyu-admin/src/main/resources/mappers/plugin-sqlmap.xml
@@ -155,6 +155,18 @@
left join plugin_handle ph on p.id = ph.plugin_id
left join selector s on p.id = s.plugin_id
where p.enabled = true
+ <if test="userId != null">
+ and p.name in (
+ select r.name
+ from resource r
+ inner join permission
+ on permission.resource_id = r.id
+ where parent_id = '1346775491550474240'
+ and object_id = (select role_id
+ from user_role
+ where user_id = #{userId})
+ )
+ </if>
group by p.id, p.name, config, p.role, p.sort
order by p.sort, p.id
</select>
@@ -170,6 +182,18 @@
left join plugin_handle ph on p.id = ph.plugin_id
left join selector s on p.id = s.plugin_id
where p.enabled = 1
+ <if test="userId != null">
+ and p.name in (
+ select r.name
+ from resource r
+ inner join permission
+ on permission.resource_id = r.id
+ where parent_id = '1346775491550474240'
+ and object_id = (select role_id
+ from user_role
+ where user_id = #{userId})
+ )
+ </if>
group by p.id, p.name, to_char(config), p.role, p.sort
order by p.sort, p.id
</select>