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

说明:下面的这些配置类SpringDataRedisConfig.java,DatabaseConfig .java文件需要在spring-base.xml配置文件中定义扫描这些配置类所在的包(扫描包),然后其他类中需要使用的时候直接采用@Autoried(自动注入)注解注入即可。

  使用缓存:RedisCache .java,然后在spring-cache.xml配置文件中引入此类。

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.java文件

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.

转载于:https://www.cnblogs.com/wxl123/p/11110260.html

本课程是一门具有很强实践性质的“项目实战”课程,即“企业中台系统实战”,其中主要包含三大块核心内容,如下图所示(右键可以在新标签页中打开图片放大查看): 即主要包含以下三大块内容: ① 企业内部应用系统菜单资源和操作权限的统一管理; ② 分布式应用系统通信时的统一授权,即基于AccessToken的授权认证; ③ 分布式服务/系统通信时的两大方式(基于dubbo rpc协议和基于http协议的restful api实战)。   值得一提的是,这套中台系统由于讲解了如何统一管理企业内部各大应用系统的“菜单资源列表”、“操作权限”,故而本门课程的“代码实战”是建立在之前debug录制的“企业权限管理平台”这套课程的基础之上的,故而在这里debug建议没有项目开发基础的小伙伴可以先去学习我的那套“企业权限管理平台”的实战课程,之后再来学习我的这套中台系统的实战才不会很吃力(课程链接:)   本课程的课程大纲如下图所示(右键可以在新标签页中打开图片放大查看):   除此之外,这套“中台系统”由于统一管理了企业内部各大应用系统的“菜单资源和操作权限”以及“应用系统之间通信时的统一授权”,故而难免需要涉及到“中台系统”与“中台子系统”、“中台子系统”与“中台子系统”之间的通信(即分布式服务之间的通信),在这里我们是采用“dubbo + zookeeper”的方式加以落地实现的,详情如下图所示(右键可以在新标签页中打开图片放大查看):   而众所周知,作为一款知名以及相当流行的分布式服务调度中间件,dubbo现如今已经晋升为Apache顶级的开源项目,未来也仍将成为“分布式系统”开发实战的一大利器,如下图所示为dubbo底层核心系统架构图(右键可以在新标签页中打开图片放大查看): 而在这门“中台系统实战”的课程中,我们也将始终贯彻、落地dubbo的这一核心系统架构图,即如何将中台系统开发的服务注册/发布到注册中心zookeeper,中台子系统如何订阅/消费/调度中台系统发布在zookeeper的接口服务,中台子系统在走http协议调度通信时dubbo如何进行拦截、基于token认证接口的调用者等等,这些内容我们在课程中将一一得到代码层面的实战落地!   下图为本课程中涉及到的分布式系统/服务之间 采用“http协议restfulapi”方式通信时的Token授权认证的流程图(右键可以在新标签页中打开图片放大查看): 而不夸张地说,基于AccessToken的授权认证方式在现如今微服务、分布式时代系统与系统在通信期间最为常用的“授权方式”了,可想而知,掌握其中的流程思想是多么的重要!   以下为本门课程的部分截图(右键可以在新标签页中打开图片放大查看):     核心技术列表: 值得一提的是,由于本门课程是一门真正介绍“中台思想”以及将“中台思想”和“分布式系统开发实战”相结合落地的课程,故而在学完本门课程之后,可以掌握到的核心技术自然是相当多的。主要由SpringBoot2.0、SpringMVC、Mybatis、Dubbo、ZooKeeper、Redis、OkHttp3、Guava-Retrying重试机制、JWT(Json Web Token)、Shiro、分布式集群session共享、Lombok、StreamAPI、Dubbo-Filter以及ServiceBean等等。如下图所示(右键可以在新标签页中打开图片放大查看):
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值