Skip to content

Commit

Permalink
增加用户访问日志记录拦截器
Browse files Browse the repository at this point in the history
  • Loading branch information
penggle committed May 20, 2017
1 parent f018443 commit f3eb4d2
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.certusnet.xproject.admin.web.LoginToken;
import com.certusnet.xproject.common.consts.GlobalConstants;
import com.certusnet.xproject.common.support.AbstractXTreeBuilder;
import com.certusnet.xproject.common.support.HttpAccessLogging;
import com.certusnet.xproject.common.support.TreeNodeConverter;
import com.certusnet.xproject.common.util.DateTimeUtils;
import com.certusnet.xproject.common.web.BaseController;
Expand Down Expand Up @@ -53,6 +54,7 @@ public class AdminResourceMgtController extends BaseController {
* @return
*/
@RequestMapping(value="/admin/resource/available", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/资源管理/查看资源树结构")
public Object getAvailableResources(HttpServletRequest request, HttpServletResponse response) {
List<AdminResource> allResourceList = adminResourceService.getAllResourceList(null);
List<Map<String,Object>> dataList = resourceTreeBuilder.buildObjectTree(GlobalConstants.DEFAULT_ADMIN_ROOT_RESOURCE_ID, allResourceList, resourceTreeNodeConverter);
Expand All @@ -67,8 +69,10 @@ public Object getAvailableResources(HttpServletRequest request, HttpServletRespo
* @return
*/
@RequestMapping(value="/admin/resource/add/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/资源管理/新增资源")
public Object addResource(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminResource resourceAddForm) {
LoginToken<AdminUser> loginToken = ShiroUtils.getSessionAttribute(LoginToken.LOGIN_TOKEN_SESSION_KEY);
resourceAddForm.setResourceId(null);
resourceAddForm.setCreateTime(DateTimeUtils.formatNow());
resourceAddForm.setCreateBy(loginToken.getLoginId());
resourceAddForm.setResourceType(AdminResourceTypeEnum.ADMIN_RESOURCE_TYPE_NORMAL.getTypeCode());
Expand All @@ -84,6 +88,7 @@ public Object addResource(HttpServletRequest request, HttpServletResponse respon
* @return
*/
@RequestMapping(value="/admin/resource/edit/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/资源管理/修改资源")
public Object editResource(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminResource resourceEditForm) {
LoginToken<AdminUser> loginToken = ShiroUtils.getSessionAttribute(LoginToken.LOGIN_TOKEN_SESSION_KEY);
resourceEditForm.setUpdateTime(DateTimeUtils.formatNow());
Expand All @@ -100,6 +105,7 @@ public Object editResource(HttpServletRequest request, HttpServletResponse respo
* @return
*/
@RequestMapping(value="/admin/resource/del", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/资源管理/删除资源")
public Object delResource(HttpServletRequest request, HttpServletResponse response, Long id) {
adminResourceService.deleteResourceById(id, true);
return genSuccessResult("删除成功!", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.certusnet.xproject.admin.web.LoginToken;
import com.certusnet.xproject.common.consts.GlobalConstants;
import com.certusnet.xproject.common.support.AbstractXTreeBuilder;
import com.certusnet.xproject.common.support.HttpAccessLogging;
import com.certusnet.xproject.common.support.OrderBy;
import com.certusnet.xproject.common.support.Pager;
import com.certusnet.xproject.common.support.PagingList;
Expand Down Expand Up @@ -69,6 +70,7 @@ public class AdminRoleMgtController extends BaseController {
* @return
*/
@RequestMapping(value="/admin/role/list", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/角色管理/查询角色列表")
public Object listRole(HttpServletRequest request, HttpServletResponse response, AdminRole roleQueryForm, OrderBy orderBy, Pager pager) {
PagingList<AdminRole> roleList = adminRoleService.getRoleList(roleQueryForm, pager, orderBy);
return genSuccessPagingResult(roleList);
Expand All @@ -82,8 +84,10 @@ public Object listRole(HttpServletRequest request, HttpServletResponse response,
* @return
*/
@RequestMapping(value="/admin/role/add/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/角色管理/新增角色")
public Object addRole(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminRole roleAddForm) {
LoginToken<AdminUser> loginToken = ShiroUtils.getSessionAttribute(LoginToken.LOGIN_TOKEN_SESSION_KEY);
roleAddForm.setRoleId(null);
roleAddForm.setCreateTime(DateTimeUtils.formatNow());
roleAddForm.setCreateBy(loginToken.getLoginId());
roleAddForm.setRoleType(AdminRoleTypeEnum.ADMIN_ROLE_TYPE_NORMAL.getTypeCode());
Expand All @@ -99,6 +103,7 @@ public Object addRole(HttpServletRequest request, HttpServletResponse response,
* @return
*/
@RequestMapping(value="/admin/role/edit/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/角色管理/修改角色")
public Object editRole(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminRole roleEditForm) {
LoginToken<AdminUser> loginToken = ShiroUtils.getSessionAttribute(LoginToken.LOGIN_TOKEN_SESSION_KEY);
roleEditForm.setUpdateTime(DateTimeUtils.formatNow());
Expand All @@ -115,6 +120,7 @@ public Object editRole(HttpServletRequest request, HttpServletResponse response,
* @return
*/
@RequestMapping(value="/admin/role/del", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/角色管理/删除角色")
public Object delRole(HttpServletRequest request, HttpServletResponse response, Long id) {
adminRoleService.deleteRoleById(id);
return genSuccessResult("删除成功!", null);
Expand Down Expand Up @@ -153,6 +159,7 @@ public Object loadRoleResources(HttpServletRequest request, HttpServletResponse
* @return
*/
@RequestMapping(value="/admin/role/config/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/角色管理/配置角色资源关系")
public Object configRoleResources(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String,Object> parameter) {
List<Long> resourceIdList = new ArrayList<Long>();
String resourceIds = MapUtils.getString(parameter, "resourceIds");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.certusnet.xproject.admin.service.AdminUserService;
import com.certusnet.xproject.admin.web.LoginToken;
import com.certusnet.xproject.common.consts.GlobalConstants;
import com.certusnet.xproject.common.support.HttpAccessLogging;
import com.certusnet.xproject.common.support.OrderBy;
import com.certusnet.xproject.common.support.Pager;
import com.certusnet.xproject.common.support.PagingList;
Expand Down Expand Up @@ -66,6 +67,7 @@ public class AdminUserMgtController extends BaseController {
* @return
*/
@RequestMapping(value="/admin/user/list", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/查询用户列表")
public Object listRole(HttpServletRequest request, HttpServletResponse response, AdminUser userQueryForm, OrderBy orderBy, Pager pager) {
PagingList<AdminUser> dataList = adminUserService.getUserList(userQueryForm, pager, orderBy);
return genSuccessPagingResult(dataList);
Expand All @@ -79,8 +81,10 @@ public Object listRole(HttpServletRequest request, HttpServletResponse response,
* @return
*/
@RequestMapping(value="/admin/user/add/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/新增用户")
public Object addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminUser userAddForm) throws Exception {
LoginToken<AdminUser> loginToken = ShiroUtils.getSessionAttribute(LoginToken.LOGIN_TOKEN_SESSION_KEY);
userAddForm.setUserId(null);
userAddForm.setCreateTime(DateTimeUtils.formatNow());
userAddForm.setCreateBy(loginToken.getLoginId());
userAddForm.setStatus(AdminUserStatusEnum.ADMIN_USER_STATUS_ENABLED.getStatusCode());
Expand Down Expand Up @@ -111,6 +115,7 @@ public Object addUser(HttpServletRequest request, HttpServletResponse response,
* @return
*/
@RequestMapping(value="/admin/user/edit/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/修改用户")
public Object editUser(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminUser userEditForm) throws Exception {
LoginToken<AdminUser> loginToken = ShiroUtils.getSessionAttribute(LoginToken.LOGIN_TOKEN_SESSION_KEY);
userEditForm.setUpdateBy(loginToken.getLoginId());
Expand Down Expand Up @@ -140,6 +145,7 @@ public Object editUser(HttpServletRequest request, HttpServletResponse response,
* @return
*/
@RequestMapping(value="/admin/user/del", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/删除用户")
public Object delUser(HttpServletRequest request, HttpServletResponse response, Long id) {
AdminUser user = new AdminUser();
user.setUserId(id);
Expand All @@ -148,13 +154,14 @@ public Object delUser(HttpServletRequest request, HttpServletResponse response,
}

/**
* 修改密码
* 修改用户密码
* @param request
* @param response
* @param passwdEditForm
* @return
*/
@RequestMapping(value="/admin/user/changepwd/submit", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/修改用户密码", excludeParamNames={"password","repassword"})
public Object changeUserPasswd(HttpServletRequest request, HttpServletResponse response, @RequestBody AdminUser passwdEditForm, Boolean forceUpdate) {
if(forceUpdate == null){
forceUpdate = false;
Expand All @@ -171,6 +178,7 @@ public Object changeUserPasswd(HttpServletRequest request, HttpServletResponse r
* @return
*/
@RequestMapping(value="/admin/user/enable", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/启用用户")
public Object enableUser(HttpServletRequest request, HttpServletResponse response, Long userId) {
return updateUserStatus(request, response, userId, AdminUserStatusEnum.ADMIN_USER_STATUS_ENABLED);
}
Expand All @@ -183,6 +191,7 @@ public Object enableUser(HttpServletRequest request, HttpServletResponse respons
* @return
*/
@RequestMapping(value="/admin/user/disable", method=GET, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/禁用用户")
public Object disableUser(HttpServletRequest request, HttpServletResponse response, Long userId) {
return updateUserStatus(request, response, userId, AdminUserStatusEnum.ADMIN_USER_STATUS_DISABLED);
}
Expand Down Expand Up @@ -217,6 +226,7 @@ public Object loadUserRoles(HttpServletRequest request, HttpServletResponse resp
* @return
*/
@RequestMapping(value="/admin/user/config/add", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/添加用户角色配置")
public Object addUserRoles(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String,Object> parameter) {
Long userId = MapUtils.getLong(parameter, "userId");
String roleIds = MapUtils.getString(parameter, "roleIds");
Expand Down Expand Up @@ -246,6 +256,7 @@ public Object addUserRoles(HttpServletRequest request, HttpServletResponse respo
* @return
*/
@RequestMapping(value="/admin/user/config/del", method=POST, consumes=APPLICATION_JSON, produces=APPLICATION_JSON)
@HttpAccessLogging(title="系统管理/用户管理/删除用户角色配置")
public Object delUserRoles(HttpServletRequest request, HttpServletResponse response, @RequestBody Map<String,Object> parameter) {
Long userId = MapUtils.getLong(parameter, "userId");
String roleIds = MapUtils.getString(parameter, "roleIds");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.certusnet.xproject.admin.web.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;

import com.certusnet.xproject.admin.model.AdminUser;
import com.certusnet.xproject.admin.model.AdminUserAccessLog;
Expand All @@ -9,32 +11,29 @@
import com.certusnet.xproject.common.util.CollectionUtils;
import com.certusnet.xproject.common.util.DateTimeUtils;
import com.certusnet.xproject.common.util.JsonUtils;
import com.certusnet.xproject.common.util.SpringUtils;
import com.certusnet.xproject.common.web.springmvc.interceptor.AbstractHttpAccessLogHandler;
import com.certusnet.xproject.common.web.springmvc.interceptor.HttpAccessLog;
import com.certusnet.xproject.common.web.springmvc.interceptor.HttpAccessLogDAO;

public class DbStoreHttpAccessLogHandler extends AbstractHttpAccessLogHandler<AdminUser> {
@Component
public class DbStoreHttpAccessLogDAO implements HttpAccessLogDAO {

private final AdminUserAccessLogService adminUserAccessLogService;
@Autowired
private AdminUserAccessLogService adminUserAccessLogService;

public DbStoreHttpAccessLogHandler(HttpAccessLog<AdminUser> httpAccessLog) {
super(httpAccessLog);
this.adminUserAccessLogService = SpringUtils.getBean(AdminUserAccessLogService.class);
}

public LoggingType getLoggingType() {
return LoggingType.DB;
}

public void handleLogger(HttpAccessLog<AdminUser> httpAccessLog) throws Exception {
public void saveLog(HttpAccessLog<?> httpAccessLog) throws Exception {
AdminUser adminUser = (AdminUser) httpAccessLog.getAccessUser();
AdminUserAccessLog accessLog = new AdminUserAccessLog();
accessLog.setTitle(httpAccessLog.getTitle());
accessLog.setUri(httpAccessLog.getUri());
accessLog.setMethod(httpAccessLog.getMethod());
accessLog.setRequestHeader(CollectionUtils.isEmpty(httpAccessLog.getRequestHeader()) ? null : JsonUtils.object2Json(httpAccessLog.getRequestHeader()));
accessLog.setRequestContentType(httpAccessLog.getRequestContentType() == null ? null : httpAccessLog.getRequestContentType().toString());
accessLog.setRequestParameter(JsonUtils.object2Json(httpAccessLog.getRequestParameter()));
accessLog.setAccessUserId(httpAccessLog.getAccessUser().getUserId());
accessLog.setAccessUserId(adminUser.getUserId());
accessLog.setAccessTime(httpAccessLog.getAccessTime());
accessLog.setClientIpAddr(httpAccessLog.getClientIpAddr());
accessLog.setServerIpAddr(httpAccessLog.getServerIpAddr());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
import com.certusnet.xproject.admin.model.AdminUser;
import com.certusnet.xproject.admin.service.AdminUserService;
import com.certusnet.xproject.admin.web.LoginToken;
import com.certusnet.xproject.common.support.HttpAccessLogging.LoggingType;
import com.certusnet.xproject.common.web.shiro.ShiroUtils;
import com.certusnet.xproject.common.web.springmvc.interceptor.AbstractHttpAccessLogHandler;
import com.certusnet.xproject.common.web.springmvc.interceptor.AbstractHttpAccessLoggingInterceptor;
import com.certusnet.xproject.common.web.springmvc.interceptor.HttpAccessLog;
import com.certusnet.xproject.common.web.springmvc.interceptor.HttpAccessLog.HttpRequestParameter;

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -51,14 +48,4 @@ protected AdminUser getAccessUser(HttpServletRequest request, LoggingContext log
}
}

protected AbstractHttpAccessLogHandler<AdminUser> createHttpAccessLoggerHandler(LoggingContext loggingContext) {
LoggingType loggingType = loggingContext.getHttpAccessLogging().loggingType();
if(LoggingType.DB.equals(loggingType)){
return new DbStoreHttpAccessLogHandler((HttpAccessLog<AdminUser>) loggingContext.getHttpAccessLog());
}else if(LoggingType.FILE.equals(loggingType)){
return new FileStoreHttpAccessLogHandler((HttpAccessLog<AdminUser>) loggingContext.getHttpAccessLog());
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,35 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.certusnet.xproject.admin.model.AdminUser;
import com.certusnet.xproject.admin.model.AdminUserAccessLog;
import com.certusnet.xproject.common.support.HttpAccessLogging.LoggingType;
import com.certusnet.xproject.common.util.CollectionUtils;
import com.certusnet.xproject.common.util.DateTimeUtils;
import com.certusnet.xproject.common.util.JsonUtils;
import com.certusnet.xproject.common.web.springmvc.interceptor.AbstractHttpAccessLogHandler;
import com.certusnet.xproject.common.web.springmvc.interceptor.HttpAccessLog;
import com.certusnet.xproject.common.web.springmvc.interceptor.HttpAccessLogDAO;

public class FileStoreHttpAccessLogHandler extends AbstractHttpAccessLogHandler<AdminUser> {
@Component
public class FileStoreHttpAccessLogDAO implements HttpAccessLogDAO {

private static final Logger logger = LoggerFactory.getLogger(FileStoreHttpAccessLogHandler.class);
private static final Logger logger = LoggerFactory.getLogger(FileStoreHttpAccessLogDAO.class);

public FileStoreHttpAccessLogHandler(HttpAccessLog<AdminUser> httpAccessLog) {
super(httpAccessLog);
}

public LoggingType getLoggingType() {
return LoggingType.DB;
}

public void handleLogger(HttpAccessLog<AdminUser> httpAccessLog) throws Exception {
public void saveLog(HttpAccessLog<?> httpAccessLog) throws Exception {
AdminUser adminUser = (AdminUser) httpAccessLog.getAccessUser();
AdminUserAccessLog accessLog = new AdminUserAccessLog();
accessLog.setTitle(httpAccessLog.getTitle());
accessLog.setUri(httpAccessLog.getUri());
accessLog.setMethod(httpAccessLog.getMethod());
accessLog.setRequestHeader(CollectionUtils.isEmpty(httpAccessLog.getRequestHeader()) ? null : JsonUtils.object2Json(httpAccessLog.getRequestHeader()));
accessLog.setRequestParameter(JsonUtils.object2Json(httpAccessLog.getRequestParameter()));
accessLog.setAccessUserId(httpAccessLog.getAccessUser().getUserId());
accessLog.setAccessUserId(adminUser.getUserId());
accessLog.setAccessTime(httpAccessLog.getAccessTime());
accessLog.setClientIpAddr(httpAccessLog.getClientIpAddr());
accessLog.setServerIpAddr(httpAccessLog.getServerIpAddr());
Expand Down
2 changes: 1 addition & 1 deletion xproject-admin/src/main/webapp/admin/role/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ <h3 class="el-title">
</div>
<div slot="footer">
<el-button icon="fa-ban" @click="viewConfigDialogVisible = false">关 闭</el-button>
<el-button v-if="currentActionType == 'conf'" type="primary" icon="fa-save" @click="saveRoleResourceConfig" :loading="submiting">保存配置</el-button>
<el-button v-if="currentActionType == 'conf' && viewConfigActiveTabName == 'roleResourceConfig'" type="primary" icon="fa-save" @click="saveRoleResourceConfig" :loading="submiting">保存配置</el-button>
</div>
</el-dialog>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,8 @@ Vue.onDocumentReady(function() {
closeEditDialog: function(){
this.editDialogVisible = false;
this.$refs.resourceEditForm.resetFields();
this.resourceEditForm.resourceId = '';
this.resourceEditForm.parentResourceId = '';
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ Vue.onDocumentReady(function() {
closeEditDialog: function(){
this.editDialogVisible = false;
this.$refs.roleEditForm.resetFields();
this.roleEditForm.roleId = ''; //手动reset
},
loadRoleResourceDataList: function(loading, roleId){
var _this = this;
Expand Down Expand Up @@ -353,10 +354,12 @@ Vue.onDocumentReady(function() {
]);
},
onRoleResourceTreeNodeCheckClick: function(data, node){
if(node.checked){
this.recursiveCheckParent(node, node.checked); //当前节点被选中时则选中其所有父节点
}else{
this.recursiveCheckChild(node, node.checked); //当前节点被取消选中时则取消选中其所有子节点
if(this.currentActionType == 'conf'){
if(!node.checked){
this.recursiveCheckParent(node, !node.checked); //当前节点被选中时则选中其所有父节点
}else{
this.recursiveCheckChild(node, !node.checked); //当前节点被取消选中时则取消选中其所有子节点
}
}
},
recursiveCheckParent: function(node, checked){
Expand Down
Loading

0 comments on commit f3eb4d2

Please sign in to comment.