----------------------------------------------------------------------------------------------------------------
springboot 详解 (一) helloworld 下载demo
springboot 详解 (四)redis & filter 下载demo
springboot 详解 (五)interceptor 下载demo
springboot 详解 (六)servlet & scheduled & listener 下载demo
springboot 详解(七) dubbox & zookeeper 下载(productor) 下载(constumser)
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; } @Bean public 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{ @Autowired private RedisTemplate<String, String> redisTemplate; @Override public boolean set(final String key, final String value) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public Boolean doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); connection.set(serializer.serialize(key), serializer.serialize(value)); return true; } }); return result; } @Override public boolean set(final String key, final String value,final long expire) { boolean result = redisTemplate.execute(new RedisCallback<Boolean>() { @Override public 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>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { RedisSerializer<String> serializer = redisTemplate.getStringSerializer(); byte[] value = connection.get(serializer.serialize(key)); return serializer.deserialize(value); } }); return result; } @Override public boolean expire(final String key, long expire) { return redisTemplate.expire(key, expire, TimeUnit.SECONDS); } @Override public <T> boolean setList(String key, List<T> list) { String value = JSONUtil.toJson(list); return set(key,value); } @Override public <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; } @Override public long lpush(final String key, Object obj) { final String value = JSONUtil.toJson(obj); long result = redisTemplate.execute(new RedisCallback<Long>() { @Override public 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; } @Override public long rpush(final String key, Object obj) { final String value = JSONUtil.toJson(obj); long result = redisTemplate.execute(new RedisCallback<Long>() { @Override public 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; } @Override public String lpop(final String key) { String result = redisTemplate.execute(new RedisCallback<String>() { @Override public 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; // @Resource IRedisService iredisService; @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public 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); } @Override public 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>
相关推荐
1.5 入门案例详解 11 1.5.1 POM文件 11 1.5.2 主程序类,主入口类 12 1.6 使用Spring Initializer向导快速创建Spring Boot 16 2 Spring Boot配置 18 2.1 配置文件 18 2.2 YML语法 19 2.3 YML配置文件值获取 21 2.4 ...
**SpringBoot2整合Shiro详解** 在现代Java Web开发中,SpringBoot以其便捷的配置、快速的启动和强大的集成能力,成为了许多开发者的首选框架。而Shiro则是一款轻量级的安全框架,提供了身份验证、授权、会话管理和...
《SpringBoot与Shiro构建细粒度动态权限管理系统详解》 在现代Web开发中,权限管理是不可或缺的一部分,尤其对于企业级应用来说更是如此。SpringBoot以其轻量级、便捷的特性,已经成为Java开发的首选框架之一。而...
- Spring Boot 基础知识,包括SpringBoot起步、配置详解、aop、filter、拦截器、监听、启动器、全局异常处理、外部Tomcat启动、HTTPS、监控 等。 - springboot-data - Spring Boot 数据库操作,包括SpringJDBC、...
在SpringBoot中,我们可以通过实现`HandlerInterceptor`接口或使用`@WebFilter`、`@WebListener`注解来创建拦截器。本项目可能使用拦截器来实现权限验证、日志记录等功能,进一步优化系统架构。 四、全局异常处理 ...
详解SpringBoot2 使用Spring Session集群 SpringBoot2 是一个流行的 Java 框架,用于构建基于 Web 的应用程序,而 Spring Session 是一个用于管理会话的框架。本文将详细介绍如何使用 Spring Session 在 SpringBoot...
### Spring Boot .yml 配置详解 #### 一、概览 在现代Java开发中,Spring Boot凭借其“约定优于配置”的理念极大地简化了应用的搭建与部署过程。其中,`application.yml`文件作为Spring Boot项目的配置文件之一,...
**SpringBoot + Shiro 权限管理详解** 在现代企业级应用开发中,权限管理是不可或缺的一部分,它确保用户只能访问他们被授权的资源。SpringBoot 和 Apache Shiro 是两个非常流行的 Java 开发框架,它们结合可以构建...
《Spring Boot整合Shiro实战详解》 在当前的Web开发领域,Spring Boot以其简洁的配置、快速的开发体验以及强大的生态系统,成为了许多开发者的选择。而Apache Shiro则是一款优秀的安全管理框架,它为Java应用提供了...
Shiro 的配置主要包括两个部分:一个是 Shiro 的配置文件,另一个是 Shiro 的 Filter 配置。Shiro 的 Filter 配置主要包括身份验证、授权、会话管理等功能。 CAS 介绍 CAS(Central Authentication Service)是一...
《Spring Boot结合Shiro实现权限管理详解》 在现代企业级应用开发中,权限管理是一项不可或缺的功能,它确保了系统安全性和用户数据的隐私。Spring Boot作为Java领域的一款热门微服务框架,以其简洁的配置和快速...
**Spring Boot 整合 Shiro 知识点详解** 在现代Web开发中,Spring Boot以其简洁的配置和强大的功能成为了主流框架。同时,对于权限管理,Apache Shiro提供了灵活且易于使用的安全解决方案。本项目"shiroredis.zip...
n 技术详解-切入点表达式详解 l Spring JDBC n 基础应用-JdbcTemplate的使用 n 源码分析-自定义JdbcTemplate n 设计模式-RowMapper的策略模式 n 高级应用-NamedParameterJdbcTemplate的使用 n 源码分析-...
【Java工程师简历模板详解】 在IT行业中,一份优秀的简历是获得理想职位的关键。对于薪资30K的Java工程师,他们的简历通常需要展示出深厚的技术底蕴和丰富的项目经验。以下是基于提供的简历内容,对Java工程师应...
### JAVA面试题知识点详解 #### 1. Redis的数据类型 - **知识点**: Redis支持多种数据类型,主要包括String(字符串)、Hash(散列)、List(列表)、Set(集合)以及Sorted Set(有序集合)。这些数据类型提供了...
- **Consumer-Filter**:展示了如何实现自定义过滤器并在客户端使用。 **3.2 Hello World Demo详解** - **服务提供者**:创建服务接口和实现类,使用`@Service`注解标注实现类,启动时向注册中心注册服务。 - **...