ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Dubbo与SSM整合(认证授权)步骤

2019-06-30 16:43:54  阅读:160  来源: 互联网

标签:Dubbo redis Value 认证 SSM springframework org import redisTemplate


1.导入web项目,创建authentication模块,并修改pom.xml文件

2.将profiles文件粘贴到authentication模块中,profiles文件夹里面有两个文件database.properties和redis.properties文件

修改database.properties中的数据库连接属性(用户名,密码,数据库名称等),修改redis.properties文件redis属性(可能修改主机地址,连接密码)

3.创建server-common模块,配置相应pom文件,并且在父pom文件中定义此模块,然后在authentication模块中引用此模块,在server-common模块的src/main/com.yootk.server.config/粘贴SpringDataRedisConfig.class文件

package com.yootk.server.config;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@PropertySource("classpath:redis.properties")
public class SpringDataRedisConfig {
    @Bean("redisConfiguration")
    public RedisStandaloneConfiguration getRedisConfiguration(
            @Value("${redis.host}") String hostName ,
            @Value("${redis.port}") int port,
            @Value("${redis.auth}") String password,
            @Value("${redis.database}") int database
    ) {
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration() ;
        configuration.setHostName(hostName); // 设置Redis主机名称
        configuration.setPort(port); // 设置Redis的访问端口
        configuration.setPassword(RedisPassword.of(password)); // 设置密码
        configuration.setDatabase(database); // 设置数据库索引
        return configuration ;
    }
    @Bean("objectPoolConfig")
    public GenericObjectPoolConfig getObjectPoolConfig(
            @Value("${redis.pool.maxTotal}") int maxTotal ,
            @Value("${redis.pool.maxIdle}") int maxIdle ,
            @Value("${redis.pool.minIdle}") int minIdle ,
            @Value("${redis.pool.testOnBorrow}") boolean testOnBorrow
    ) {
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig() ;
        poolConfig.setMaxTotal(maxTotal);
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig ;
    }
    @Bean("lettuceClientConfiguration")
    public LettuceClientConfiguration getLettuceClientConfiguration(
            @Autowired GenericObjectPoolConfig poolConfig
    ) { // 创建Lettuce组件的连接池客户端配置对象
        return LettucePoolingClientConfiguration.builder().poolConfig(poolConfig).build() ;
    }
    @Bean("redisConnectionFactory")
    public RedisConnectionFactory getConnectionFactory(
            @Autowired RedisStandaloneConfiguration redisConfiguration ,
            @Autowired LettuceClientConfiguration lettuceClientConfiguration
    ) {
        LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisConfiguration,lettuceClientConfiguration) ;
        return connectionFactory ;
    }
    @Bean("redisTemplate")
    public RedisTemplate getRedisTempalate(
            @Autowired RedisConnectionFactory connectionFactory
    ) {
        RedisTemplate<Object,Object> redisTemplate = new RedisTemplate<>() ;
        redisTemplate.setConnectionFactory(connectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer()); // 数据的key通过字符串存储
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // 保存的value为对象
        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // 数据的key通过字符串存储
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); // 保存的value为对象
        return redisTemplate ;
    }
}

  4.在server-common模块的src/main/com.yootk.server.config/粘贴DatabaseConfig.class文件,写完之后spring-database.xml配置文件就没用了,删除掉它。

package com.yootk.server.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.repository.NoRepositoryBean;
import javax.sql.DataSource;
import java.sql.SQLException;
@Configuration
@PropertySource("classpath:database.properties")
public class DatabaseConfig {
    @Value("${db.druid.driverClassName}")
    private String driverClassName ;
    @Value("${db.druid.url}")
    private String url ;
    @Value("${db.druid.username}")
    private String username ;
    @Value("${db.druid.password}")
    private String password ;
    @Value("${db.druid.maxActive}")
    private int maxActive ;
    @Value("${db.druid.minIdle}")
    private int minIdle ;
    @Value("${db.druid.initialSize}")
    private int initialSize ;
    @Value("${db.druid.maxWait}")
    private long maxWait ;
    @Value("${db.druid.timeBetweenEvictionRunsMillis}")
    private long timeBetweenEvictionRunsMillis ;
    @Value("${db.druid.minEvictableIdleTimeMillis}")
    private long minEvictableIdleTimeMillis ;
    @Value("${db.druid.validationQuery}")
    private String validationQuery ;
    @Value("${db.druid.testWhileIdle}")
    private boolean testWhileIdle ;
    @Value("${db.druid.testOnBorrow}")
    private boolean testOnBorrow ;
    @Value("${db.druid.testOnReturn}")
    private boolean testOnReturn ;

    @Value("${db.druid.poolPreparedStatements}")
    private boolean poolPreparedStatements ;
    @Value("${db.druid.maxPoolPreparedStatementPerConnectionSize}")
    private int maxPoolPreparedStatementPerConnectionSize ;
    @Value("${db.druid.filters}")
    private String filters ;
    @Bean("dataSource")
    public DataSource getDruidDataSource() {
        DruidDataSource dataSource = new DruidDataSource() ;
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setUrl(this.url);
        dataSource.setUsername(this.username);
        dataSource.setPassword(this.password);
        dataSource.setMaxActive(this.maxActive);
        dataSource.setMinIdle(this.minIdle);
        dataSource.setInitialSize(this.initialSize);
        dataSource.setMaxWait(this.maxWait);
        dataSource.setTimeBetweenEvictionRunsMillis(this.timeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(this.minEvictableIdleTimeMillis);
        dataSource.setValidationQuery(this.validationQuery);
        dataSource.setTestWhileIdle(this.testWhileIdle);
        dataSource.setTestOnBorrow(this.testOnBorrow);
        dataSource.setTestOnReturn(this.testOnReturn);
        dataSource.setPoolPreparedStatements(this.poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(this.maxPoolPreparedStatementPerConnectionSize);
        try {
            dataSource.setFilters(this.filters);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dataSource ;
    }

}

  5.在server-common模块的src/main/com.yootk.server.cache目录下粘贴RedisCache.java文件

package com.yootk.server.cache;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.Callable;

public class RedisCache implements Cache {
    private RedisTemplate<String,Object> redisTemplate ;
    private String name ; // 缓存名称
    // 此时进行的Redis缓存操作实现类型需要通过配置文件的形式完成
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return this.name;
    }
    @Override
    public Object getNativeCache() {
        return this.redisTemplate;
    }
    @Override
    public ValueWrapper get(Object o) {
        Object result = this.redisTemplate.opsForValue().get(String.valueOf(o)) ;
        if (result != null) {   // 已经查找到相应数据
            return new SimpleValueWrapper(result) ;
        }
        return null;
    }
    @Override
    public <T> T get(Object o, Class<T> aClass) {
        Object result = this.redisTemplate.opsForValue().get(String.valueOf(o)) ;
        if (result != null) {   // 已经查找到相应数据
            return (T) result ;
        }
        return null;
    }
    @Override
    public <T> T get(Object o, Callable<T> callable) {
        return null;
    }
    @Override
    public void put(Object key, Object value) {
        this.redisTemplate.opsForValue().set(String.valueOf(key),value);
    }
    @Override
    public ValueWrapper putIfAbsent(Object key, Object value) {
        Object result = this.redisTemplate.opsForValue().get(String.valueOf(key)) ;
        if (result == null) {
            this.redisTemplate.opsForValue().set(String.valueOf(key),value);
            return new SimpleValueWrapper(value) ;
        }
        return new SimpleValueWrapper(result);
    }
    @Override
    public void evict(Object o) {
        this.redisTemplate.delete(String.valueOf(o)) ;
    }
    @Override
    public void clear() {
        this.redisTemplate.getConnectionFactory().getConnection().flushDb();
    }
}

  6.

标签:Dubbo,redis,Value,认证,SSM,springframework,org,import,redisTemplate
来源: https://www.cnblogs.com/wxl123/p/11110260.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有