springcloud微服务基于redis集群的单点登录实现解析

前端之家收集整理的这篇文章主要介绍了springcloud微服务基于redis集群的单点登录实现解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_404_1@简介

本文介绍微服务架构中如何实现单点登录功能

创建三个服务:

  • 操作redis集群的服务,用于多个服务之间共享数据
  • 统一认证中心服务,用于整个系统的统一登录认证
  • 服务消费者,用于测试单点登录

大体思路:每个服务都设置一个拦截器检查cookie中是否有token,若有token,则放行,若没有token,重定向到统一认证中心服务进行登录登录成功后返回到被拦截的服务。

@H_404_1@搭建redis集群服务

搭建redis集群参考文档

@H_404_1@搭建统一认证中心

@H_404_1@主函数添加注解

  1. /**
  2. * 单点登录既要注册到服务注册中心,又要向redis服务系统获取鼓舞
  3. * 所以要添加 @EnableDiscoveryClient @EnableEurekaClient 两个注解
  4. *
  5. */
  6. @EnableDiscoveryClient
  7. @EnableEurekaClient
  8. @EnableFeignClients
  9. @MapperScan(basePackages = "com.example.itokenservicesso.mapper")
  10. @SpringBootApplication
  11. public class ItokenServiceSsoApplication {
  12. public static void main(String[] args) {
  13. SpringApplication.run(ItokenServiceSsoApplication.class,args);
  14. }
  15. }

@H_404_1@消费redis服务和熔断器

  1. @FeignClient(value = "itoken-service-redis",fallback = RedisServiceFallBack.class)
  2. public interface RedisService {
  3.  
  4. @PostMapping(value = "put")
  5. public String put(@RequestParam(value = "key") String key,@RequestParam(value = "value") String value,@RequestParam(value = "seconds") long seconds);
  6.  
  7. @GetMapping(value = "get")
  8. public String get(@RequestParam(value = "key") String key);
  9.  
  10. }
  1. @Component
  2. public class RedisServiceFallBack implements RedisService {
  3. @Override
  4. public String put(String key,String value,long seconds) {
  5. return FallBack.badGateWay();
  6. }
  7.  
  8. @Override
  9. public String get(String key) {
  10. return FallBack.badGateWay();
  11. }
  12. }
  1. public class FallBack {
  2.  
  3. public static String badGateWay(){
  4. try {
  5. return JsonUtil.objectToString(ResultUtil.error(502,"内部错误"));
  6. } catch (JsonProcessingException e) {
  7. e.printStackTrace();
  8. }
  9. return null;
  10. }
  11. }

@H_404_1@登录服务

  1. @Service
  2. public class LoginServiceImpl implements LoginService {
  3. @Autowired
  4. private UserMapper userMapper;
  5. @Autowired
  6. private RedisService redisService;
  7. @Override
  8. public User login(String loginCode,String plantPassword) {
  9. //从缓存中获取登录用户的数据
  10. String json = redisService.get(loginCode);
  11. User user = null;
  12. //如果缓存中没有数据,从数据库取数据
  13. if (json == null) {
  14. user = userMapper.selectAll(loginCode);
  15. String passwordMd5 = DigestUtils.md5DigestAsHex(plantPassword.getBytes());
  16. if (user != null && passwordMd5.equals(user.getPassword())) {
  17. //登录成功,刷新缓存
  18. try {
  19. redisService.put(loginCode,JsonUtil.objectToString(user),60 * 60 * 24);
  20. } catch (JsonProcessingException e) {
  21. e.printStackTrace();
  22. }
  23. return user;
  24. } else {
  25. return null;
  26. }
  27. }
  28. //如果缓存中有数据
  29. else {
  30. try {
  31. user = JsonUtil.stringToObject(json,User.class);
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. return user;
  37. }
  38. }

@H_404_1@contoller层,处理登录业务和登录跳转

@H_404_1@登录业务

  1. /**
  2. * 登录业务
  3. *
  4. * @param loginCode
  5. * @param password
  6. * @return
  7. */
  8. @PostMapping("login")
  9. public String login(String loginCode,String password,@RequestParam(required = false) String url,HttpServletRequest request,HttpServletResponse response,RedirectAttributes redirectAttributes) {
  10. User user = loginService.login(loginCode,password);
  11. //登录成功
  12. if (user != null) {
  13.  
  14. String token = UUID.randomUUID().toString();
  15. //将token放入缓存
  16. String result = redisService.put(token,loginCode,60 * 60 * 24);
  17. //如果redisService没有熔断,也就是返回ok,才能执行
  18. if (result != null && result.equals("ok")) {
  19. CookieUtil.setCookie(response,"token",token,60 * 60 * 24);
  20. if (url != null && !url.trim().equals(""))
  21. return "redirect:" + url;
  22. }
  23. //熔断后返回错误提示
  24. else {
  25. redirectAttributes.addFlashAttribute("message","服务器异常");
  26. }
  27.  
  28. }
  29. //登录失败
  30. else {
  31. redirectAttributes.addFlashAttribute("message","用户名或密码错误");
  32. }
  33. return "redirect:/login";
  34. }

@H_404_1@登录跳转

  1. @Autowired
  2. private LoginService loginService;
  3.  
  4. @Autowired
  5. private RedisService redisService;
  6.  
  7. /**
  8. * 跳转登录
  9. */
  10. @GetMapping("login")
  11. public String login(HttpServletRequest request,Model model,@RequestParam(required = false) String url
  12. ) {
  13. String token = CookieUtil.getCookie(request,"token");
  14. //token不为空可能已登录,从redis获取账号
  15. if (token != null && token.trim().length() != 0) {
  16. String loginCode = redisService.get(token);
  17. //如果账号不为空,从redis获取该账号的个人信息
  18. if (loginCode != null && loginCode.trim().length() != 0) {
  19. String json = redisService.get(loginCode);
  20. if (json != null && json.trim().length() != 0) {
  21. try {
  22. User user = JsonUtil.stringToObject(json,User.class);
  23.  
  24. //已登录
  25. if (user != null) {
  26. if (url != null && url.trim().length() != 0) {
  27. return "redirect:" + url;
  28. }
  29. }
  30. //将登录信息传到登录
  31. model.addAttribute("user",user);
  32.  
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36.  
  37. }
  38. }
  39. }
  40. return "login";
  41. }

@H_404_1@搭建服务消费者:添加一个拦截器,判断token是否为空

@H_404_1@拦截

  1. public class WebAdminInterceptor implements HandlerInterceptor {
  2. @Autowired
  3. private RedisService redisService;
  4. @Override
  5. public boolean preHandle(HttpServletRequest request,Object handler) throws Exception {
  6. String token = CookieUtil.getCookie(request,"token");
  7. //token为空,一定没有登录
  8. if (token == null || token.isEmpty()) {
  9. response.sendRedirect("http://localhost:8503/login?url=http://localhost:8601/login");
  10. return false;
  11. }
  12. return true;
  13. }
  14. @Override
  15. public void postHandle(HttpServletRequest request,Object handler,ModelAndView modelAndView) throws Exception {
  16. HttpSession session = request.getSession();
  17. User user = (User) session.getAttribute("user");
  18. //已登陆状态
  19. if (user != null) {
  20. if (modelAndView != null) {
  21. modelAndView.addObject("user",user);
  22. }
  23. }
  24. //未登录状态
  25. else {
  26. String token = CookieUtil.getCookie(request,"token");
  27. if (token != null && !token.isEmpty()) {
  28. String loginCode = redisService.get(token);
  29.  
  30. if (loginCode != null && !loginCode.isEmpty()) {
  31. String json = redisService.get(loginCode);
  32. if (json != null && !json.isEmpty()) {
  33. //已登录状态,创建局部会话
  34. user = JsonUtil.stringToObject(json,User.class);
  35. if (modelAndView != null) {
  36. modelAndView.addObject("user",user);
  37. }
  38. request.getSession().setAttribute("user",user);
  39. }
  40. }
  41. }
  42. }
  43.  
  44. //二次确认是否有用户信息
  45. if (user == null) {
  46. response.sendRedirect("http://localhost:8503/login?url=http://localhost:8601/login");
  47.  
  48. }
  49. }
  50. @Override
  51. public void afterCompletion(HttpServletRequest request,Exception ex) throws Exception {
  52.  
  53. }
  54. }

@H_404_1@配置拦截

  1. @Configuration
  2. public class WebAdminInterceptorConfig implements WebMvcConfigurer {
  3.  
  4. //将拦截器设置为Bean,在拦截其中才能使用@AutoWired注解自动注入
  5. @Bean
  6. WebAdminInterceptor webAdminInterceptor() {
  7. return new WebAdminInterceptor();
  8. }
  9.  
  10. @Override
  11. public void addInterceptors(InterceptorRegistry registry) {
  12. registry.addInterceptor(webAdminInterceptor())
  13. .addPathPatterns("/**")
  14. .excludePathPatterns("/static");
  15. }
  16. }

@H_404_1@任意写一个接口,触发拦截器进行测试

  1. @RequestMapping(value = {"/login"})
  2. public String index(){
  3. return "index";
  4. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的Java相关文章