redis Summary - (1) single sign-on

Posted by Ryan Sanders on Tue, 21 May 2019 23:49:29 +0200

redis single sign-on
Recent SSO single sign-on system using redis + spring boot, summarize.
In order to satisfy the unified login authentication management of multiple subsystems.

User SSO module login, first verify that your username and password are correct, if correct.
A unique ticket will be generated. We call it user Accesstoken here. Put this user Accesstoken in redis. The way to store it is as follows.
Processing flow

#Log in. If you have logged in, return directly, or execute the logon.
stringRedisTemplate.opsForValue().set(userName, userAccesstoken,3600*24*31, TimeUnit.SECONDS);
#Authorized authentication, separate authentication of whether to log in.
stringRedisTemplate.opsForValue().get(userName);
#Exit login, check first, delete.
stringRedisTemplate.hasKey(userName);
stringRedisTemplate.delete(userName);

Common String RedisTemplate operations

#Store data in redis and set cache time
stringRedisTemplate.opsForValue().set("userName", "100",60*10,TimeUnit.SECONDS);
# val does - 1 operation 
stringRedisTemplate.boundValueOps("userName").increment(-1);
#Get the val in the cache based on key
stringRedisTemplate.opsForValue().get("userName") 
#val +1  
stringRedisTemplate.boundValueOps("userName").increment(1);
#Obtain expiration time based on key
stringRedisTemplate.getExpire("userName"); 
#Obtain expiration time according to key and convert it to designated unit
stringRedisTemplate.getExpire("userName",TimeUnit.SECONDS);
#Delete the cache according to key   
stringRedisTemplate.delete("userName");
#Check whether the key exists and return the boolean value  
stringRedisTemplate.hasKey("userName");
#Store set collections in the specified key
stringRedisTemplate.opsForSet().add("userName", "1","2","3");
#Set expiration time    
stringRedisTemplate.expire("userName",1000 , TimeUnit.MILLISECONDS);
#View the existence of specified data in the collection based on key 
stringRedisTemplate.opsForSet().isMember("userName", "1")
#Get set sets based on key 
stringRedisTemplate.opsForSet().members("userName"); 

The advantages of redis are as follows:
(1) Fast speed, because the data exists in memory.
(2) Support rich data types, support string, list, set, sorted set, hash.
(3) Supporting transactions, operations are atomic, the so-called atomicity is to change the data or all of them are executed, or all of them are not executed.
(4) Rich features: can be used for caching, messages, set expiration time by key, after expiration will be automatically deleted.
(5) redis is a single process and single thread: redis uses queue technology to change concurrent access into serial access, eliminating the overhead of traditional database serial control.
Recycling strategy of redis:
volatile-lru: Select the least recently used data from the data set (server.db[i].expires) that has set expiration times.
volatile-ttl: Select the data that will expire from the data set (server.db[i].expires) that has set expiration time.
volatile-random: Select data culling arbitrarily from a set of data sets (server.db[i].expires) that have set expiration times.
allkeys-lru: Select the least recently used data from the data set (server.db[i].dict) and eliminate it.
allkeys-random: Select data culling arbitrarily from the data set (server.db[i].dict).
no-enviction: No expulsion of data.

Topics: Redis Spring Database