Dubbo and SSM integration (authentication and authorization) steps

Keywords: PHP Redis Druid Database Java

1. Import the web project, create the authentication module, and modify the pom.xml file

2. Paste the profiles file into the authentication module. There are two files database.properties and redis.properties in the profiles folder.

Modify the database connection properties (user name, password, database name, etc.) in database.properties, and modify the redis property in redis.properties file (maybe modify the host address, connection password)

3. Create the server common module, configure the corresponding pom file, define the module in the parent pom file, and then reference the module in the authentication module. Paste the spring dataredisconfig.class file in src/main/com.yootk.server.config/of the server common module.

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); // Set Redis host name
        configuration.setPort(port); // Set the access port of Redis
        configuration.setPassword(RedisPassword.of(password)); // Set password
        configuration.setDatabase(database); // Set database index
        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
    ) { // Create a connection pool client configuration object for the Lettuce component
        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()); // The key of data is stored by string
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // The saved value is the object
        redisTemplate.setHashKeySerializer(new StringRedisSerializer()); // The key of data is stored by string
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); // The saved value is the object
        return redisTemplate ;
    }
}

4. Paste the database config.class file in src/main/com.yootk.server.config/of the server-common module. After writing, the spring-database.xml configuration file will be useless. Delete it.

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. Paste the RedisCache.java file in the src/main/com.yootk.server.cache directory of the server-common module.

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 ; // Cache name
    // At this time, the implementation type of Redis cache operation needs to be completed in the form of configuration file.
    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) {   // Corresponding data has been found
            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) {   // Corresponding data has been found
            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.

Posted by idweb on Thu, 31 Oct 2019 20:07:50 -0700