commit 8dfd76084d0ff0dedaa097d6fb95ef703d9db5fb
Author: barney <15270405776@163.com>
Date: Sun Mar 12 22:11:47 2023 +0800
v1.0
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..35410ca
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/compiler.xml b/.idea/compiler.xml
new file mode 100644
index 0000000..ef06c80
--- /dev/null
+++ b/.idea/compiler.xml
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/encodings.xml b/.idea/encodings.xml
new file mode 100644
index 0000000..7482feb
--- /dev/null
+++ b/.idea/encodings.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..fe66321
--- /dev/null
+++ b/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml
new file mode 100644
index 0000000..abb532a
--- /dev/null
+++ b/.idea/jarRepositories.xml
@@ -0,0 +1,20 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..ae9c995
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,14 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml
new file mode 100644
index 0000000..2b63946
--- /dev/null
+++ b/.idea/uiDesigner.xml
@@ -0,0 +1,124 @@
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+ -
+
+
+
+
+ -
+
+
+ -
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..381e630
--- /dev/null
+++ b/README.md
@@ -0,0 +1,41 @@
+### 基于SpringBoot+Thymeleaf+maven实现基于Cookie的单点登录
+
+#### 使用依赖
+
+```xml
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
+```
+
+#### 四个模块
+
+- `sso-main`: 首页
+- `sso-login`: 登录
+- `sso-cart`: 购物车
+- `sso-vip`: `vip`系统
+
+#### 接口
+
+| 接口类型 | 接口地址 |
+| --------- | -------------------------------------------- |
+| 首页 | `http://www.codeshop.com:8082/view/index` |
+| 登录 | `http://login.codeshop.com:8081/view/login` |
+| 登出 | `http://login.codeshop.com:8081/view/logout` |
+| 购物车 | `http://cart.codeshop.com:8080/view/index` |
+| `vip`系统 | `http://vip.codeshop.com:8083/view/index` |
+
+#### 单点登录
+
+三个服务: 首页、购物车、`vip`系统,有一个成功登录,则所有服务均成功登录,一个退出登录,所有的服务均需重新登录
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..6a71508
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,57 @@
+
+
+ 4.0.0
+
+ cc.bnblogs
+ use-cookie-sso
+ 1.0-SNAPSHOT
+ pom
+
+ sso-main
+ sso-cart
+ sso-vip
+ sso-login
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+
+ 2.7.9
+
+
+
+ 8
+ 8
+ UTF-8
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+ org.springframework.boot
+ spring-boot-devtools
+ runtime
+ true
+
+
+
+ org.projectlombok
+ lombok
+
+
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
+
+
+
+
\ No newline at end of file
diff --git a/sso-cart/pom.xml b/sso-cart/pom.xml
new file mode 100644
index 0000000..d7ce83b
--- /dev/null
+++ b/sso-cart/pom.xml
@@ -0,0 +1,20 @@
+
+
+ 4.0.0
+
+ cc.bnblogs
+ use-cookie-sso
+ 1.0-SNAPSHOT
+
+
+ sso-cart
+
+
+ 8
+ 8
+ UTF-8
+
+
+
\ No newline at end of file
diff --git a/sso-cart/src/main/java/cc/bnblogs/CartApplication.java b/sso-cart/src/main/java/cc/bnblogs/CartApplication.java
new file mode 100644
index 0000000..924d852
--- /dev/null
+++ b/sso-cart/src/main/java/cc/bnblogs/CartApplication.java
@@ -0,0 +1,23 @@
+package cc.bnblogs;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @description: 购物车子模块
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 15:37
+ */
+@SpringBootApplication
+public class CartApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(CartApplication.class,args);
+ }
+
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+}
diff --git a/sso-cart/src/main/java/cc/bnblogs/controller/ViewController.java b/sso-cart/src/main/java/cc/bnblogs/controller/ViewController.java
new file mode 100644
index 0000000..56253a9
--- /dev/null
+++ b/sso-cart/src/main/java/cc/bnblogs/controller/ViewController.java
@@ -0,0 +1,43 @@
+package cc.bnblogs.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.CookieValue;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.client.RestTemplate;
+import org.thymeleaf.util.StringUtils;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpSession;
+import java.util.Map;
+
+/**
+ * @description: 购物车页面
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 16:16
+ */
+@Controller
+@RequestMapping("/view")
+public class ViewController {
+ @Autowired
+ private RestTemplate restTemplate;
+
+ private final String LOGIN_INFO_ADDR = "http://login.codeshop.com:8081/login/info?token=";
+
+ @GetMapping("/index")
+ public String toIndex(@CookieValue(required = false,value = "TOKEN")Cookie cookie,
+ HttpSession httpSession) {
+ if (cookie!=null) {
+ String token = cookie.getValue();
+ if(!StringUtils.isEmpty(token)) {
+ // 访问/login/info接口时带上token请求登录用户信息
+ Map result = restTemplate.getForObject(LOGIN_INFO_ADDR + token, Map.class);
+ // 打印已登录用户信息
+// System.out.println(result);
+ httpSession.setAttribute("loginUser",result);
+ }
+ }
+ return "index";
+ }
+}
diff --git a/sso-cart/src/main/resources/application.yml b/sso-cart/src/main/resources/application.yml
new file mode 100644
index 0000000..47fbb02
--- /dev/null
+++ b/sso-cart/src/main/resources/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8080
\ No newline at end of file
diff --git a/sso-cart/src/main/resources/templates/index.html b/sso-cart/src/main/resources/templates/index.html
new file mode 100644
index 0000000..f0f76b4
--- /dev/null
+++ b/sso-cart/src/main/resources/templates/index.html
@@ -0,0 +1,18 @@
+
+
+
+
+ 购物车
+
+
+ 这里是购物车
+
+ 已登录
+
+
+
+ 登录
+ 退出
+
+
+
\ No newline at end of file
diff --git a/sso-cart/target/classes/application.yml b/sso-cart/target/classes/application.yml
new file mode 100644
index 0000000..47fbb02
--- /dev/null
+++ b/sso-cart/target/classes/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8080
\ No newline at end of file
diff --git a/sso-cart/target/classes/cc/bnblogs/CartApplication.class b/sso-cart/target/classes/cc/bnblogs/CartApplication.class
new file mode 100644
index 0000000..50d4610
Binary files /dev/null and b/sso-cart/target/classes/cc/bnblogs/CartApplication.class differ
diff --git a/sso-cart/target/classes/cc/bnblogs/controller/ViewController.class b/sso-cart/target/classes/cc/bnblogs/controller/ViewController.class
new file mode 100644
index 0000000..c79b3c5
Binary files /dev/null and b/sso-cart/target/classes/cc/bnblogs/controller/ViewController.class differ
diff --git a/sso-cart/target/classes/templates/index.html b/sso-cart/target/classes/templates/index.html
new file mode 100644
index 0000000..f0f76b4
--- /dev/null
+++ b/sso-cart/target/classes/templates/index.html
@@ -0,0 +1,18 @@
+
+
+
+
+ 购物车
+
+
+ 这里是购物车
+
+ 已登录
+
+
+
+ 登录
+ 退出
+
+
+
\ No newline at end of file
diff --git a/sso-login/pom.xml b/sso-login/pom.xml
new file mode 100644
index 0000000..43e778f
--- /dev/null
+++ b/sso-login/pom.xml
@@ -0,0 +1,20 @@
+
+
+ 4.0.0
+
+ cc.bnblogs
+ use-cookie-sso
+ 1.0-SNAPSHOT
+
+
+ sso-login
+
+
+ 8
+ 8
+ UTF-8
+
+
+
\ No newline at end of file
diff --git a/sso-login/src/main/java/cc/bnblogs/LoginApplication.java b/sso-login/src/main/java/cc/bnblogs/LoginApplication.java
new file mode 100644
index 0000000..bd6b7ad
--- /dev/null
+++ b/sso-login/src/main/java/cc/bnblogs/LoginApplication.java
@@ -0,0 +1,23 @@
+package cc.bnblogs;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @description: 登录子模块
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 15:38
+ */
+@SpringBootApplication
+public class LoginApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(LoginApplication.class,args);
+ }
+
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+}
diff --git a/sso-login/src/main/java/cc/bnblogs/controller/LoginController.java b/sso-login/src/main/java/cc/bnblogs/controller/LoginController.java
new file mode 100644
index 0000000..5a4cf33
--- /dev/null
+++ b/sso-login/src/main/java/cc/bnblogs/controller/LoginController.java
@@ -0,0 +1,83 @@
+package cc.bnblogs.controller;
+
+import cc.bnblogs.pojo.User;
+import cc.bnblogs.utils.LoginCacheUtils;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.*;
+import org.thymeleaf.util.StringUtils;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.util.*;
+
+/**
+ * @description: 用户登录逻辑
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 15:53
+ */
+@Controller
+@RequestMapping("/login")
+public class LoginController {
+
+ // 模拟用户数据
+ private static Set dbUsers;
+
+ static {
+ dbUsers = new HashSet<>();
+ dbUsers.add(new User(1,"user1","123456"));
+ dbUsers.add(new User(2,"user2","123456"));
+ dbUsers.add(new User(3,"user3","123456"));
+ dbUsers.add(new User(4,"user4","123456"));
+ dbUsers.add(new User(5,"user5","123456"));
+ dbUsers.add(new User(6,"user6","123456"));
+ }
+ @PostMapping
+ public String doLogin(User user, HttpSession httpSession, HttpServletResponse httpServletResponse) {
+ String target = (String) httpSession.getAttribute("target");
+
+ // 模拟数据库根据用户名和密码查询该用户是否存在
+ Optional first = dbUsers.stream().filter(dbUser -> dbUser.getUsername().equals(user.getUsername()) &&
+ dbUser.getPassword().equals(user.getPassword())).findFirst();
+
+ // 如果该用户存在
+ if (first.isPresent()) {
+ // 保存用户登录信息
+ // 生成一个token作为key
+ String token = UUID.randomUUID().toString();
+ // 将token存入cookie
+ Cookie cookie = new Cookie("TOKEN",token);
+ // 设置相同的域
+ cookie.setDomain("codeshop.com");
+ // 将cookie通过响应返回
+ httpServletResponse.addCookie(cookie);
+
+ LoginCacheUtils.loginUser.put(token,first.get());
+ }
+ else {
+ // 返回登录失败界面,可选择重新登录
+ httpSession.setAttribute("msg","用户名或密码错误");
+ return "login";
+ }
+ // 重定向到登录前的地址
+ return "redirect:" + target;
+ }
+
+ /**
+ * 根据请求中的token决定是否返回用户数据
+ * @param token 请求方提供访问数据的token
+ * @return 返回响应结果
+ */
+ @GetMapping("info")
+ @ResponseBody
+ public ResponseEntity getUserInfo(String token) {
+ if (!StringUtils.isEmpty(token)) {
+ User user = LoginCacheUtils.loginUser.get(token);
+ return ResponseEntity.ok(user);
+ } else {
+ return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
+ }
+ }
+}
diff --git a/sso-login/src/main/java/cc/bnblogs/controller/ViewController.java b/sso-login/src/main/java/cc/bnblogs/controller/ViewController.java
new file mode 100644
index 0000000..ebf5ddb
--- /dev/null
+++ b/sso-login/src/main/java/cc/bnblogs/controller/ViewController.java
@@ -0,0 +1,59 @@
+package cc.bnblogs.controller;
+
+import cc.bnblogs.pojo.User;
+import cc.bnblogs.utils.LoginCacheUtils;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.CookieValue;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.thymeleaf.util.StringUtils;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+/**
+ * @description: 页面跳转逻辑
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 15:55
+ */
+@Controller
+@RequestMapping("/view")
+
+public class ViewController {
+ @GetMapping("/login")
+ public String toLogin(@RequestParam(required = false,defaultValue = "") String target,
+ HttpSession httpSession,
+ @CookieValue(required = false, value = "TOKEN") Cookie cookie) {
+ if (StringUtils.isEmpty(target)) {
+ target = "http://www.codeshop.com:8082/view/index/";
+ }
+
+ // 如果用户已经成功登录,则不需要重复登录,直接重定向到页面
+ // 从token中获取到用户信息
+ if (cookie!=null) {
+ String token = cookie.getValue();
+ User user = LoginCacheUtils.loginUser.get(token);
+ if (user != null) {
+ return "redirect:" + target;
+ }
+ }
+
+ // todo: 注意这里没有做地址校验
+ httpSession.setAttribute("target",target);
+ return "login";
+ }
+
+ /**
+ *退出账户,将cookie设置为已过期
+ *删除已登录的用户
+ */
+ @GetMapping("/logout")
+ public String loginOut(@CookieValue(value = "TOKEN") Cookie cookie, HttpServletResponse response, String target) {
+ cookie.setMaxAge(0);
+ LoginCacheUtils.loginUser.remove(cookie.getValue());
+ response.addCookie(cookie);
+ return "redirect:" + target;
+ }
+}
diff --git a/sso-login/src/main/java/cc/bnblogs/pojo/User.java b/sso-login/src/main/java/cc/bnblogs/pojo/User.java
new file mode 100644
index 0000000..c2fcbfc
--- /dev/null
+++ b/sso-login/src/main/java/cc/bnblogs/pojo/User.java
@@ -0,0 +1,23 @@
+package cc.bnblogs.pojo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import lombok.experimental.Accessors;
+
+/**
+ * @description: 用户实体
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 15:51
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+// 使用链式调用
+@Accessors(chain = true)
+public class User {
+ private Integer id;
+ private String username;
+ private String password;
+
+}
diff --git a/sso-login/src/main/java/cc/bnblogs/utils/LoginCacheUtils.java b/sso-login/src/main/java/cc/bnblogs/utils/LoginCacheUtils.java
new file mode 100644
index 0000000..ed6c6cb
--- /dev/null
+++ b/sso-login/src/main/java/cc/bnblogs/utils/LoginCacheUtils.java
@@ -0,0 +1,15 @@
+package cc.bnblogs.utils;
+
+import cc.bnblogs.pojo.User;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @description:
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 17:42
+ */
+public class LoginCacheUtils {
+ public static Map loginUser = new HashMap<>();
+}
diff --git a/sso-login/src/main/resources/application.yml b/sso-login/src/main/resources/application.yml
new file mode 100644
index 0000000..54b155f
--- /dev/null
+++ b/sso-login/src/main/resources/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8081
\ No newline at end of file
diff --git a/sso-login/src/main/resources/templates/login.html b/sso-login/src/main/resources/templates/login.html
new file mode 100644
index 0000000..09276e3
--- /dev/null
+++ b/sso-login/src/main/resources/templates/login.html
@@ -0,0 +1,23 @@
+
+
+
+
+ 登录页
+
+
+
+
+ 欢迎来到登录页面
+
+
+
+
+
\ No newline at end of file
diff --git a/sso-login/target/classes/application.yml b/sso-login/target/classes/application.yml
new file mode 100644
index 0000000..54b155f
--- /dev/null
+++ b/sso-login/target/classes/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8081
\ No newline at end of file
diff --git a/sso-login/target/classes/cc/bnblogs/LoginApplication.class b/sso-login/target/classes/cc/bnblogs/LoginApplication.class
new file mode 100644
index 0000000..4fc7380
Binary files /dev/null and b/sso-login/target/classes/cc/bnblogs/LoginApplication.class differ
diff --git a/sso-login/target/classes/cc/bnblogs/controller/LoginController.class b/sso-login/target/classes/cc/bnblogs/controller/LoginController.class
new file mode 100644
index 0000000..17613eb
Binary files /dev/null and b/sso-login/target/classes/cc/bnblogs/controller/LoginController.class differ
diff --git a/sso-login/target/classes/cc/bnblogs/controller/ViewController.class b/sso-login/target/classes/cc/bnblogs/controller/ViewController.class
new file mode 100644
index 0000000..754ecba
Binary files /dev/null and b/sso-login/target/classes/cc/bnblogs/controller/ViewController.class differ
diff --git a/sso-login/target/classes/cc/bnblogs/pojo/User.class b/sso-login/target/classes/cc/bnblogs/pojo/User.class
new file mode 100644
index 0000000..466fd69
Binary files /dev/null and b/sso-login/target/classes/cc/bnblogs/pojo/User.class differ
diff --git a/sso-login/target/classes/cc/bnblogs/utils/LoginCacheUtils.class b/sso-login/target/classes/cc/bnblogs/utils/LoginCacheUtils.class
new file mode 100644
index 0000000..f66cb83
Binary files /dev/null and b/sso-login/target/classes/cc/bnblogs/utils/LoginCacheUtils.class differ
diff --git a/sso-login/target/classes/templates/login.html b/sso-login/target/classes/templates/login.html
new file mode 100644
index 0000000..09276e3
--- /dev/null
+++ b/sso-login/target/classes/templates/login.html
@@ -0,0 +1,23 @@
+
+
+
+
+ 登录页
+
+
+
+
+ 欢迎来到登录页面
+
+
+
+
+
\ No newline at end of file
diff --git a/sso-main/pom.xml b/sso-main/pom.xml
new file mode 100644
index 0000000..9cc0835
--- /dev/null
+++ b/sso-main/pom.xml
@@ -0,0 +1,20 @@
+
+
+ 4.0.0
+
+ cc.bnblogs
+ use-cookie-sso
+ 1.0-SNAPSHOT
+
+
+ sso-main
+
+
+ 8
+ 8
+ UTF-8
+
+
+
\ No newline at end of file
diff --git a/sso-main/src/main/java/cc/bnblogs/MainApplication.java b/sso-main/src/main/java/cc/bnblogs/MainApplication.java
new file mode 100644
index 0000000..5a172b4
--- /dev/null
+++ b/sso-main/src/main/java/cc/bnblogs/MainApplication.java
@@ -0,0 +1,28 @@
+package cc.bnblogs;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @description: 网站首页
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 15:40
+ */
+@SpringBootApplication
+public class MainApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(MainApplication.class,args);
+ }
+
+ /**
+ * RestTemplate是由Spring框架提供的一个可用于应用中调用rest服务的类它简化了与http服务的通信方式
+ * 需要手动将RestTemplate注入到Bean容器中
+ * @return
+ */
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+}
diff --git a/sso-main/src/main/java/cc/bnblogs/controller/ViewController.java b/sso-main/src/main/java/cc/bnblogs/controller/ViewController.java
new file mode 100644
index 0000000..ec1e13c
--- /dev/null
+++ b/sso-main/src/main/java/cc/bnblogs/controller/ViewController.java
@@ -0,0 +1,48 @@
+package cc.bnblogs.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.CookieValue;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.client.RestTemplate;
+import org.thymeleaf.util.StringUtils;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpSession;
+import java.util.Map;
+
+
+/**
+ * @description: 网站主页
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 16:12
+ */
+@Controller
+@RequestMapping("/view")
+public class ViewController {
+
+ @Autowired
+ private RestTemplate restTemplate;
+
+ private final String LOGIN_INFO_ADDR = "http://login.codeshop.com:8081/login/info?token=";
+ @GetMapping("/index")
+ /**
+ * 跳转到index.html
+ * 没有登录的时候没有cookie值,所以required要设为false
+ */
+ public String toIndex(@CookieValue(required = false,value = "TOKEN")Cookie cookie,
+ HttpSession httpSession) {
+ if (cookie != null) {
+ String token = cookie.getValue();
+ if (!StringUtils.isEmpty(token)) {
+ // 访问/login/info接口时带上token请求登录用户信息
+ Map result = restTemplate.getForObject(LOGIN_INFO_ADDR + token,
+ Map.class);
+ // 保存登录用户
+ httpSession.setAttribute("loginUser",result);
+ }
+ }
+ return "index";
+ }
+}
diff --git a/sso-main/src/main/resources/application.yml b/sso-main/src/main/resources/application.yml
new file mode 100644
index 0000000..0884131
--- /dev/null
+++ b/sso-main/src/main/resources/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8082
\ No newline at end of file
diff --git a/sso-main/src/main/resources/templates/index.html b/sso-main/src/main/resources/templates/index.html
new file mode 100644
index 0000000..ea9ef08
--- /dev/null
+++ b/sso-main/src/main/resources/templates/index.html
@@ -0,0 +1,23 @@
+
+
+
+
+ 首页
+
+
+ 欢迎来到codeshop首页
+
+ 已登录
+
+
+
+ 登录
+ 退出
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/sso-main/target/classes/application.yml b/sso-main/target/classes/application.yml
new file mode 100644
index 0000000..0884131
--- /dev/null
+++ b/sso-main/target/classes/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8082
\ No newline at end of file
diff --git a/sso-main/target/classes/cc/bnblogs/MainApplication.class b/sso-main/target/classes/cc/bnblogs/MainApplication.class
new file mode 100644
index 0000000..c265367
Binary files /dev/null and b/sso-main/target/classes/cc/bnblogs/MainApplication.class differ
diff --git a/sso-main/target/classes/cc/bnblogs/controller/ViewController.class b/sso-main/target/classes/cc/bnblogs/controller/ViewController.class
new file mode 100644
index 0000000..e01f8cc
Binary files /dev/null and b/sso-main/target/classes/cc/bnblogs/controller/ViewController.class differ
diff --git a/sso-main/target/classes/templates/index.html b/sso-main/target/classes/templates/index.html
new file mode 100644
index 0000000..ea9ef08
--- /dev/null
+++ b/sso-main/target/classes/templates/index.html
@@ -0,0 +1,23 @@
+
+
+
+
+ 首页
+
+
+ 欢迎来到codeshop首页
+
+ 已登录
+
+
+
+ 登录
+ 退出
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/sso-vip/pom.xml b/sso-vip/pom.xml
new file mode 100644
index 0000000..387c7d3
--- /dev/null
+++ b/sso-vip/pom.xml
@@ -0,0 +1,20 @@
+
+
+ 4.0.0
+
+ cc.bnblogs
+ use-cookie-sso
+ 1.0-SNAPSHOT
+
+
+ sso-vip
+
+
+ 8
+ 8
+ UTF-8
+
+
+
\ No newline at end of file
diff --git a/sso-vip/src/main/java/cc/bnblogs/VipApplication.java b/sso-vip/src/main/java/cc/bnblogs/VipApplication.java
new file mode 100644
index 0000000..3823da7
--- /dev/null
+++ b/sso-vip/src/main/java/cc/bnblogs/VipApplication.java
@@ -0,0 +1,22 @@
+package cc.bnblogs;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * @description:
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 15:42
+ */
+@SpringBootApplication
+public class VipApplication {
+ public static void main(String[] args) {
+ SpringApplication.run(VipApplication.class,args);
+ }
+ @Bean
+ public RestTemplate restTemplate() {
+ return new RestTemplate();
+ }
+}
diff --git a/sso-vip/src/main/java/cc/bnblogs/controller/ViewController.java b/sso-vip/src/main/java/cc/bnblogs/controller/ViewController.java
new file mode 100644
index 0000000..aec7dd6
--- /dev/null
+++ b/sso-vip/src/main/java/cc/bnblogs/controller/ViewController.java
@@ -0,0 +1,40 @@
+package cc.bnblogs.controller;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.CookieValue;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.client.RestTemplate;
+import org.thymeleaf.util.StringUtils;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpSession;
+import java.util.Map;
+
+/**
+ * @description:
+ * @author: zfp@bnblogs.cc
+ * @date: 2023/3/12 16:17
+ */
+@Controller
+@RequestMapping("/view")
+public class ViewController {
+ @Autowired
+ private RestTemplate restTemplate;
+
+ private final String LOGIN_INFO_ADDR = "http://login.codeshop.com:8081/login/info?token=";
+
+ @GetMapping("/index")
+ public String toIndex(@CookieValue(required = false,value = "TOKEN")Cookie cookie,
+ HttpSession httpSession) {
+ if (cookie != null) {
+ String token = cookie.getValue();
+ if (!StringUtils.isEmpty(token)) {
+ Map result = restTemplate.getForObject(LOGIN_INFO_ADDR + token, Map.class);
+ httpSession.setAttribute("loginUser",result);
+ }
+ }
+ return "index";
+ }
+}
diff --git a/sso-vip/src/main/resources/application.yml b/sso-vip/src/main/resources/application.yml
new file mode 100644
index 0000000..9899989
--- /dev/null
+++ b/sso-vip/src/main/resources/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8083
\ No newline at end of file
diff --git a/sso-vip/src/main/resources/templates/index.html b/sso-vip/src/main/resources/templates/index.html
new file mode 100644
index 0000000..bfdea56
--- /dev/null
+++ b/sso-vip/src/main/resources/templates/index.html
@@ -0,0 +1,18 @@
+
+
+
+
+ vip系统
+
+
+ 这里是Vip系统
+
+ 已登录
+
+
+
+ 登录
+ 退出
+
+
+
\ No newline at end of file
diff --git a/sso-vip/target/classes/application.yml b/sso-vip/target/classes/application.yml
new file mode 100644
index 0000000..9899989
--- /dev/null
+++ b/sso-vip/target/classes/application.yml
@@ -0,0 +1,2 @@
+server:
+ port: 8083
\ No newline at end of file
diff --git a/sso-vip/target/classes/cc/bnblogs/VipApplication.class b/sso-vip/target/classes/cc/bnblogs/VipApplication.class
new file mode 100644
index 0000000..bc7e9ff
Binary files /dev/null and b/sso-vip/target/classes/cc/bnblogs/VipApplication.class differ
diff --git a/sso-vip/target/classes/cc/bnblogs/controller/ViewController.class b/sso-vip/target/classes/cc/bnblogs/controller/ViewController.class
new file mode 100644
index 0000000..3049058
Binary files /dev/null and b/sso-vip/target/classes/cc/bnblogs/controller/ViewController.class differ
diff --git a/sso-vip/target/classes/templates/index.html b/sso-vip/target/classes/templates/index.html
new file mode 100644
index 0000000..bfdea56
--- /dev/null
+++ b/sso-vip/target/classes/templates/index.html
@@ -0,0 +1,18 @@
+
+
+
+
+ vip系统
+
+
+ 这里是Vip系统
+
+ 已登录
+
+
+
+ 登录
+ 退出
+
+
+
\ No newline at end of file