springcloud微服务基于redis集群的单点登录
springcloud微服务基于redis集群的单点登录
yls
2019-9-23
简介
本文介绍微服务架构中如何实现单点登录功能
创建三个服务:
- 操作redis集群的服务,用于多个服务之间共享数据
- 统一认证中心服务,用于整个系统的统一登录认证
- 服务消费者,用于测试单点登录
大体思路:每个服务都设置一个拦截器检查cookie中是否有token,若有token,则放行,若没有token,重定向到统一认证中心服务进行登录,登录成功后返回到被拦截的服务。
搭建redis集群服务
搭建统一认证中心
- 主函数添加注解
/**
* 单点登录既要注册到服务注册中心,又要向redis服务系统获取鼓舞
* 所以要添加 @EnableDiscoveryClient @EnableEurekaClient 两个注解
*
*/
@EnableDiscoveryClient
@EnableEurekaClient
@EnableFeignClients
@MapperScan(basePackages = "com.example.itokenservicesso.mapper")
@SpringBootApplication
public class ItokenServiceSsoApplication {
public static void main(String[] args) {
SpringApplication.run(ItokenServiceSsoApplication.class, args);
}
}
- 消费redis服务和熔断器
@FeignClient(value = "itoken-service-redis", fallback = RedisServiceFallBack.class)
public interface RedisService {
@PostMapping(value = "put")
public String put(@RequestParam(value = "key") String key, @RequestParam(value = "value") String value, @RequestParam(value = "seconds") long seconds);
@GetMapping(value = "get")
public String get(@RequestParam(value = "key") String key);
}
@Component
public class RedisServiceFallBack implements RedisService {
@Override
public String put(String key, String value, long seconds) {
return FallBack.badGateWay();
}
@Override
public String get(String key) {
return FallBack.badGateWay();
}
}
public class FallBack {
public static String badGateWay(){
try {
return JsonUtil.objectToString(ResultUtil.error(502,"内部错误"));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
}
- 登录服务
@Service
public class LoginServiceImpl implements LoginService {
@Autowired
private UserMapper userMapper;
@Autowired
private RedisService redisService;
@Override
public User login(String loginCode, String plantPassword) {
//从缓存中获取登录用户的数据
String json = redisService.get(loginCode);
User user = null;
//如果缓存中没有数据,从数据库取数据
if (json == null) {
user = userMapper.selectAll(loginCode);
String passwordMd5 = DigestUtils.md5DigestAsHex(plantPassword.getBytes());
if (user != null && passwordMd5.equals(user.getPassword())) {
//登录成功,刷新缓存
try {
redisService.put(loginCode, JsonUtil.objectToString(user), 60 * 60 * 24);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return user;
} else {
return null;
}
}
//如果缓存中有数据
else {
try {
user = JsonUtil.stringToObject(json, User.class);
} catch (IOException e) {
e.printStackTrace();
}
}
return user;
}
}
contoller层,处理登录业务和登录跳转
登录业务
/**
* 登录业务
*
* @param loginCode
* @param password
* @return
*/
@PostMapping("login")
public String login(String loginCode,
String password,
@RequestParam(required = false) String url,
HttpServletRequest request,
HttpServletResponse response,
RedirectAttributes redirectAttributes) {
User user = loginService.login(loginCode, password);
//登录成功
if (user != null) { String token = UUID.randomUUID().toString();
//将token放入缓存
String result = redisService.put(token, loginCode, 60 * 60 * 24);
//如果redisService没有熔断,也就是返回ok,才能执行
if (result != null && result.equals("ok")) {
CookieUtil.setCookie(response, "token", token, 60 * 60 * 24);
if (url != null && !url.trim().equals(""))
return "redirect:" + url;
}
//熔断后返回错误提示
else {
redirectAttributes.addFlashAttribute("message", "服务器异常");
} }
//登录失败
else {
redirectAttributes.addFlashAttribute("message", "用户名或密码错误");
}
return "redirect:/login";
}
登录跳转
@Autowired
private LoginService loginService; @Autowired
private RedisService redisService; /**
* 跳转登录页
*/
@GetMapping("login")
public String login(HttpServletRequest request,
Model model,
@RequestParam(required = false) String url
) {
String token = CookieUtil.getCookie(request, "token");
//token不为空可能已登录,从redis获取账号
if (token != null && token.trim().length() != 0) {
String loginCode = redisService.get(token);
//如果账号不为空,从redis获取该账号的个人信息
if (loginCode != null && loginCode.trim().length() != 0) {
String json = redisService.get(loginCode);
if (json != null && json.trim().length() != 0) {
try {
User user = JsonUtil.stringToObject(json, User.class); //已登录
if (user != null) {
if (url != null && url.trim().length() != 0) {
return "redirect:" + url;
}
}
//将登录信息传到登录页
model.addAttribute("user", user); } catch (IOException e) {
e.printStackTrace();
} }
}
}
return "login";
}
搭建服务消费者:添加一个拦截器,判断token是否为空
- 拦截器
public class WebAdminInterceptor implements HandlerInterceptor { @Autowired
private RedisService redisService; @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = CookieUtil.getCookie(request, "token"); //token为空,一定没有登录
if (token == null || token.isEmpty()) {
response.sendRedirect("http://localhost:8503/login?url=http://localhost:8601/login");
return false;
}
return true;
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { HttpSession session = request.getSession();
User user = (User) session.getAttribute("user"); //已登陆状态
if (user != null) {
if (modelAndView != null) {
modelAndView.addObject("user", user); }
}
//未登录状态
else {
String token = CookieUtil.getCookie(request, "token");
if (token != null && !token.isEmpty()) {
String loginCode = redisService.get(token); if (loginCode != null && !loginCode.isEmpty()) {
String json = redisService.get(loginCode);
if (json != null && !json.isEmpty()) {
//已登录状态,创建局部会话
user = JsonUtil.stringToObject(json, User.class);
if (modelAndView != null) {
modelAndView.addObject("user", user);
}
request.getSession().setAttribute("user", user);
}
}
}
} //二次确认是否有用户信息
if (user == null) {
response.sendRedirect("http://localhost:8503/login?url=http://localhost:8601/login"); } } @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { }
}
- 配置拦截器
@Configuration
public class WebAdminInterceptorConfig implements WebMvcConfigurer { //将拦截器设置为Bean,在拦截其中才能使用@AutoWired注解自动注入
@Bean
WebAdminInterceptor webAdminInterceptor() {
return new WebAdminInterceptor();
} @Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(webAdminInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/static");
}
}
任意写一个接口,触发拦截器进行测试
@RequestMapping(value = {"/login"})
public String index(){
return "index";
}
springcloud微服务基于redis集群的单点登录的更多相关文章
- 庐山真面目之六微服务架构Consul集群、Ocelot网关集群和Nginx版本实现
庐山真面目之六微服务架构Consul集群.Ocelot网关集群和Nginx版本实现 一.简介 在上一篇文章<庐山真面目之五微服务架构Consul集群.Ocelot网关和Nginx版本实 ...
- 庐山真面目之七微服务架构Consul集群、Ocelot网关集群和IdentityServer4版本实现
庐山真面目之七微服务架构Consul集群.Ocelot网关集群和IdentityServer4版本实现 一.简介 在上一篇文章<庐山真面目之六微服务架构Consul集群.Ocelot网 ...
- 交付Dubbo微服务到kubernetes集群
1.基础架构 1.1.架构图 Zookeeper是Dubbo微服务集群的注册中心 它的高可用机制和k8s的etcd集群一致 java编写,需要jdk环境 1.2.节点规划 主机名 角色 ip hdss ...
- (转)实验文档2:实战交付一套dubbo微服务到kubernetes集群
基础架构 主机名 角色 ip HDSS7-11.host.com k8s代理节点1,zk1 10.4.7.11 HDSS7-12.host.com k8s代理节点2,zk2 10.4.7.12 HDS ...
- 基于redis集群实现的分布式锁,可用于秒杀商品的库存数量管理,有測试代码(何志雄)
转载请标明出处. 在分布式系统中,常常会出现须要竞争同一资源的情况,本代码基于redis3.0.1+jedis2.7.1实现了分布式锁. redis集群的搭建,请见我的另外一篇文章:<>& ...
- 基于redis集群实现的分布式锁,可用于秒杀,定时器。
在分布式系统中,经常会出现需要竞争同一资源的情况,使用redis可以实现分布式锁. 前提:redis集群已经整合项目,并且可以直接注入JedisCluster使用: @Autowired privat ...
- SpringCloud微服务(基于Eureka+Feign+Hystrix+Zuul)
一.搭建注册中心 1.1.创建一个cloud-service项目 1.2:POM文件依赖 1 <?xml version="1.0" encoding="UTF-8 ...
- 8.实战交付一套dubbo微服务到k8s集群(1)之Zookeeper部署
1.基础架构 主机名 角色 ip HDSS7-11.host.com K8S代理节点1,zk1 10.4.7.11 HDSS7-12.host.com K8S代理节点2,zk2 10.4.7.12 H ...
- 实战交付一套dubbo微服务到k8s集群(1)之Zookeeper部署
基础架构 主机名 角色 IP地址 mfyxw10.mfyxw.com K8S代理节点1,zk1 192.168.80.10 mfyxw20.mfyxw.com K8S代理节点2,zk2 192.168 ...
随机推荐
- Ubuntu和开发板用网线直连ping不通的问题
我装的Ubuntu 18.04双系统,在通过网络加载内核和文件系统那一步一直连接不上,uboot里面ping我的主机IP地址,提示: ping failed; host 192.168.1.111 i ...
- samba文件共享服务部署
1.安装samaba服务程序 yum install -y samba 2.查看smaba文件,由于注释空行较多,选择过滤 egrep -v "#|;|^$" /etc/samba ...
- Powershell基础之脚本执行
Bat 这就是我们常用的Bat脚本,全名为批处理文件,脚本中就是我们在CMD中使用到的命令,这里提一个小问题:CMD的命令行执行命令的优先级是.bat > .exe,那么假如我放一个cmd.ba ...
- 什么是IDS/IPS?
目录 摘要 0x00 基于网络的IDS和IPS0x01 设计考虑因素0X02 IDS/IPS 总结 摘要 摘要 这篇文章主要介绍的是入侵检测系统(IDS)和入侵防御系统(IPS ...
- [CODEVS6333] (数据加强)特种部队
题目描述 Description 某特种部队接到一个任务,需要潜入一个仓库.该部队士兵分为两路,第一路士兵已经在正面牵制住了敌人,第二路士兵正在悄悄地从后方秘密潜入敌人的仓库.当他们到达仓库时候,发现 ...
- Myeclipse 反编译工具插件
JadClipse是java的反编译工具,是命令行执行,反编译出来的源文件可读性较高.可惜用起来不太方便.还好 找到myeclipse下的插件,叫JadClipse,安装好之后,只要双击.class文 ...
- MySQL 分页查询优化——延迟关联优化
目录 1. InnoDB表的索引的几个概念 2. 覆盖索引和回表 3. 分页查询 4. 延迟关联优化 写在前面 下面的介绍均是在选用MySQL数据库和Innodb引擎的基础开展.我们先 ...
- 玩转OneNET物联网平台之MQTT服务④ —— 远程控制LED(设备自注册)+ Android App控制
授人以鱼不如授人以渔,目的不是为了教会你具体项目开发,而是学会学习的能力.希望大家分享给你周边需要的朋友或者同学,说不定大神成长之路有博哥的奠基石... QQ技术互动交流群:ESP8266&3 ...
- 别再让你的微服务裸奔了,基于 Spring Session & Spring Security 微服务权限控制
微服务架构 网关:路由用户请求到指定服务,转发前端 Cookie 中包含的 Session 信息: 用户服务:用户登录认证(Authentication),用户授权(Authority),用户管理(R ...
- Python字典及相关操作(内含例题)
Python字典类型 今天将会介绍一种在python中十分常见的组合数据类型——字典 通过一些实例来理解字典中的常规操作 什么是字典类型? 列表中查找是通过整数的索引(元素在列表中的序号)来实现查找功 ...