Commit 98d89338 authored by Quxl's avatar Quxl

操作日志

parent 0c33d873
package com.egolm.film.api;
import javax.servlet.http.HttpServletRequest;
import com.egolm.film.model.LoginToken;
public interface TokenService {
......@@ -15,4 +17,6 @@ public interface TokenService {
boolean isLogin();
boolean isLoginByToken(String token);
void log(HttpServletRequest request);
}
package com.egolm.film.api.admin;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -8,7 +10,6 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.egolm.common.bean.Rjx;
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.model.LoginToken;
......@@ -23,23 +24,20 @@ public class AdminLoginController {
@Autowired
private AdminTokenService tokenService;
@Autowired
private Messages messages;
@ResponseBody
@PostMapping("login")
@ApiOperation("登陆")
public Object login(String username, String password) {
XException.assertNotBlank(messages.get("err.user_pwd_null"), username, password);
public Object login(String username, String password, HttpServletRequest request) {
XException.assertNotBlank("用户名或密码不能为空", username, password);
if(tokenService.isLogin()) {
throw new XException("用户已登录", 200);
} else {
LoginToken token = tokenService.doLogin(username, password);
if(token != null) {
tokenService.log(request);
return Rjx.jsonOk().setData(token);
} else {
throw new XException(messages.get("err.user_or_pwd"));
throw new XException("用户名或密码错误");
}
}
}
......
......@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService;
public interface AdminTokenService extends TokenService {
final String TOKEN_NAME = "FILM_ADMIN_LOGIN_TOKEN_NAME";
final String LOG_TYPE = "ADMIN";
}
package com.egolm.film.api.admin.service.impl;
import java.util.Date;
import java.util.List;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
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.admin.service.AdminTokenService;
import com.egolm.film.bean.Fc_admin;
import com.egolm.film.config.WebMvcConfig;
......@@ -95,4 +99,19 @@ public class AdminTokenServiceImpl implements AdminTokenService {
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;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -34,14 +36,14 @@ public class MemberLoginController {
@ApiImplicitParams({
@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);
if(tokenService.isLogin()) {
throw new XException("", 200);
} else {
LoginToken token = tokenService.doLogin(username, password);
if(token != null) {
tokenService.log(request);
return Rjx.jsonOk();
} else {
throw new XException(messages.get("err.user_or_pwd"));
......
......@@ -5,5 +5,6 @@ import com.egolm.film.api.TokenService;
public interface MemberTokenService extends TokenService {
final String TOKEN_NAME = "FILM_MEMBER_LOGIN_TOKEN_NAME";
final String LOG_TYPE = "MEMBER";
}
package com.egolm.film.api.member.service.impl;
import java.util.Date;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
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.member.service.MemberService;
import com.egolm.film.api.member.service.MemberTokenService;
......@@ -24,6 +30,9 @@ public class MemberTokenServiceImpl implements MemberTokenService {
@Autowired
private MemberService memberService;
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public LoginToken doLogin(String email, String password) {
HttpSession session = WebMvcConfig.getSession();
......@@ -94,5 +103,19 @@ public class MemberTokenServiceImpl implements MemberTokenService {
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;
public interface UserTokenService extends TokenService {
final String TOKEN_NAME = "FILM_USER_LOGIN_TOKEN_NAME";
final String LOG_TYPE = "USER";
}
package com.egolm.film.api.user.service.impl;
import java.util.Date;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
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.user.service.UserTokenService;
import com.egolm.film.bean.Fc_user;
......@@ -89,4 +94,18 @@ public class UserTokenServiceImpl implements UserTokenService {
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 {
@Autowired
private AdminTokenService tokenService;
@Autowired
private LogsUtil logUtil;
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
if(tokenService.isLogin()) {
logUtil.log(tokenService, req);
tokenService.log(req);
return super.preHandle(req, resp, handler);
} else {
String sessionid = null;
......@@ -35,7 +32,7 @@ public class AdminLoginInterceptor extends HandlerInterceptorAdapter {
}
}
if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
logUtil.log(tokenService, req);
tokenService.log(req);
return super.preHandle(req, resp, handler);
}
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 {
@Autowired
private MemberTokenService tokenService;
@Autowired
private LogsUtil logUtil;
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
if(tokenService.isLogin()) {
logUtil.log(tokenService, req);
tokenService.log(req);
return super.preHandle(req, resp, handler);
} else {
String sessionid = null;
......@@ -35,7 +32,7 @@ public class MemberLoginInterceptor extends HandlerInterceptorAdapter {
}
}
if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
logUtil.log(tokenService, req);
tokenService.log(req);
return super.preHandle(req, resp, handler);
}
throw new XException("用户未登陆", 300);
......
......@@ -18,13 +18,10 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter {
@Autowired
private UserTokenService tokenService;
@Autowired
private LogsUtil logUtil;
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
if(tokenService.isLogin()) {
logUtil.log(tokenService, req);
tokenService.log(req);
return super.preHandle(req, resp, handler);
} else {
String sessionid = null;
......@@ -35,7 +32,7 @@ public class UserLoginInterceptor extends HandlerInterceptorAdapter {
}
}
if(sessionid != null && tokenService.isLoginByToken(sessionid)) {
logUtil.log(tokenService, req);
tokenService.log(req);
return super.preHandle(req, resp, handler);
}
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