在上一篇文章中,用户信息通过直接查询MySQL判断,这样做效率很低,这里通过Redis进行验证
1.在conf目录下新建redis.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
          p:max-total="10" />
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:host-name="127.0.0.1" 
          p:port="6379" 
          p:use-pool="true">
        <constructor-arg ref="jedisPoolConfig"/>
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" 
          p:connection-factory-ref="jedisConnectionFactory">
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
    </bean>
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" 
          p:connection-factory-ref="jedisConnectionFactory" 
          />
    <bean id="topic" class="org.apache.activemq.broker.region.chenk.RedisPlugin">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
</beans>之后在activemq.xml的
<import resource="redis.xml"/>2.在chenk目录下添加RedisPlugin类
package org.apache.activemq.broker.region.chenk;
import org.springframework.data.redis.core.RedisTemplate;
import java.io.Serializable;
/**
 * @Author chenk
 * @create 2021/1/15 9:42
 */
public class RedisPlugin {
    private static RedisTemplate<Serializable, Object> redisTemplate;
    public RedisPlugin() {
    }
    public static RedisTemplate getRedisTemplate() {
        return redisTemplate;
    }
    public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    public static Object getByKey(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    public static Object getListByKey(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }
    public static void put(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }
}3.修改AuthBroker
将getUser方法改为
private String getUser(String username) {
    Object object = RedisPlugin.getByKey("TokenOf" + username);
    return object == null ? null : (String) object;
}