1、代码部分
1.1认证中心代码:
1.1.1Controller
@Controller public class LoginController { /** * 当你的redis数据库里面本来存的是字符串数据或者你要存取的数据就是字符串类型数据的时候,那么你就使用StringRedisTemplate即可, * 但是如果你的数据是复杂的对象类型,而取出的时候又不想做任何的数据转换,直接从Redis里面取出一个对象,那么使用RedisTemplate是 */ @Autowired StringRedisTemplate redisTemplate; @GetMapping("/login.html") public String Login(@RequestParam(value = "redirect_url",required = false) String url, Model model, @CookieValue(value ="sso_token",required = false) String sso_token){ if (!StringUtils.isEmpty(sso_token)){ return "redirect:"+url+"?token="+sso_token; }else { model.addAttribute("url",url); return "login"; } } @PostMapping("/doLogin") public String doLogin(String username, String password, String url, HttpServletResponse response){ if (username!=null&&password!=null){ //登陆成功后跳回之前页面 //把登录成功的用户存起来 String uuid = UUID.randomUUID().toString().replace("-",""); redisTemplate.opsForValue().set(uuid,username); Cookie sso_token = new Cookie("sso_token",uuid); response.addCookie(sso_token); //用token作为辨别是否已登录 return "redirect:"+url+"?token="+uuid; } return "login"; } }
1.1.2前端代码
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<form action="/doLogin" method="post">
账号:<input name="username"><br/>
密码:<input name="password" type="password"><br/>
<input type="hidden" name="url" th:value="${url}">
<input type="submit" value="登录"/>
form>
body>
html>
1.2客户端1代码
1.2.1客户端1Controller
@Controller public class HelloController { @Value("${sso.server.url}") String ssoServer; @GetMapping("/employees") public String employees(Model model, HttpSession session, @RequestParam(value = "token", required = false) String token) { Object loginUser = session.getAttribute("loginUser"); //TODO 1、去ssoserver获取当前token真正对应的用户信息 if (token != null && loginUser != null ) { Listemps = new ArrayList<>(); emps.add("张三"); emps.add("李四"); model.addAttribute("emps", emps); return "list"; } else { session.setAttribute("loginUser", "zhangsan"); return "redirect:" + ssoServer + "?redirect_url=http://clientone.com:8081/employees"; } } }
1.2.2前端代码
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>员工列表title>
head>
<body>
<h1>
欢迎:[]
h1>
<ul>
<li th:each="emp:${emps}">姓名是:[[${emp}]]li>
ul>
body>
html>
1.2.3配置文件
server.port=8081
sso.server.url = http://sso.com:8080/login.html
spring.redis.host=192.168.111.131
1.3客户端2代码
1.3.1客户端2Controller
@Controller public class HelloController { @Value("${sso.server.url}") String ssoServer; @GetMapping("/boss") public String employees(Model model, HttpSession session, @RequestParam(value = "token", required = false) String token) { Object loginUser = session.getAttribute("loginUser"); //TODO 1、去ssoserver获取当前token真正对应的用户信息 if (token != null && loginUser != null) { Listemps = new ArrayList<>(); emps.add("张三"); emps.add("李四"); model.addAttribute("emps", emps); return "list"; } else { session.setAttribute("loginUser", "zhangsan"); return "redirect:" + ssoServer + "?redirect_url=http://clientone.com:8082/boss"; } } }
前端代码和客户端1一样,配置文件更改server.port=8082即可
2、流程图部分