Skip to content

Commit 86a5952

Browse files
committed
update parameters
1 parent 71e4ad9 commit 86a5952

File tree

9 files changed

+621
-4
lines changed

9 files changed

+621
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.fluent.builtin;
2+
3+
import java.math.BigDecimal;
4+
5+
public class BigDecimalUtil {
6+
7+
//-1
8+
public static final BigDecimal NEGATIVE_ONE = BigDecimal.valueOf(-1);
9+
public static final BigDecimal ONE_HUNDRED = BigDecimal.valueOf(100);
10+
public static final BigDecimal ONE_HUNDREDTH = new BigDecimal("0.01");
11+
public static final BigDecimal HOUR_8 = BigDecimal.valueOf(8);
12+
13+
public static boolean isEmpty(BigDecimal bigDecimal) {
14+
15+
return bigDecimal == null || BigDecimal.ZERO.compareTo(bigDecimal) == 0;
16+
}
17+
}

components/fluent-spring/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>io.fluent</groupId>
8+
<artifactId>components</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>fluent-spring</artifactId>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
</properties>
17+
<dependencies>
18+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
19+
<dependency>
20+
<groupId>org.springframework</groupId>
21+
<artifactId>spring-core</artifactId>
22+
<version>6.1.11</version>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-context</artifactId>
27+
<version>6.1.11</version>
28+
</dependency>
29+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-webmvc</artifactId>
33+
<version>6.1.11</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.springframework</groupId>
38+
<artifactId>spring-context-support</artifactId>
39+
<version>6.1.11</version>
40+
</dependency>
41+
<!-- https://mvnrepository.com/artifact/jakarta.servlet/jakarta.servlet-api -->
42+
<dependency>
43+
<groupId>jakarta.servlet</groupId>
44+
<artifactId>jakarta.servlet-api</artifactId>
45+
<version>6.1.0</version>
46+
<scope>provided</scope>
47+
</dependency>
48+
49+
</dependencies>
50+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.fluent.spring.config;
2+
3+
import io.fluent.spring.util.SpringUtils;
4+
import org.springframework.context.annotation.Bean;
5+
6+
7+
public class SpringConfig {
8+
@Bean("fluentSpringUtils")
9+
public SpringUtils springUtils() {
10+
return new SpringUtils();
11+
}
12+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
package io.fluent.spring.util;
2+
3+
4+
import jakarta.servlet.http.HttpServletRequest;
5+
6+
import java.net.InetAddress;
7+
import java.net.UnknownHostException;
8+
import java.util.Objects;
9+
10+
/**
11+
* 获取IP方法
12+
*/
13+
public class IpUtils {
14+
public static String getIpAddr(HttpServletRequest request) {
15+
if (Objects.isNull(request)) {
16+
return "127.0.0.1";
17+
}
18+
String ip = null;
19+
20+
// X-Forwarded-For:Squid 服务代理
21+
String ipAddresses = request.getHeader("X-Forwarded-For");
22+
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
23+
// Proxy-Client-IP:apache 服务代理
24+
ipAddresses = request.getHeader("Proxy-Client-IP");
25+
}
26+
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
27+
// WL-Proxy-Client-IP:weblogic 服务代理
28+
ipAddresses = request.getHeader("WL-Proxy-Client-IP");
29+
}
30+
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
31+
// HTTP_CLIENT_IP:有些代理服务器
32+
ipAddresses = request.getHeader("HTTP_CLIENT_IP");
33+
}
34+
if (ipAddresses == null || ipAddresses.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
35+
// X-Real-IP:nginx服务代理
36+
ipAddresses = request.getHeader("X-Real-IP");
37+
}
38+
39+
// 有些网络通过多层代理,那么获取到的ip就会有多个,一般都是通过逗号(,)分割开来,并且第一个ip为客户端的真实IP
40+
if (ipAddresses != null && ipAddresses.length() != 0) {
41+
ip = ipAddresses.split(",")[0];
42+
}
43+
44+
// 还是不能获取到,最后再通过request.getRemoteAddr();获取
45+
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ipAddresses)) {
46+
ip = request.getRemoteAddr();
47+
}
48+
return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
49+
}
50+
51+
public static boolean internalIp(String ip) {
52+
byte[] addr = textToNumericFormatV4(ip);
53+
return internalIp(addr) || "127.0.0.1".equals(ip);
54+
}
55+
56+
private static boolean internalIp(byte[] addr) {
57+
if (addr == null || addr.length < 2) {
58+
return true;
59+
}
60+
final byte b0 = addr[0];
61+
final byte b1 = addr[1];
62+
// 10.x.x.x/8
63+
final byte SECTION_1 = 0x0A;
64+
// 172.16.x.x/12
65+
final byte SECTION_2 = (byte) 0xAC;
66+
final byte SECTION_3 = (byte) 0x10;
67+
final byte SECTION_4 = (byte) 0x1F;
68+
// 192.168.x.x/16
69+
final byte SECTION_5 = (byte) 0xC0;
70+
final byte SECTION_6 = (byte) 0xA8;
71+
switch (b0) {
72+
case SECTION_1:
73+
return true;
74+
case SECTION_2:
75+
if (b1 >= SECTION_3 && b1 <= SECTION_4) {
76+
return true;
77+
}
78+
case SECTION_5:
79+
switch (b1) {
80+
case SECTION_6:
81+
return true;
82+
}
83+
default:
84+
return false;
85+
}
86+
}
87+
88+
/**
89+
* 将IPv4地址转换成字节
90+
*
91+
* @param text IPv4地址
92+
* @return byte 字节
93+
*/
94+
public static byte[] textToNumericFormatV4(String text) {
95+
if (text.length() == 0) {
96+
return null;
97+
}
98+
99+
byte[] bytes = new byte[4];
100+
String[] elements = text.split("\\.", -1);
101+
try {
102+
long l;
103+
int i;
104+
switch (elements.length) {
105+
case 1:
106+
l = Long.parseLong(elements[0]);
107+
if ((l < 0L) || (l > 4294967295L)) {
108+
return null;
109+
}
110+
bytes[0] = (byte) (int) (l >> 24 & 0xFF);
111+
bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
112+
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
113+
bytes[3] = (byte) (int) (l & 0xFF);
114+
break;
115+
case 2:
116+
l = Integer.parseInt(elements[0]);
117+
if ((l < 0L) || (l > 255L)) {
118+
return null;
119+
}
120+
bytes[0] = (byte) (int) (l & 0xFF);
121+
l = Integer.parseInt(elements[1]);
122+
if ((l < 0L) || (l > 16777215L)) {
123+
return null;
124+
}
125+
bytes[1] = (byte) (int) (l >> 16 & 0xFF);
126+
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
127+
bytes[3] = (byte) (int) (l & 0xFF);
128+
break;
129+
case 3:
130+
for (i = 0; i < 2; ++i) {
131+
l = Integer.parseInt(elements[i]);
132+
if ((l < 0L) || (l > 255L)) {
133+
return null;
134+
}
135+
bytes[i] = (byte) (int) (l & 0xFF);
136+
}
137+
l = Integer.parseInt(elements[2]);
138+
if ((l < 0L) || (l > 65535L)) {
139+
return null;
140+
}
141+
bytes[2] = (byte) (int) (l >> 8 & 0xFF);
142+
bytes[3] = (byte) (int) (l & 0xFF);
143+
break;
144+
case 4:
145+
for (i = 0; i < 4; ++i) {
146+
l = Integer.parseInt(elements[i]);
147+
if ((l < 0L) || (l > 255L)) {
148+
return null;
149+
}
150+
bytes[i] = (byte) (int) (l & 0xFF);
151+
}
152+
break;
153+
default:
154+
return null;
155+
}
156+
} catch (NumberFormatException e) {
157+
return null;
158+
}
159+
return bytes;
160+
}
161+
162+
public static String getHostIp() {
163+
try {
164+
return InetAddress.getLocalHost().getHostAddress();
165+
} catch (UnknownHostException e) {
166+
}
167+
return "127.0.0.1";
168+
}
169+
170+
public static String getHostName() {
171+
try {
172+
return InetAddress.getLocalHost().getHostName();
173+
} catch (UnknownHostException e) {
174+
}
175+
return "未知";
176+
}
177+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package io.fluent.spring.util;
2+
3+
import org.springframework.beans.factory.config.BeanDefinition;
4+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
5+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
6+
import org.springframework.context.ConfigurableApplicationContext;
7+
import org.springframework.util.ClassUtils;
8+
import org.springframework.util.ReflectionUtils;
9+
import org.springframework.util.StringUtils;
10+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
11+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
12+
13+
import java.lang.reflect.InvocationTargetException;
14+
import java.lang.reflect.Method;
15+
16+
/**
17+
* 手动注入
18+
19+
*/
20+
public class SpringRegisterUtil {
21+
22+
/**
23+
* 向IOC 容器中注入 bean
24+
*
25+
* @param clazz bean class
26+
* @param beanName bean beanName
27+
*/
28+
public static void registerBean(Class<?> clazz, String beanName) {
29+
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
30+
BeanDefinition beanDefinition = beanDefinitionBuilder.getRawBeanDefinition();
31+
//设置当前bean定义对象是单利的
32+
beanDefinition.setScope("singleton");
33+
if (!StringUtils.hasText(beanName)) {
34+
//将变量首字母置小写
35+
beanName = StringUtils.uncapitalize(clazz.getSimpleName());
36+
beanName = beanName.substring(beanName.lastIndexOf(".") + 1);
37+
beanName = StringUtils.uncapitalize(beanName);
38+
}
39+
40+
//将applicationContext转换为ConfigurableApplicationContext
41+
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringUtils.getApplicationContext();
42+
// 获取bean工厂并转换为DefaultListableBeanFactory
43+
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
44+
defaultListableBeanFactory.registerBeanDefinition(beanName, beanDefinition);
45+
}
46+
47+
/**
48+
* 卸载bean
49+
*
50+
* @param className
51+
* @throws Exception
52+
*/
53+
public void unregisterBean(String className) throws Exception {
54+
55+
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) SpringUtils.getApplicationContext();
56+
// 获取bean工厂并转换为DefaultListableBeanFactory
57+
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext.getBeanFactory();
58+
defaultListableBeanFactory.removeBeanDefinition(className);
59+
}
60+
61+
/**
62+
* 注册Controller
63+
*
64+
* @param controllerBeanName
65+
* @throws NoSuchMethodException
66+
* @throws InvocationTargetException
67+
* @throws IllegalAccessException
68+
*/
69+
public static void registerController(String controllerBeanName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
70+
71+
//先注册bean
72+
//registerBean(controllerBeanName);
73+
74+
final RequestMappingHandlerMapping requestMappingHandlerMapping = SpringUtils.getBean(RequestMappingHandlerMapping.class);
75+
String handler = controllerBeanName;
76+
Object controller = SpringUtils.getBean(handler);
77+
// unregisterController(controllerBeanName);
78+
if (controller == null) {
79+
return;
80+
}
81+
//注册Controller
82+
Method method = requestMappingHandlerMapping.getClass().getSuperclass().getSuperclass().
83+
getDeclaredMethod("detectHandlerMethods", Object.class);
84+
//将private改为可使用
85+
method.setAccessible(true);
86+
method.invoke(requestMappingHandlerMapping, controllerBeanName);
87+
}
88+
89+
/**
90+
* 去掉Controller的Mapping
91+
*
92+
* @param controllerBeanName
93+
*/
94+
public static void unregisterController(String controllerBeanName) {
95+
final RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping)
96+
SpringUtils.getBean("requestMappingHandlerMapping");
97+
if (requestMappingHandlerMapping != null) {
98+
String handler = controllerBeanName;
99+
Object controller = SpringUtils.getBean(handler);
100+
if (controller == null) {
101+
return;
102+
}
103+
final Class<?> targetClass = controller.getClass();
104+
ReflectionUtils.doWithMethods(targetClass, new ReflectionUtils.MethodCallback() {
105+
public void doWith(Method method) {
106+
Method specificMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
107+
try {
108+
Method createMappingMethod = RequestMappingHandlerMapping.class.
109+
getDeclaredMethod("getMappingForMethod", Method.class, Class.class);
110+
createMappingMethod.setAccessible(true);
111+
RequestMappingInfo requestMappingInfo = (RequestMappingInfo)
112+
createMappingMethod.invoke(requestMappingHandlerMapping, specificMethod, targetClass);
113+
if (requestMappingInfo != null) {
114+
requestMappingHandlerMapping.unregisterMapping(requestMappingInfo);
115+
}
116+
} catch (Exception e) {
117+
e.printStackTrace();
118+
}
119+
}
120+
}, ReflectionUtils.USER_DECLARED_METHODS);
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)