Kaynağa Gözat

最终完成版

DUANY\DuanY 4 yıl önce
ebeveyn
işleme
72dce93c1f

+ 0 - 1
pom.xml

@@ -10,7 +10,6 @@
     <version>1.0-SNAPSHOT</version>
     <modules>
         <module>sso-login</module>
-        <module>sso-cart</module>
         <module>sso-vip</module>
         <module>sso-main</module>
     </modules>

+ 45 - 2
sso-main/src/main/java/com/sso/main/controller/ViewController.java

@@ -9,8 +9,10 @@ import org.springframework.web.servlet.i18n.CookieLocaleResolver;
 import org.thymeleaf.util.StringUtils;
 
 import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -25,20 +27,61 @@ public class ViewController {
 
     private final String LOGIN_INFO_ADDRESS="http://login.codeshop.com/login/info?token=";
 
+    /**
+     * 判断是否跳转登录
+     * @param cookie
+     * @param session
+     * @param response
+     * @return
+     * @throws IOException
+     */
     @RequestMapping("/main")
-    public String toLogin(@CookieValue(required = false,value = "TOKEN")Cookie cookie, HttpSession session){
+    public String toLogin(@CookieValue(required = false,value = "TOKEN")Cookie cookie, HttpSession session,HttpServletResponse response) throws IOException {
+        //如果有cookie,进入界面
         if (cookie!=null){
             String token=cookie.getValue();
             if(!StringUtils.isEmpty(token)){
+                //获取用户数据
                 Map result = restTemplate.getForObject(LOGIN_INFO_ADDRESS + token, Map.class);
                 System.out.println(token);
                 //保存用户
                 session.setAttribute("loginUser",result);
             }
+            //如果cookie没有,跳回登录界面
+        }else {
+            String url="http://login.codeshop.com/view/login?target=http://www.codeshop.com/view/main";
+            response.sendRedirect(url);
         }
         return "index";
     }
 
-
+    /**
+     * 退出用户
+     * @param cookie
+     * @param request
+     * @param response
+     * @return
+     * @throws IOException
+     */
+    @RequestMapping("/delete")
+    public String deleteLogin(@CookieValue(required = false,value = "TOKEN")Cookie cookie,HttpServletRequest request
+            ,HttpServletResponse response) throws IOException {
+        //判断是否存在cookie
+        if (cookie!=null){
+            String token=cookie.getValue();
+            if(!StringUtils.isEmpty(token)){
+                //删除cookie
+                Cookie cookie1=new Cookie("TOKEN",null);
+                cookie1.setDomain("codeshop.com");
+                cookie1.setMaxAge(0);
+                cookie1.setPath("/");
+                response.addCookie(cookie1);
+                //跳转回登录界面
+                String url="http://login.codeshop.com/view/login?target=http://www.codeshop.com/view/main";
+                response.sendRedirect(url);
+            }
+        }
+        return "index";
+    }
 }
 

+ 2 - 5
sso-main/src/main/resources/templates/index.html

@@ -9,11 +9,7 @@
 
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
-    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js">
-    
-    
-  
-    </script>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
 </head>  
 
@@ -208,6 +204,7 @@ body{
                 <a href="#">主页</a></li>
                 <li><a th:if="${session.loginUser==null}" href="http://login.codeshop.com/view/login?target=http://www.codeshop.com/view/main">登录</a></li>
                 <li><a href="http://vip.codeshop.com/view/vip">VIP</a></li>
+                <li><a th:unless="${session.loginUser==null}" href="/view/delete">退出</a></li>
             </ul>
         </div>
     </div>

BIN
sso-main/target/classes/com/sso/main/controller/ViewController.class


+ 4 - 7
sso-main/target/classes/templates/index.html

@@ -9,11 +9,7 @@
 
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
-    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js">
-    
-    
-  
-    </script>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
 
 </head>  
 
@@ -206,8 +202,9 @@ body{
             <ul class="nav navbar-nav">
                 <li class="active">
                 <a href="#">主页</a></li>
-                <li><a th:if="${session.loginUser==null}" href="http://localhost:8081/view/login?target=http://localhost:8082/view/main">登录</a></li>
-                <li><a href="http://localhost:8083/view/vip">VIP</a></li>
+                <li><a th:if="${session.loginUser==null}" href="http://login.codeshop.com/view/login?target=http://www.codeshop.com/view/main">登录</a></li>
+                <li><a href="http://vip.codeshop.com/view/vip">VIP</a></li>
+                <li><a th:unless="${session.loginUser==null}" href="/view/delete">退出</a></li>
             </ul>
         </div>
     </div>

+ 34 - 3
sso-vip/src/main/java/com/sso/vip/controller/ViewController.java

@@ -8,8 +8,10 @@ import org.springframework.web.client.RestTemplate;
 import org.thymeleaf.util.StringUtils;
 
 import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
+import java.io.IOException;
 import java.util.Map;
 
 /**
@@ -25,20 +27,49 @@ public class ViewController {
     private final String LOGIN_INFO_ADDRESS="http://login.codeshop.com/login/info?token=";
 
     @RequestMapping("/vip")
-    public String toLogin(@CookieValue(required = false,value = "TOKEN")Cookie cookie, HttpSession session){
+    public String toLogin(@CookieValue(required = false,value = "TOKEN")Cookie cookie, HttpSession session,HttpServletResponse response) throws IOException {
+        //如果有cookie,进入界面
         if (cookie!=null){
             String token=cookie.getValue();
             if(!StringUtils.isEmpty(token)){
+                //获取用户数据
                 Map result = restTemplate.getForObject(LOGIN_INFO_ADDRESS + token, Map.class);
                 //保存用户
                 session.setAttribute("loginUser",result);
             }
+            //如果cookie没有,跳回登录界面
+        }else {
+            String url="http://login.codeshop.com/view/login?target=http://vip.codeshop.com/view/vip";
+            response.sendRedirect(url);
         }
         return "index";
     }
 
-    @RequestMapping("/remove")
-    public String removeLogin(){
+    /**
+     * 退出数据
+     * @param cookie
+     * @param request
+     * @param response
+     * @return
+     * @throws IOException
+     */
+    @RequestMapping("/delete")
+    public String deleteLogin(@CookieValue(required = false,value = "TOKEN")Cookie cookie, HttpServletRequest request
+            , HttpServletResponse response) throws IOException {
+        if (cookie!=null){
+            String token=cookie.getValue();
+            if(!StringUtils.isEmpty(token)){
+                //删除cookie
+                Cookie cookie1=new Cookie("TOKEN",null);
+                cookie1.setDomain("codeshop.com");
+                cookie1.setMaxAge(0);
+                cookie1.setPath("/");
+                response.addCookie(cookie1);
+                //跳转回登录界面
+                String url="http://login.codeshop.com/view/login?target=http://vip.codeshop.com/view/vip";
+                response.sendRedirect(url);
+            }
+        }
         return "index";
     }
 }

+ 1 - 0
sso-vip/src/main/resources/templates/index.html

@@ -205,6 +205,7 @@ body{
                 <a href="http://www.codeshop.com/view/main">主页</a></li>
                 <li><a th:if="${session.loginUser==null}" href="http://login.codeshop.com/view/login?target=http://vip.codeshop.com/view/vip">登录</a></li>
                 <li><a href="#">VIP</a></li>
+                <li><a th:unless="${session.loginUser==null}" href="/view/delete">退出</a></li>
             </ul>
         </div>
     </div>

BIN
sso-vip/target/classes/com/sso/vip/controller/ViewController.class


+ 1 - 0
sso-vip/target/classes/templates/index.html

@@ -205,6 +205,7 @@ body{
                 <a href="http://www.codeshop.com/view/main">主页</a></li>
                 <li><a th:if="${session.loginUser==null}" href="http://login.codeshop.com/view/login?target=http://vip.codeshop.com/view/vip">登录</a></li>
                 <li><a href="#">VIP</a></li>
+                <li><a th:unless="${session.loginUser==null}" href="/view/delete">退出</a></li>
             </ul>
         </div>
     </div>