Skip to content

Instantly share code, notes, and snippets.

@AutMaple
Last active January 15, 2023 12:44
Show Gist options
  • Select an option

  • Save AutMaple/84ac7e2f9284c8c8c103e18e630966c3 to your computer and use it in GitHub Desktop.

Select an option

Save AutMaple/84ac7e2f9284c8c8c103e18e630966c3 to your computer and use it in GitHub Desktop.
[工具类仓库]
package com.autmaple.oauth.components.redis;
import com.autmaple.oauth.common.utils.SpringUtil;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
public class RedisUtil {
@SuppressWarnings("unchecked")
private static final RedisTemplate<String, Object> template = SpringUtil.getBean(RedisTemplate.class);
public static Boolean expire(String key, long timeout, TimeUnit unit) {
return template.expire(key, timeout, unit);
}
public static Boolean expire(String key, long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
public static Long getExpire(String key) {
return template.getExpire(key);
}
public static Boolean del(String key) {
return template.delete(key);
}
public static Long del(List<String> keys) {
return template.delete(keys);
}
public static void set(String key, Object value) {
template.opsForValue().set(key, value);
}
public static void set(String key, Object value, long timeout) {
template.opsForValue().set(key, value);
expire(key, timeout);
}
public static void set(String key, Object value, long timeout, TimeUnit unit) {
template.opsForValue().set(key, value);
expire(key, timeout, unit);
}
public static Object get(String key) {
return template.opsForValue().get(key);
}
public static Boolean hasKey(String key) {
return template.hasKey(key);
}
public static Long increment(String key, long delta) {
return template.opsForValue().increment(key, delta);
}
public static Long decrement(String key, long delta) {
return increment(key, -delta);
}
public static Object hashGet(String key, String hashKey) {
return template.opsForHash().get(key, hashKey);
}
public static List<Object> hashMultiGet(String key, Collection<Object> hashKeys) {
return template.opsForHash().multiGet(key, hashKeys);
}
public static Map<Object, Object> hashGetAll(String key) {
return template.opsForHash().entries(key);
}
public static void hashSet(String key, String hashKey, Object value) {
template.opsForHash().put(key, hashKey, value);
}
public static void hashSet(String key, Map<String, Object> values) {
template.opsForHash().putAll(key, values);
}
public static void hashDel(String key, Object... hashKeys) {
template.opsForHash().delete(key, hashKeys);
}
public static Boolean hashHasKey(String key, String hashKey) {
return template.opsForHash().hasKey(key, hashKey);
}
public static Long hashIncrement(String key, String hashKey, Long delta) {
return template.opsForHash().increment(key, hashKey, delta);
}
public static Long hashDecrement(String key, String hashKey, Long delta) {
return hashIncrement(key, hashKey, -delta);
}
public static Set<Object> setGet(String key) {
return template.opsForSet().members(key);
}
public static Long setAdd(String key, Object... values) {
return template.opsForSet().add(key, values);
}
public static Boolean setIsMember(String key, Object value) {
return template.opsForSet().isMember(key, value);
}
public static Long setSize(String key) {
return template.opsForSet().size(key);
}
public static Long setDelete(String key, Object... values) {
return template.opsForSet().remove(key, values);
}
public static List<Object> listRange(String key, long start, long end) {
return template.opsForList().range(key, start, end);
}
public static Long listSize(String key) {
return template.opsForList().size(key);
}
public static Object listGet(String key, long index) {
return template.opsForList().index(key, index);
}
public static Long ListPushAll(String key, Object... values) {
return template.opsForList().rightPushAll(key, values);
}
/**
* 从 list 中删除 count 个 与 value 值相等的元素
*
* @param count 0 表示删除所有与 value 相等的元素, 大于 0 表示从左边开始删除, 小于 0 表示从右边开始删除
*/
public static Long listRemove(String key, long count, Object value) {
return template.opsForList().remove(key, count, value);
}
public static Boolean bitSet(String key, int offset, boolean bit) {
return template.opsForValue().setBit(key, offset, bit);
}
public static Boolean bitGet(String key, int offset) {
return template.opsForValue().getBit(key, offset);
}
public static Long bitCount(String key) {
return template.execute((RedisCallback<Long>) connection -> connection.bitCount(key.getBytes()));
}
public static byte[] bitGetAll(String key) {
return template.execute((RedisCallback<byte[]>) connection -> connection.get(key.getBytes()));
}
}
package com.autmaple.mall.tiny.common.utils;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @ClassName RequestUtil
* @Description HTTP 请求工具类
* @Author AutMaple
* @Date 2022/7/10 14:29
* @Version 1.0
**/
public class RequestUtil {
// 获取发送请求的真实 IP 地址
public static String getRequestIp(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();
// 从本地访问时,根据网卡读取本机配置的 IP
if ("127.0.0.1".equals(ipAddress) || "0:0:0:0:0:0:0:1".equals(ipAddress)) {
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inetAddress.getHostAddress();
}
}
// 通过多个代理转发的情况,第一个 IP 为客户端真实 IP,多个 IP 按照 `,` 分割
if (ipAddress != null && ipAddress.length() > 15) {
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
}
package com.autmaple.oauth.common.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static Object getBean(String beanName) {
return context.getBean(beanName);
}
public static <T> T getBean(Class<T> clazz) {
return context.getBean(clazz);
}
public static <T> T getBean(String beanName, Class<T> clazz) {
return context.getBean(beanName, clazz);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment