Commit 98d89338 authored by Quxl's avatar Quxl

操作日志

parent 0c33d873
package com.egolm.film.api; package com.egolm.film.api;
import javax.servlet.http.HttpServletRequest;
import com.egolm.film.model.LoginToken; import com.egolm.film.model.LoginToken;
public interface TokenService { public interface TokenService {
...@@ -15,4 +17,6 @@ public interface TokenService { ...@@ -15,4 +17,6 @@ public interface TokenService {
boolean isLogin(); boolean isLogin();
boolean isLoginByToken(String token); boolean isLoginByToken(String token);
void log(HttpServletRequest request);
} }
package com.egolm.film.api.admin; package com.egolm.film.api.admin;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -8,7 +10,6 @@ import org.springframework.web.bind.annotation.ResponseBody; ...@@ -8,7 +10,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.egolm.common.bean.Rjx; import com.egolm.common.bean.Rjx;
import com.egolm.film.api.admin.service.AdminTokenService; import com.egolm.film.api.admin.service.AdminTokenService;
import com.egolm.film.api.common.service.Messages;
import com.egolm.film.config.XException; import com.egolm.film.config.XException;
import com.egolm.film.model.LoginToken; import com.egolm.film.model.LoginToken;
...@@ -23,23 +24,20 @@ public class AdminLoginController { ...@@ -23,23 +24,20 @@ public class AdminLoginController {
@Autowired @Autowired
private AdminTokenService tokenService; private AdminTokenService tokenService;
@Autowired
private Messages messages;
@ResponseBody @ResponseBody
@PostMapping("login") @PostMapping("login")
@ApiOperation("登陆") @ApiOperation("登陆")
public Object login(String username, String password) { public Object login(String username, String password, HttpServletRequest request) {
XException.assertNotBlank(messages.get("err.user_pwd_null"), username, password); XException.assertNotBlank("用户名或密码不能为空", username, password);
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
throw new XException("用户已登录", 200); throw new XException("用户已登录", 200);
} else { } else {
LoginToken token = tokenService.doLogin(username, password); LoginToken token = tokenService.doLogin(username, password);
if(token != null) { if(token != null) {
tokenService.log(request);
return Rjx.jsonOk().setData(token); return Rjx.jsonOk().setData(token);
} else { } else {
throw new XException(messages.get("err.user_or_pwd")); throw new XException("用户名或密码错误");
} }
} }
} }
......
...@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService; ...@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService;
public interface AdminTokenService extends TokenService { public interface AdminTokenService extends TokenService {
final String TOKEN_NAME = "FILM_ADMIN_LOGIN_TOKEN_NAME"; final String TOKEN_NAME = "FILM_ADMIN_LOGIN_TOKEN_NAME";
final String LOG_TYPE = "ADMIN";
} }
package com.egolm.film.api.admin.service.impl; package com.egolm.film.api.admin.service.impl;
import java.util.Date;
import java.util.List; import java.util.List;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.egolm.common.jdbc.JdbcTemplate; import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.web.ServletUtil;
import com.egolm.film.api.admin.service.AdminTokenService; import com.egolm.film.api.admin.service.AdminTokenService;
import com.egolm.film.bean.Fc_admin; import com.egolm.film.bean.Fc_admin;
import com.egolm.film.config.WebMvcConfig; import com.egolm.film.config.WebMvcConfig;
...@@ -95,4 +99,19 @@ public class AdminTokenServiceImpl implements AdminTokenService { ...@@ -95,4 +99,19 @@ public class AdminTokenServiceImpl implements AdminTokenService {
return false; return false;
} }
} }
@Override
public void log(HttpServletRequest request) {
LoginToken token = this.getToken();
String loginid = String.valueOf(token.getId());
Date now = new Date();
String path = request.getRequestURI().replace(request.getContextPath(), "");
String prefix = path.split("/")[1];
String content = JSON.toJSONString(request.getParameterMap());
String ip = ServletUtil.readReqJson(request);
String sql = "insert into fc_logs (type, loginid, prefix, path, ip, content, create_time) values (?, ?, ?, ?, ?, ?, ?)";
Object[] objs = new Object[]{LOG_TYPE, loginid, prefix, path, ip, content, now};
jdbcTemplate.executeUpdate(sql, objs);
}
} }
package com.egolm.film.api.member; package com.egolm.film.api.member;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -34,14 +36,14 @@ public class MemberLoginController { ...@@ -34,14 +36,14 @@ public class MemberLoginController {
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(paramType="header", dataType="string", name="i18n_language") @ApiImplicitParam(paramType="header", dataType="string", name="i18n_language")
}) })
public Object login(String username, String password) { public Object login(String username, String password, HttpServletRequest request) {
XException.assertNotBlank(messages.get("err.user_pwd_null"), username, password); XException.assertNotBlank(messages.get("err.user_pwd_null"), username, password);
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
throw new XException("", 200); throw new XException("", 200);
} else { } else {
LoginToken token = tokenService.doLogin(username, password); LoginToken token = tokenService.doLogin(username, password);
if(token != null) { if(token != null) {
tokenService.log(request);
return Rjx.jsonOk(); return Rjx.jsonOk();
} else { } else {
throw new XException(messages.get("err.user_or_pwd")); throw new XException(messages.get("err.user_or_pwd"));
......
...@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService; ...@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService;
public interface MemberTokenService extends TokenService { public interface MemberTokenService extends TokenService {
final String TOKEN_NAME = "FILM_MEMBER_LOGIN_TOKEN_NAME"; final String TOKEN_NAME = "FILM_MEMBER_LOGIN_TOKEN_NAME";
final String LOG_TYPE = "MEMBER";
} }
package com.egolm.film.api.member.service.impl; package com.egolm.film.api.member.service.impl;
import java.util.Date;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.web.ServletUtil;
import com.egolm.film.api.common.service.Messages; import com.egolm.film.api.common.service.Messages;
import com.egolm.film.api.member.service.MemberService; import com.egolm.film.api.member.service.MemberService;
import com.egolm.film.api.member.service.MemberTokenService; import com.egolm.film.api.member.service.MemberTokenService;
...@@ -24,6 +30,9 @@ public class MemberTokenServiceImpl implements MemberTokenService { ...@@ -24,6 +30,9 @@ public class MemberTokenServiceImpl implements MemberTokenService {
@Autowired @Autowired
private MemberService memberService; private MemberService memberService;
@Autowired
private JdbcTemplate jdbcTemplate;
@Override @Override
public LoginToken doLogin(String email, String password) { public LoginToken doLogin(String email, String password) {
HttpSession session = WebMvcConfig.getSession(); HttpSession session = WebMvcConfig.getSession();
...@@ -94,5 +103,19 @@ public class MemberTokenServiceImpl implements MemberTokenService { ...@@ -94,5 +103,19 @@ public class MemberTokenServiceImpl implements MemberTokenService {
return false; return false;
} }
} }
@Override
public void log(HttpServletRequest request) {
LoginToken token = this.getToken();
String loginid = String.valueOf(token.getId());
Date now = new Date();
String path = request.getRequestURI().replace(request.getContextPath(), "");
String prefix = path.split("/")[1];
String content = JSON.toJSONString(request.getParameterMap());
String ip = ServletUtil.readReqJson(request);
String sql = "insert into fc_logs (type, loginid, prefix, path, ip, content, create_time) values (?, ?, ?, ?, ?, ?, ?)";
Object[] objs = new Object[]{LOG_TYPE, loginid, prefix, path, ip, content, now};
jdbcTemplate.executeUpdate(sql, objs);
}
} }
...@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService; ...@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService;
public interface UserTokenService extends TokenService { public interface UserTokenService extends TokenService {
final String TOKEN_NAME = "FILM_USER_LOGIN_TOKEN_NAME"; final String TOKEN_NAME = "FILM_USER_LOGIN_TOKEN_NAME";
final String LOG_TYPE = "USER";
} }
package com.egolm.film.api.user.service.impl; package com.egolm.film.api.user.service.impl;
import java.util.Date;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.egolm.common.jdbc.JdbcTemplate; import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.web.ServletUtil;
import com.egolm.film.api.common.service.Messages; import com.egolm.film.api.common.service.Messages;
import com.egolm.film.api.user.service.UserTokenService; import com.egolm.film.api.user.service.UserTokenService;
import com.egolm.film.bean.Fc_user; import com.egolm.film.bean.Fc_user;
...@@ -89,4 +94,18 @@ public class UserTokenServiceImpl implements UserTokenService { ...@@ -89,4 +94,18 @@ public class UserTokenServiceImpl implements UserTokenService {
return false; return false;
} }
} }
@Override
public void log(HttpServletRequest request) {
LoginToken token = this.getToken();
String loginid = String.valueOf(token.getId());
Date now = new Date();
String path = request.getRequestURI().replace(request.getContextPath(), "");
String prefix = path.split("/")[1];
String content = JSON.toJSONString(request.getParameterMap());
String ip = ServletUtil.readReqJson(request);
String sql = "insert into fc_logs (type, loginid, prefix, path, ip, content, create_time) values (?, ?, ?, ?, ?, ?, ?)";
Object[] objs = new Object[]{LOG_TYPE, loginid, prefix, path, ip, content, now};
jdbcTemplate.executeUpdate(sql, objs);
}
} }
...@@ -18,13 +18,10 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -18,13 +18,10 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter {
@Autowired @Autowired
private AdminTokenService tokenService; private AdminTokenService tokenService;
@Autowired
private LogsUtil logUtil;
@Override @Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception { public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
logUtil.log(tokenService, req); tokenService.log(req);
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} else { } else {
String sessionid = null; String sessionid = null;
...@@ -35,7 +32,7 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -35,7 +32,7 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter {
} }
} }
if(sessionid != null && tokenService.isLoginByToken(sessionid)) { if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
logUtil.log(tokenService, req); tokenService.log(req);
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} }
throw new XException("用户未登陆", 300); throw new XException("用户未登陆", 300);
......
package com.egolm.film.config.interceptor;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.web.ServletUtil;
import com.egolm.film.api.TokenService;
import com.egolm.film.api.admin.service.AdminTokenService;
import com.egolm.film.api.member.service.MemberTokenService;
import com.egolm.film.api.user.service.UserTokenService;
import com.egolm.film.model.LoginToken;
@Component
public class LogsUtil {
@Autowired
private JdbcTemplate jdbcTemplate;
public void log(TokenService tokenService, HttpServletRequest request) {
LoginToken token = tokenService.getToken();
String type = null;
if(tokenService instanceof AdminTokenService) {
type = "admin";
} else if(tokenService instanceof MemberTokenService) {
type = "member";
} else if(tokenService instanceof UserTokenService) {
type = "user";
}
String loginid = String.valueOf(token.getId());
Date now = new Date();
String path = request.getRequestURI().replace(request.getContextPath(), "");
String prefix = path.split("/")[1];
String content = JSON.toJSONString(request.getParameterMap());
String ip = ServletUtil.readReqJson(request);
String sql = "insert into fc_logs (type, loginid, prefix, path, ip, content, create_time) values (?, ?, ?, ?, ?, ?, ?)";
Object[] objs = new Object[]{type, loginid, prefix, path, ip, content, now};
jdbcTemplate.executeUpdate(sql, objs);
}
}
...@@ -18,13 +18,10 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -18,13 +18,10 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter {
@Autowired @Autowired
private MemberTokenService tokenService; private MemberTokenService tokenService;
@Autowired
private LogsUtil logUtil;
@Override @Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception { public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
logUtil.log(tokenService, req); tokenService.log(req);
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} else { } else {
String sessionid = null; String sessionid = null;
...@@ -35,7 +32,7 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -35,7 +32,7 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter {
} }
} }
if(sessionid != null && tokenService.isLoginByToken(sessionid)) { if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
logUtil.log(tokenService, req); tokenService.log(req);
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} }
throw new XException("用户未登陆", 300); throw new XException("用户未登陆", 300);
......
...@@ -18,13 +18,10 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -18,13 +18,10 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter {
@Autowired @Autowired
private UserTokenService tokenService; private UserTokenService tokenService;
@Autowired
private LogsUtil logUtil;
@Override @Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception { public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
if(tokenService.isLogin()) { if(tokenService.isLogin()) {
logUtil.log(tokenService, req); tokenService.log(req);
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} else { } else {
String sessionid = null; String sessionid = null;
...@@ -35,7 +32,7 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter { ...@@ -35,7 +32,7 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter {
} }
} }
if(sessionid != null && tokenService.isLoginByToken(sessionid)) { if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
logUtil.log(tokenService, req); tokenService.log(req);
return super.preHandle(req, resp, handler); return super.preHandle(req, resp, handler);
} }
throw new XException("用户未登陆", 300); throw new XException("用户未登陆", 300);
......
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