当前位置:网站首页>Redis realizes single sign on -- system framework construction (I)
Redis realizes single sign on -- system framework construction (I)
2022-07-26 12:46:00 【User 9919783】
Environmental requirements :redis3.8.2+maven+springBoot+jdk1.8+freemarker Templates
1、 Demand background
At home on weekends , Last colleague suddenly contacted , Let me do a single sign on function for his website , So I sort out the logic first , Realize the function and record .
Single sign in (Single sign on) seeing the name of a thing one thinks of its function , After a website logs in , Other websites do not need users to continue to enter account passwords , And can login without secret , This technology is used very frequently in large websites , For example, Alibaba , When users log in to their system , Each subsystem can log in automatically , If you log in to a treasure , Log in to a monument , You also need a password , You also need to re-enter your password to log in to Taobao , This will definitely result in a very poor user experience , And the authentication logic of the system will also be troublesome , At this time, single sign on appears .
2、 Code instance
This article demonstrates two systems
@Slf4j
@RestController
@RequestMapping("/redis/single")
public class RedisSingleController {
private static final String NAME = "name";
private static final String PASSWORD = "password";
@Resource
private RedisTemplate redisTemplate;
@GetMapping("/login_page")
public ModelAndView loginPage(){
ModelAndView mv = new ModelAndView();
mv.setViewName("loginPage");
return mv;
}
/**
* System 1
* @return
*/
@GetMapping("/system1")
public ModelAndView system1(HttpServletRequest request){
ModelAndView mv = new ModelAndView();
if(redisTemplate.hasKey("name")){
mv.setViewName("systemOnePage");
return mv;
}
request.getSession().setAttribute("goUrl","indexSystem1");
mv.setViewName("loginPage");
return mv;
}
/**
* System 2
* @return
*/
@GetMapping("/system2")
public ModelAndView system2(HttpServletRequest request){
ModelAndView mv = new ModelAndView();
if(redisTemplate.hasKey("name")){
mv.setViewName("systemTwoPage");
return mv;
}
request.getSession().setAttribute("goUrl","indexSystem2");
mv.setViewName("loginPage");
return mv;
}
/**
* Verify whether the account and password are normal
* @param name
* @param password
* @return
*/
@PostMapping("/detection")
@ResponseBody
public Map<String, Object> index( String name, String password, String gotoUrl){
Map<String, Object> map = new HashMap<>();
if(!ObjectUtil.equals(name,NAME) || !ObjectUtil.equals(password,PASSWORD)){
map.put("flag",false);
map.put("data"," Account password does not match !");
return map;
}
map.put("flag",true);
map.put("data"," Correct account password !");
//redis cache 20s invalid
redisTemplate.opsForValue().set("name",name,10, TimeUnit.SECONDS);
return map;
}
/**
* Login successful , As soon as the system enters the home page
* @return
*/
@GetMapping("/indexSystem1")
@ResponseBody
public ModelAndView index(){
ModelAndView mv = new ModelAndView();
mv.setViewName("systemOnePage");
return mv;
}
/**
* Login successful , System 2 enters the home page
* @return
*/
@GetMapping("/indexSystem2")
@ResponseBody
public ModelAndView indexSystem2(){
ModelAndView mv = new ModelAndView();
mv.setViewName("systemTwoPage");
return mv;
}1、 Ensure that when accessing the login page of the system , Enter the first homepage of the system , When visiting the login page of system 2 , Enter the homepage of system 2 .
2、 When accessing system one or system two , We need to judge redis Whether there is a current user in , If it exists, login without secret .
3、 Logical processing , Before logging in, first judge whether the account and password in the database are correct , If the user logs in correctly on the page , Then it returns login success , And put in redis, Set an expiration time for him .
4、 Guaranteed at redis Other subsystems can log in without secret within the expiration time .
边栏推荐
- 食品安全 | 微波炉什么食品都能加热?这些安全隐患要知道
- QT introduction and case explanation
- Oracle AWR report script: SQL ordered by elapsed time
- Ssj-21b time relay
- Redis master-slave replication principle
- 腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
- HCIP-9.OSPF的各种拓展
- Interviewer: how to deal with high concurrency?
- Commonly used list Why does isempty() suddenly report null?
- In the digital era, what "golden treasure" is driving the development of pharmaceutical enterprises for a century?
猜你喜欢

食品安全 | 这些常见食物小心有毒!速查自家餐桌

HCIP-9.OSPF的各种拓展

10. 509. Introduction to PKCs file format

Microsoft has shut down two attack methods: Office macro and RDP brute force cracking

什么是回调函数,对于“回”字的理解

Kubernetes----PV和PVC的生命周期简介

代码报错解决问题经验之二:YOLOv5中的test报错

Huawei ultra fusion fusioncube solution notes

A super easy-to-use artifact apifox, throw swagger a few streets... (glory Collection Edition)

食品安全 | 随便果可以”随便“吃吗?
随机推荐
如何组装一个注册中心?
手机上买股票,在哪里开户比较安全?
A super easy-to-use artifact apifox, throw swagger a few streets... (glory Collection Edition)
Ssj-21b time relay
Various extensions of hcip-9.ospf
Redis master-slave replication principle
Backtracking - 491. Incremental subsequence
结合环境光、接近传感以及红外测距的光距感芯片4530A
In the digital era, what "golden treasure" is driving the development of pharmaceutical enterprises for a century?
论文阅读-MLPD:Multi-Label Pedestrian Detector in Multispectral Domain(海康威视研究院实习项目)
.eslintrc.js配置说明
The.Net webapi uses groupname to group controllers to render the swagger UI
腾讯云与智慧产业事业群(CSIG)调整组织架构,成立数字孪生产品部
2022.7.23-----leetcode.剑指offer.115
.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI
Interview JD T5, was pressed on the ground friction, who knows what I experienced?
食品安全 | 微波炉什么食品都能加热?这些安全隐患要知道
【2243】module_ param.m
一文看懂GaitSet中的test.py
Kuzaobao: summary of Web3 encryption industry news on July 25