springboot配置redis

  • 1.pom文件
        
        
            org.springframework.boot
            spring-boot-starter-redis
            1.3.2.RELEASE
        
  • 2.application.properties文件
#redis
spring.redis.database=1
spring.redis.host=192.168.0.x
spring.redis.port=6379
spring.redis.password=123456
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
  • 3.redis配置类
package com.infinite.common.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 
* @ClassName: RedisConfig
* @Description: redisTemplate配置类
* @author chenliqiao
* @date 2018年4月9日 上午10:31:28
*
 */
@Configuration
@EnableCaching
public class RedisConfig {
    
    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate){
        CacheManager cacheManager=new RedisCacheManager(redisTemplate);
        return cacheManager;
    }
    
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        
        //更改序列化方式
        RedisSerializer stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(stringSerializer);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(stringSerializer);
        
        return redisTemplate;
    }
    
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        StringRedisTemplate stringRedisTemplate=new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringRedisTemplate;
    }

}

  • 4.RedisTemplate使用
/**
 * 
* @ClassName: SecurityService
* @Description: 系统登录鉴权业务类
* @author chenliqiao
* @date 2018年4月4日 上午11:04:03
*
 */
@Service
public class SecurityService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录成功后,缓存用户信息
     */
    private void saveLoginInfoToCache(String token)throws Exception{
        //判断是否已缓存了该用户信息
        if(this.redisTemplate.opsForValue().get(token)!=null)
            return;
        
        //通过token查询sso的用户信息
        SsoUserInfo ssoUserInfo=this.remoteService.getSsoUserInfo(token);
        //检查本地是否存在该用户信息
        UserInfo baseInfo=ssoUserInfo!=null ? this.userInfoService.findById(ssoUserInfo.getId()):null;
        if(baseInfo==null)
            throw new Exception("本地不存在该用户信息!");
        
        CurrentUserInfo curUserInfo=new CurrentUserInfo();
        //保存用户基本信息、角色信息和权限英文名集合
        curUserInfo.setBaseInfo(baseInfo);
        curUserInfo.setRoleInfo(this.roleInfoService.findById(baseInfo.getRoleId()));
        curUserInfo.setPermissionEnNames(this.permissionInfoService.findPermissionEnNamesByRoleId(baseInfo.getRoleId()));
        //缓存到redis中,并设置过期时间
        this.redisTemplate.opsForValue().set(token,JsonUtil.beanToJson(curUserInfo),Constants.CUR_USER_CACHING_EXPIRE_TIME,TimeUnit.MINUTES);
    }
    
    /**
     * 检查是否拥有访问当前方法的权限
     */
    private boolean ifOwnPermission(String currentPermissionName,String token)throws Exception{
        //从缓存中获取用户信息
        if(this.redisTemplate.opsForValue().get(token)==null)
            return false;
        CurrentUserInfo curUserInfo=JsonUtil.jsonToBean((String) this.redisTemplate.opsForValue().get(token), CurrentUserInfo.class);
        
        //根据用户信息中的权限英文集合属性进行判断
        List permissionNames=curUserInfo.getPermissionEnNames();
        if(CollectionUtils.isEmpty(permissionNames))
            return false;
        if(!permissionNames.contains(currentPermissionName))
            return false;
        return true;
    }
    

}

你可能感兴趣的:(springboot配置redis)