Skip to content

Commit

Permalink
修改数据库🪐
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Oct 30, 2021
1 parent 69825de commit 6eab1c1
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 21 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
>
>项目上线 v1.0 (whz)
>
>主页样式优化 v1.1 (zzp)
>主页样式优化 v1.1 (zzp)
>
>2021/10/30
>
>修改数据库 v1.2 (whz)
16 changes: 10 additions & 6 deletions src/main/java/com/mewhz/paste/controller/HeartController.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package com.mewhz.paste.controller;

import cn.hutool.core.net.NetUtil;
import com.mewhz.paste.utils.IPUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;

@Controller
public class HeartController {

@RequestMapping("heart")
@ResponseBody
public String heart(){
@CrossOrigin
public String heart(HttpServletRequest request){
String userAgent = request.getHeader("user-agent");
System.out.println(userAgent);
String ip = IPUtils.getIpAddr(request);
System.out.println(ip);
return "success";
}
}
10 changes: 8 additions & 2 deletions src/main/java/com/mewhz/paste/controller/SubmitController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

import com.mewhz.paste.model.Code;
import com.mewhz.paste.utils.CodeSQL;
import com.mewhz.paste.utils.IPUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class SubmitController {

@RequestMapping("/submit")
// @ResponseBody
public String code(@RequestParam String text, @RequestParam String type){
@CrossOrigin
public String code(@RequestParam String text, @RequestParam String type, HttpServletRequest request){
Date date = new Date();
Code code = new Code(text, type, date);
String ip = IPUtils.getIpAddr(request);
String userAgent = request.getHeader("User-Agent");
Code code = new Code(text, type, date, ip, userAgent);
System.out.println(code);
CodeSQL codeSql = new CodeSQL();
codeSql.insertCode(code);
Expand Down
38 changes: 28 additions & 10 deletions src/main/java/com/mewhz/paste/model/Code.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ public class Code {
private String text;
private String type;
private Date date;

private String ip;
private String userAgent;
public Code() {

}

public Code(String text, String type, Date date) {
public Code(int id, String text, String type, Date date, String ip, String userAgent) {
this.id = id;
this.text = text;
this.type = type;
this.date = date;
this.ip = ip;
this.userAgent = userAgent;
}

public Code(String text, String type, Date date, String ip, String userAgent) {
this.text = text;
this.type = type;
this.date = date;
this.ip = ip;
this.userAgent = userAgent;
}

public int getId() {
Expand Down Expand Up @@ -50,13 +62,19 @@ public void setDate(Date date) {
this.date = date;
}

@Override
public String toString() {
return "Code{" +
"id=" + id +
", text='" + text + '\'' +
", type='" + type + '\'' +
", date=" + date +
'}';
public String getIp() {
return ip;
}

public void setIp(String ip) {
this.ip = ip;
}

public String getUserAgent() {
return userAgent;
}

public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/mewhz/paste/utils/CodeSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public void insertCode(Code code){
.set("text", code.getText())
.set("time_id", code.getDate().getTime()+"")
.set("type", code.getType())
.set("date", code.getDate())
.set("ip", code.getIp())
.set("user_agent", code.getUserAgent())
);
} catch (SQLException e) {
e.printStackTrace();
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/com/mewhz/paste/utils/IPUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mewhz.paste.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
* Ip地址
*
* @author niunafei
* @function
* @email [email protected]
* @date 2020/9/28 11:23 AM
*/
public class IPUtils {
private static Logger logger = LoggerFactory.getLogger(IPUtils.class);
private static final String IP_UTILS_FLAG = ",";
private static final String UNKNOWN = "unknown";
private static final String LOCALHOST_IP = "0:0:0:0:0:0:0:1";
private static final String LOCALHOST_IP1 = "127.0.0.1";

/**
* 获取IP地址
* <p>
* 使用Nginx等反向代理软件, 则不能通过request.getRemoteAddr()获取IP地址
* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,X-Forwarded-For中第一个非unknown的有效IP字符串,则为真实IP地址
*/
public static String getIpAddr(HttpServletRequest request) {
String ip = null;
try {
//以下两个获取在k8s中,将真实的客户端IP,放到了x-Original-Forwarded-For。而将WAF的回源地址放到了 x-Forwarded-For了。
ip = request.getHeader("X-Original-Forwarded-For");
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
//获取nginx等代理的ip
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("x-forwarded-for");
}
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
//兼容k8s集群获取ip
if (StringUtils.isEmpty(ip) || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
if (LOCALHOST_IP1.equalsIgnoreCase(ip) || LOCALHOST_IP.equalsIgnoreCase(ip)) {
//根据网卡取本机配置的IP
InetAddress iNet = null;
try {
iNet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
logger.error("getClientIp error: {}", e);
}
ip = iNet.getHostAddress();
}
}
} catch (Exception e) {
logger.error("IPUtils ERROR ", e);
}
//使用代理,则获取第一个IP地址
if (!StringUtils.isEmpty(ip) && ip.indexOf(IP_UTILS_FLAG) > 0) {
ip = ip.substring(0, ip.indexOf(IP_UTILS_FLAG));
}

return ip;
}

}
4 changes: 2 additions & 2 deletions src/main/resources/db.setting
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
url = jdbc:mysql://127.0.0.1:3306/paste_code?useSSL=false
url = jdbc:mysql://127.0.0.1:3306/code_mewhz_com?useSSL=false

user = root
pass = root
pass = root

0 comments on commit 6eab1c1

Please sign in to comment.