• 单点登录-同域名


    该篇文章基于cookie实现,局限性很大,仅供学习,实际项目不要这么用。

    1 首先新建项目一,项目结构如下图


    2 接着将该项目端口设为8081;

    3 接着编写一个项目1主页

    4 接着编写主页控制器,代码如下

    package com.chenyulin.controller;
    
    import com.chenyulin.util.SSOCheck;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * Created by Administrator on 2017/6/29.
     */
    @Controller
    public class Demo1Controller {
        private  String gotoUrl;
        @GetMapping("/demo1")
        public ModelAndView main(HttpServletRequest request){
            if (SSOCheck.checkCookie(request)) {
                ModelAndView m1 = new ModelAndView("Demo1");
                return m1;
            }
            //failed to login add "gotoUrl"
            gotoUrl = "/demo1";
            ModelAndView m2 = new ModelAndView("redirect:http://127.0.0.1:8080/intoLogin");
            m2.addObject("gotoUrl", gotoUrl);
            return m2;
        }
    }
    


    工具类代码如下

    package com.chenyulin.util;
    
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * Created by Administrator on 2017/6/29.
     */
    public class SSOCheck {
    
        public static boolean checkCookie(HttpServletRequest request) {
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("ssocookie")&&cookie.getValue().equals("sso")) {
                        return true;
                    }
                }
            }
    
            return false;
        }
    }

    5 然后是认证中心


    控制器代码

    package com.chenyulin.controller;
    
    import com.chenyulin.util.SSOCheck;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.ModelAndViewDefiningException;
    
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Created by Administrator on 2017/6/29.
     */
    @Controller
    public class LoginController {
        @GetMapping("/intoLogin")
        private ModelAndView intoLogin(@RequestParam String gotoUrl) {
            ModelAndView m=new ModelAndView("login");
            m.addObject("gotoUrl", gotoUrl);
            return m;
        }
        @PostMapping("login")
        private String doLogin(@RequestParam String username, @RequestParam String password, @RequestParam String gotoUrl, HttpServletResponse response) {
            boolean ok = SSOCheck.checkLogin(username, password);
            if (ok) {
                Cookie cookie = new Cookie("ssocookie", "sso");
                cookie.setPath("/");
                response.addCookie(cookie);
                return "redirect:http://127.0.0.1:8081"+gotoUrl;
            }
            return null;
        }
    }
    工具类代码

    package com.chenyulin.util;
    
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServletRequest;
    
    /**
     * Created by Administrator on 2017/6/29.
     */
    public class SSOCheck {
        private static final String USERNAME = "user";
        private static final String PASSWORD = "123";
    
        public static boolean checkLogin(String username, String password) {
            if (username != null && password != null) {
                if (username.equals(USERNAME) && password.equals(PASSWORD)) {
                    return true;
                }
            }
            return false;
        }
    
    }

    6 项目2,将项目2的端口改为8082,其它如同项目一,简单,略去
    同时启动两项目和认证中心,输入:127.0.0.1:8081/demo1启动进入项目1登录页;127.0.0.1:8082/demo2启动进入项目2登录页;任意登录一个即可实现来回穿梭。
    下面附上源码下载地址:http://download.csdn.net/detail/qwqw3333333/9884753
    本人初涉,如有不足请指正,万分感谢qq:569265915

    
    
    
    
    
    
    
    
    
    
    
    
    只有把命运掌握在自己手中,从今天起开始努力,即使暂时看不到希望,也要相信自己。因为比你牛几倍的人,依然在努力。
  • 相关阅读:
    爬虫header和cookie
    爬虫代理squid
    response对象
    pyspider中内容选择器常用方法汇总
    非阻塞 sleep
    post请求体过大导致ngx.req.get_post_args()取不到参数体的问题
    常用lua代码块
    nginx静态文件缓存的解决方案
    lua-resty-gearman模块
    非在线PDF转图片!!!
  • 原文地址:https://www.cnblogs.com/freesky168/p/14358283.html
Copyright © 2020-2023  润新知