URL访问量,用户地址,日志登录,附件上传,下载,配置文件上传
This commit is contained in:
@@ -5,6 +5,8 @@ package com.lovenav.utils;
|
||||
|
||||
|
||||
import com.lovenav.entity.User;
|
||||
import com.sun.mail.smtp.SMTPSendFailedException;
|
||||
import com.sun.mail.util.MailConnectException;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
|
||||
@@ -14,6 +16,7 @@ import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
/*
|
||||
@@ -22,14 +25,18 @@ import java.util.Properties;
|
||||
* */
|
||||
public class EmailUtils {
|
||||
|
||||
public static String sendEmail(User user) {
|
||||
public static String sendEmail(User user, HashMap<String,String> configMap) {
|
||||
|
||||
//邮箱 lx_teach@163.com
|
||||
String myAccount = "482370576@qq.com";
|
||||
//授权码 java168
|
||||
String myPass = "hlilinclupppbice";
|
||||
//邮箱服务器
|
||||
String SMTPHost = "smtp.qq.com";
|
||||
// String myAccount = "482370576@qq.com";
|
||||
// //授权码 java168
|
||||
// String myPass = "hlilinclupppbice";
|
||||
// //邮箱服务器
|
||||
// String SMTPHost = "smtp.qq.com";
|
||||
String myAccount=configMap.get("email_sendEmail");
|
||||
String myPass=configMap.get("email_password");
|
||||
String SMTPHost = configMap.get("email_host");
|
||||
|
||||
//设置属性信息
|
||||
Properties prop = new Properties();
|
||||
//设置协议
|
||||
@@ -44,9 +51,13 @@ public class EmailUtils {
|
||||
//设置是否需要调试
|
||||
session.setDebug(false);
|
||||
//2、创建发送信息
|
||||
MimeMessage message = createMsg(session, myAccount, user, activecode);
|
||||
|
||||
|
||||
|
||||
|
||||
//4发送信息操作
|
||||
try {
|
||||
MimeMessage message = createMsg(session, myAccount, user, activecode);
|
||||
Transport tran = session.getTransport();
|
||||
//连接
|
||||
tran.connect(myAccount, myPass);
|
||||
@@ -54,10 +65,18 @@ public class EmailUtils {
|
||||
tran.sendMessage(message, message.getAllRecipients());
|
||||
//关闭
|
||||
tran.close();
|
||||
} catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}catch (SMTPSendFailedException s){
|
||||
s.printStackTrace();
|
||||
return "该邮箱不存在";
|
||||
}catch (MailConnectException m){
|
||||
m.printStackTrace();
|
||||
return "配置文件有错误";
|
||||
}catch (Exception e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
return "配置文件有错误";}
|
||||
|
||||
return activecode;
|
||||
}
|
||||
|
||||
|
105
src/main/java/com/lovenav/utils/IPutils.java
Normal file
105
src/main/java/com/lovenav/utils/IPutils.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.lovenav.utils;
|
||||
|
||||
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.lovenav.entity.IpLocation;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.lionsoul.ip2region.xdb.Searcher;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class IPutils {
|
||||
|
||||
/**
|
||||
* 字符常量0
|
||||
*/
|
||||
private static final String ZERO="0";
|
||||
/**
|
||||
* 本级ip
|
||||
*/
|
||||
private static final String LOCALHOST="127.0.0.1";
|
||||
|
||||
|
||||
/**
|
||||
* 获取客户端的IP地址
|
||||
*/
|
||||
public static String getIpAddress(HttpServletRequest request) {
|
||||
|
||||
String ipAddress = request.getHeader("X-Forwarded-For");
|
||||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getHeader("Proxy-Client-IP");
|
||||
}
|
||||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getHeader("WL-Proxy-Client-IP");
|
||||
}
|
||||
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
|
||||
ipAddress = request.getRemoteAddr();
|
||||
if (LOCALHOST.equals(ipAddress)) {
|
||||
// 根据网卡取本机配置的IP
|
||||
InetAddress inet = null;
|
||||
try {
|
||||
inet = InetAddress.getLocalHost();
|
||||
ipAddress = inet.getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
// 对于通过多个代理转发的情况,取第一个非unknown的IP地址。
|
||||
// 这里假设第一个IP为真实IP,后面的为代理IP。
|
||||
if (ipAddress != null && ipAddress.length() > 15) {
|
||||
if (ipAddress.indexOf(",") > 0) {
|
||||
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
|
||||
}
|
||||
}
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据iP获取归属地信息
|
||||
* @return
|
||||
*/
|
||||
public static IpLocation getLocation(String ip) {
|
||||
IpLocation location = new IpLocation();
|
||||
location.setIp(ip);
|
||||
try (InputStream inputStream = IPutils.class.getResourceAsStream("/ip2region.xdb");) {
|
||||
byte[] bytes = IoUtil.readBytes(inputStream);
|
||||
Searcher searcher = Searcher.newWithBuffer(bytes);
|
||||
String region = searcher.search(ip);
|
||||
if (StrUtil.isAllNotBlank(region)) {
|
||||
// xdb返回格式 国家|区域|省份|城市|ISP,
|
||||
// 只有中国的数据绝大部分精确到了城市,其他国家部分数据只能定位到国家,后前的选项全部是0
|
||||
String[] result = region.split("\\|");
|
||||
location.setCountry(ZERO.equals(result[0])?StrUtil.EMPTY:result[0]);
|
||||
location.setProvince(ZERO.equals(result[2])?StrUtil.EMPTY:result[2]);
|
||||
location.setCity(ZERO.equals(result[3])?StrUtil.EMPTY:result[3]);
|
||||
location.setIsp(ZERO.equals(result[4])?StrUtil.EMPTY:result[4]);
|
||||
}
|
||||
searcher.close();
|
||||
} catch (Exception e) {
|
||||
log.error("ip地址解析异常,error:{}",e);
|
||||
return location;
|
||||
}
|
||||
return location;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user