springboot+vue集成shiro和redis的小理解——reids

springboot+vue集成shiro和redis的小理解

本篇在springboot+vue项目搭建的基础上进行shiro和redis的集成,写上自己的一些小理解,如有错误,望大佬们指出,谢谢!
贴上之前搭建基础的链接,来个小总结
springboot+vue之旅(一)
springboot+vue之旅(二)
springboot+vue集成shiro和redis的小理解——shiro篇

pom

<!-- shiro-->
   <dependency>
       <groupId>org.apache.shiro</groupId>
       <artifactId>shiro-spring</artifactId>
       <version>1.4.0</version>
   </dependency>
   <dependency>
       <groupId>org.crazycake</groupId>
       <artifactId>shiro-redis</artifactId>
       <version>3.2.3</version>
   </dependency>
   <!-- redis -->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-data-redis</artifactId>
   </dependency>

Redis

redis给我们的项目加上了缓存,避免项目需频繁访问数据库,导致速度过慢,配置上就没啥好说的了,在yml里上加入本机的redis的配置信息

 #配置redis信息
  redis:
    database: 0
    #服务器地址
    host: 127.0.0.1
    prot: 6379
    #密码(默认为空)
    timeout: 0
    pool:
      max-idle: 8
      min-idle: 0
      max-active: 10
      max-wait: -1

随后加入redis的配置类即可
redisConfig

package com.example.demo.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {
    /**
     * 注入 RedisConnectionFactory
     */
    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        setSerializer(redisTemplate, redisConnectionFactory);
        return redisTemplate;
    }

    /**
     * 设置数据存入 redis 的序列化方式
     *
     * @param redisTemplate
     * @param factory
     */
    private void setSerializer(RedisTemplate<String, Object> redisTemplate,
                               RedisConnectionFactory factory) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate
                .setHashValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
    }
    
   
}

redis加入后,项目在没有启动redis时启动,会报链接redis失败的错误,如果配置后不用启redis则说明哪一步搞错了,哈哈哈自行百度解决吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值