日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot 详解 (四)redis filter

發(fā)布時間:2024/9/21 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot 详解 (四)redis filter 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?----------------------------------------------------------------------------------------------------------------

springboot 詳解 (一) helloworld? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????下載demo??? ? ????

springboot 詳解 (二) crud?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?下載demo?? ??

springboot 詳解 (三) 多數(shù)據(jù)源? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??下載demo??? ??

springboot 詳解 (四)redis & filter?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?下載demo????

springboot 詳解 (五)interceptor??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???下載demo?? ??

springboot 詳解 (六)servlet & scheduled & listener? ??? ? ?下載demo?? ??

springboot 詳解(七) dubbox & zookeeper? ?下載(productor)?下載(constumser)

springboot 同步解耦 異步化?下載demo

springboot jenkins docker 部署?

springboot 詳解(八) springboot & springcloud?

----------------------------------------------------------------------------------------------------------------

?

?

?

package com.curiousby.cn.redis;import java.util.List;public interface IRedisService {public boolean set(String key, String value);public boolean set(String key, String value,long expire);public String get(String key);public boolean expire(String key,long expire);public <T> boolean setList(String key ,List<T> list);public <T> List<T> getList(String key,Class<T> clz);public long lpush(String key,Object obj);public long rpush(String key,Object obj);public String lpop(String key);}

?

package com.curiousby.cn.redis;import org.apache.log4j.Logger; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;import redis.clients.jedis.JedisPoolConfig;/*** * @author vic* @desc redis config bean**/ @Configuration @EnableAutoConfiguration public class RedisConfig {private static Logger logger = Logger.getLogger(RedisConfig.class);@Bean@ConfigurationProperties(prefix="spring.redis")public JedisPoolConfig getRedisConfig(){JedisPoolConfig config = new JedisPoolConfig();return config;}@Bean@ConfigurationProperties(prefix="spring.redis")public JedisConnectionFactory getConnectionFactory(){JedisConnectionFactory factory = new JedisConnectionFactory();JedisPoolConfig config = getRedisConfig();factory.setPoolConfig(config);logger.info("JedisConnectionFactory bean init success.");return factory;}@Beanpublic RedisTemplate<?, ?> getRedisTemplate(){RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());return template;} }

?

?

package com.curiousby.cn.redis;import java.util.List; import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.stereotype.Service;import com.curiousby.cn.util.JSONUtil;/*** * @author * @desc resdis service**/ @Service(value="redisService") public class RedisServiceImpl implements IRedisService{@Autowiredprivate RedisTemplate<String, String> redisTemplate;@Overridepublic boolean set(final String key, final String value) {boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();connection.set(serializer.serialize(key), serializer.serialize(value));return true;}});return result;}@Overridepublic boolean set(final String key, final String value,final long expire) {boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();connection.set(serializer.serialize(key), serializer.serialize(value));redisTemplate.expire(key, expire, TimeUnit.SECONDS);return true;}});return result;}public String get(final String key){String result = redisTemplate.execute(new RedisCallback<String>() {@Overridepublic String doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();byte[] value = connection.get(serializer.serialize(key));return serializer.deserialize(value);}});return result;}@Overridepublic boolean expire(final String key, long expire) {return redisTemplate.expire(key, expire, TimeUnit.SECONDS);}@Overridepublic <T> boolean setList(String key, List<T> list) {String value = JSONUtil.toJson(list);return set(key,value);}@Overridepublic <T> List<T> getList(String key,Class<T> clz) {String json = get(key);if(json!=null){List<T> list = JSONUtil.toList(json, clz);return list;}return null;}@Overridepublic long lpush(final String key, Object obj) {final String value = JSONUtil.toJson(obj);long result = redisTemplate.execute(new RedisCallback<Long>() {@Overridepublic Long doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();long count = connection.lPush(serializer.serialize(key), serializer.serialize(value));return count;}});return result;}@Overridepublic long rpush(final String key, Object obj) {final String value = JSONUtil.toJson(obj);long result = redisTemplate.execute(new RedisCallback<Long>() {@Overridepublic Long doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();long count = connection.rPush(serializer.serialize(key), serializer.serialize(value));return count;}});return result;}@Overridepublic String lpop(final String key) {String result = redisTemplate.execute(new RedisCallback<String>() {@Overridepublic String doInRedis(RedisConnection connection) throws DataAccessException {RedisSerializer<String> serializer = redisTemplate.getStringSerializer();byte[] res = connection.lPop(serializer.serialize(key));return serializer.deserialize(res);}});return result;}}

?

spring.redis.database=0 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.timeout=0

?

package com.curiousby.cn.filter;import java.io.IOException;import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest;import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils;import com.curiousby.cn.redis.IRedisService;@WebFilter(filterName="ipFilter",urlPatterns="/*") public class IPFilter implements Filter {protected static final Logger logger = LoggerFactory.getLogger(IPFilter.class);public final static int IPMAXCOUNTPERMINUTES = 5;public final static long IPLIMITSENCONDS = 300;public final static long IPCOUNTSENCONDS = 60;// @ResourceIRedisService iredisService;@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request; WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());iredisService = (IRedisService) wac.getBean("redisService");String ip = getIpAddr(req); String ipLimit = iredisService.get(ip+"_limit");if (ipLimit !=null && !"".equals(ipLimit)) {req.getRequestDispatcher("/web/static/forward").forward(req, response);return;}else{String ipConut = iredisService.get(ip+"_count");if (ipConut !=null && !"".equals(ipConut)){int ipLCount = Integer.parseInt(ipConut);if(ipLCount >= IPMAXCOUNTPERMINUTES){iredisService.set(ip+"_limit", ipLCount+"", IPLIMITSENCONDS);iredisService.set(ip+"_count", "0", IPCOUNTSENCONDS); req.getRequestDispatcher("/web/static/forward").forward(req, response);return;}else{ipLCount += 1; iredisService.set(ip+"_count", ipLCount+"", IPCOUNTSENCONDS); } }else{iredisService.set(ip+"_count", "1", IPCOUNTSENCONDS);}} chain.doFilter(req, response);}@Overridepublic void destroy() {}public String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("x-forwarded-for"); if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return ip; } }

?

?



?

?

?

?

package com.curiousby.cn.util;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken;/*** * @author vic* @desc json util */ public class JSONUtil {private static Gson gson = null; static{gson = new Gson();//todo yyyy-MM-dd HH:mm:ss }public static synchronized Gson newInstance(){if(gson == null){gson = new Gson();}return gson;}public static String toJson(Object obj){return gson.toJson(obj);}public static <T> T toBean(String json,Class<T> clz){return gson.fromJson(json, clz);}public static <T> Map<String, T> toMap(String json,Class<T> clz){Map<String, JsonObject> map = gson.fromJson(json, new TypeToken<Map<String,JsonObject>>(){}.getType());Map<String, T> result = new HashMap<>();for(String key:map.keySet()){result.put(key,gson.fromJson(map.get(key),clz) );}return result;}public static Map<String, Object> toMap(String json){Map<String, Object> map = gson.fromJson(json, new TypeToken<Map<String,Object>>(){}.getType());return map;}public static <T> List<T> toList(String json,Class<T> clz){JsonArray array = new JsonParser().parse(json).getAsJsonArray(); List<T> list = new ArrayList<>();for(final JsonElement elem : array){ list.add(gson.fromJson(elem, clz));}return list;}public static void main(String[] args) {}}

?

?

?

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的springboot 详解 (四)redis filter的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。