获取客户端真实IP地址

master
kezhen0805 2 years ago
parent 39b99b25ba
commit 373d6a9533

@ -1,6 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="Encoding"> <component name="Encoding">
<file url="file://$PROJECT_DIR$/beacon-cache/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/beacon-common/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/beancon-api/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/beancon-api/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mashibing</groupId>
<artifactId>beacon-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>beacon-cache</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- web项目-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- nacos注册中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- nacos配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- data系列的redis依赖-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-redis</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.msb.cloud</groupId>
<artifactId>horse-framework-starter-redis</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>

@ -0,0 +1,18 @@
package com.mashibing.cache;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author kezhen
* @date 2022/12/25
* @description
*/
@SpringBootApplication
@EnableDiscoveryClient
public class CacheStarterApp {
public static void main(String[] args) {
SpringApplication.run(CacheStarterApp.class, args);
}
}

@ -0,0 +1,68 @@
package com.mashibing.cache.config;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author kezhen
* @date 2022/12/25
* @description redisTemplate
*/
@Configuration
public class RedisConfig {
// @Bean
// public <T> RedisTemplate<String ,T> redisTemplate(RedisConnectionFactory factory,RedisSerializer<Object> redisSerializer){
// RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
// redisTemplate.setConnectionFactory(factory);
// redisTemplate.setKeySerializer(RedisSerializer.string());
// redisTemplate.setHashKeySerializer(RedisSerializer.string());
//
// redisTemplate.setValueSerializer(redisSerializer);
// redisTemplate.setHashValueSerializer(redisSerializer);
//
// redisTemplate.afterPropertiesSet();
// return redisTemplate;
// }
//
// @Bean
// public RedisSerializer<Object> redisSerializer(){
// ObjectMapper objectMapper = new ObjectMapper();
//
// DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//
// Module timeModule = new JavaTimeModule()
// .addDeserializer(LocalDate.class,new LocalDateDeserializer(dateFormatter))
// .addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(dateTimeFormatter))
// .addSerializer(LocalDate.class,new LocalDateSerializer(dateFormatter))
// .addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(dateTimeFormatter));
// objectMapper.registerModules(timeModule);
//
// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//
// jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//
// return jackson2JsonRedisSerializer;
//
//
// }
}

@ -0,0 +1,37 @@
package com.mashibing.cache.contorller;
import com.msb.framework.redis.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;
/**
* @author kezhen
* @date 2022/12/25
* @description
*/
@RestController
public class TestController {
// @Autowired
// @Resource
// private RedisTemplate<String, Objects> redisTemplate;
@Resource
private RedisClient redisClient;
@PostMapping("/test/set/{key}")
public String set(@PathVariable String key, @RequestBody Map map){
System.out.println("TestController.set");
redisClient.putMap(key,map);
return "ok";
}
@GetMapping("/text/get/{key}")
public Map get(@PathVariable String key) {
Map<String, Object> result = redisClient.getMap(key);
return result;
}
}

@ -0,0 +1,13 @@
spring:
application:
name: beacon-cache
profiles:
active: dev
cloud:
nacos:
config:
server-addr: 192.168.1.8:8848
file-extension: yml
# beacon-cache-dev.yml
discovery:
server-addr: 192.168.1.8:8848

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.mashibing</groupId>
<artifactId>beacon-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>beacon-common</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,56 @@
package com.mashibing.common.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.time.LocalDateTime;
/**
* @author kezhen
* @date 2022/12/24
* @description -
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class StandardSubmit {
//针对当前短信的唯一标识,雪花算法
private Long sequenceId;
//客户端ID
private Long cliendId;
//客户的ip白名单
private String ip;
//客户业务内的uid客户请求传递的
private String uid;
//目标手机号
private String mobile;
//短信内容的签名
private String sign;
//短信内容
private String text;
//短信的发送时间,当前系统时间
private LocalDateTime sendTime;
//当前短信的费用计算短信内容的文字70个字一条超过部分67字一条
private Long fee;
// 目标手机号的运营商(策略模块)
private Integer operatorId;
//目标手机号的归属区号(策略模块)
private Integer areaCode;
//区域名称,目标手机号的归属地(策略模块)
private String area;
//通道下发的源号码 eg:1069123412341234 (策略模块)
private String srcNumber;
/**
* id
*/
private Long channelId;
/**
* 0-/ing1-2-0
*/
private int reportState;
//后续再做封装
}

@ -52,6 +52,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId> <artifactId>spring-boot-starter-validation</artifactId>
</dependency> </dependency>
<!-- 一些公公共的组件会放到当前的common中-->
<dependency>
<groupId>com.mashibing</groupId>
<artifactId>beacon-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
<properties> <properties>

@ -5,6 +5,7 @@ import com.mashibing.api.from.SingleSendForm;
import com.mashibing.api.util.R; import com.mashibing.api.util.R;
import com.mashibing.api.vo.ResultVO; import com.mashibing.api.vo.ResultVO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult; import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
@ -12,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/** /**
* @author kezhen * @author kezhen
* @date 2022/12/23 * @date 2022/12/23
@ -27,14 +30,55 @@ public class SmsController {
* @return * @return
*/ */
@PostMapping(value = "/single_send", produces = "application/json;charset=utf-8") @PostMapping(value = "/single_send", produces = "application/json;charset=utf-8")
public ResultVO signleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult) { public ResultVO signleSend(@RequestBody @Validated SingleSendForm singleSendForm, BindingResult bindingResult, HttpServletRequest request) {
System.out.println(singleSendForm); System.out.println(singleSendForm);
System.out.println("SmsController.signleSend"); System.out.println("SmsController.signleSend");
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
String message = bindingResult.getFieldError().getDefaultMessage(); String message = bindingResult.getFieldError().getDefaultMessage();
log.info("【接口模块-单条短信Contorller】 参数不合法msg = {}", message); log.info("【接口模块-单条短信Contorller】 参数不合法msg = {}", message);
return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(),message); return R.error(SmsCodeEnum.PARAMETER_ERROR.getCode(), message);
} }
//===============构建StandardSubmit==================
String ip = this.getRealIP(request);
System.out.println("ip = " + ip);
//===============发送到MQ交给策略模块处理==================
return R.ok(); return R.ok();
} }
/**
* IP
*
* @param request
* @return
*/
private String getRealIP(HttpServletRequest request) {
String ip;
String ips = request.getHeader("x-forwarded-for");
if (StringUtils.isEmpty(ips) || "unknow".equalsIgnoreCase(ips)) {
return ips.substring(0, ips.indexOf(","));
}
ip = request.getHeader("x-real-ip");
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-client-ip");
}
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
ip = request.getHeader("wl-proxy-client-ip");
}
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
//7. 基于其他代理服务器的方式获取请求头的IP地址
ip = request.getHeader("http_client_ip");
}
if (StringUtils.isEmpty(ip) || "unknow".equalsIgnoreCase(ip)) {
//8. 基于传统方式获取IP
ip = request.getRemoteAddr();
}
return ip;
}
} }

@ -17,14 +17,14 @@
<packaging>pom</packaging> <packaging>pom</packaging>
<modules> <modules>
<module>beancon-api</module> <module>beancon-api</module>
<module>beacon-common</module>
<module>beacon-cache</module>
</modules> </modules>
<properties> <properties>
<spring.cloud-version>Hoxton.SR12</spring.cloud-version> <spring.cloud-version>Hoxton.SR12</spring.cloud-version>
<spring.cloud.alibaba-version>2.2.6.RELEASE</spring.cloud.alibaba-version> <spring.cloud.alibaba-version>2.2.6.RELEASE</spring.cloud.alibaba-version>
<maven.compiler.source>8</maven.compiler.source> <horse-version>1.0.0</horse-version>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -43,6 +43,13 @@
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>com.msb.cloud</groupId>
<artifactId>horse-framework-starters</artifactId>
<version>${horse-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

Loading…
Cancel
Save