| | |
| | | import java.util.List; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | |
| | | |
| | | public ServerResponse saveLog(User user, HttpServletRequest request) { |
| | | try { |
| | | SiteLoginLog siteLoginLog = new SiteLoginLog(); |
| | | siteLoginLog.setUserId(user.getId()); |
| | | siteLoginLog.setUserName(user.getRealName()); |
| | | //String ips = IpUtils.getIp(request); |
| | | String userName = user.getRealName(); |
| | | if (StringUtils.isBlank(userName)) { |
| | | userName = user.getPhone(); |
| | | } |
| | | if (StringUtils.isBlank(userName)) { |
| | | userName = String.valueOf(user.getId()); |
| | | } |
| | | siteLoginLog.setUserName(userName); |
| | | |
| | | siteLoginLog.setLoginIp("--"); |
| | | //String ip_address = JuheIpApi.ip2Add(ips); |
| | | siteLoginLog.setLoginAddress("未知"); |
| | | String ips = IpUtils.getIp(request); |
| | | if (StringUtils.isBlank(ips)) { |
| | | ips = "--"; |
| | | } |
| | | siteLoginLog.setLoginIp(ips); |
| | | |
| | | String ipAddress = "未知"; |
| | | if (!"--".equals(ips)) { |
| | | try { |
| | | ipAddress = JuheIpApi.ip2Add(ips); |
| | | if (StringUtils.isBlank(ipAddress)) { |
| | | ipAddress = "未知"; |
| | | } |
| | | } catch (Exception e) { |
| | | log.warn("登录IP地址解析失败 ip={}", ips, e); |
| | | } |
| | | } |
| | | siteLoginLog.setLoginAddress(ipAddress); |
| | | siteLoginLog.setAddTime(new Date()); |
| | | this.siteLoginLogMapper.insert(siteLoginLog); |
| | | log.info("登录日志记录成功 userId={} ip={} address={}", user.getId(), ips, ipAddress); |
| | | } catch (Exception e) { |
| | | log.error("登录日志记录失败 userId={}", user.getId(), e); |
| | | } |
| | | |
| | | return ServerResponse.createBySuccess(); |
| | | } |