当前位置:   article > 正文

springboot+vue集成shiro和redis的小理解——reids_springboot+vue+redis整合shiro

springboot+vue+redis整合shiro

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

随后加入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);
    }
    
   
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/874269
推荐阅读
相关标签
  

闽ICP备14008679号