springboot实现单点登入

296 阅读1分钟

传统的单点登入模式是将信息储存在cookie里,这样缺点是,cookie范围通常是域,必须在同一个域名下,并且因为cookie存储在浏览器,相对来说不太安全,而且不能跨语言,java和php不是同一个cookie。

全称single sing on,SSO,指在一个公用的系统来实现所有的系统登入为单点登入。

 

一、首先需要定义SSOcheck工具类:里面判断登入。

private static String SSOname = "123";
    private static String SSOpassword = "333";

    private static String SSOCookie = "ssoname";
    private static String CookieValues = "sso";

    public static boolean checkLogin(String name, String password) {
        if (name.equals(SSOname) && password.equals(SSOpassword)) {
            return true;
        } else {
            return false;
        }
    }

二、来一个登入页面:


<body>
    <span>账号:</span><input type="text" name="name" id="name">
    <span>密码:</span><input type="text" name="password" id="password">
    <button type="button" id="btn">登入</button>
    <input type="hidden" id="gotoUrl" name="gotoUrl" value="${gotoUrl}">
</body>

<script type="text/javascript">
    $(function(){

    })
    $("#btn").click(function () {
        var name = $("#name").val();
        var password = $("#password").val();
        var gotoUrl = $("#gotoUrl").val();
        $.ajax({
            url:'/indexOneOne',
            type:'POST',
            data:{
                name:name,
                password:password,
                gotoUrl:gotoUrl
            },
            success:function (data) {
                if(data.flag){
                    var gotoUrl = data.gotoUrl;
                    window.location.href="/"+gotoUrl;
                }else{
                    window.location.href="/login";
                }
            }
        })
    })
</script>

三、建两个登入页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>这是单点登入ONE1</h1>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>这是单点登入Two2</h1>
</body>
</html>
 /*
    * 账号密码登入
    * */
    @RequestMapping("/loginOne")
    public ModelAndView login(HttpServletRequest request) {
       /* User user = new User("zhangsan",1);
        request.getSession().setAttribute("name",user);
        System.out.println("登入");*/
        ModelAndView mv = new ModelAndView();
        mv.setViewName("login");
        return mv;
    }

    /*
    * 成功进入登入页面1
    * */
    @RequestMapping("/indexOne")
    public ModelAndView indexOne() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("indexOne");
        return mv;
    }

    /*
     * 成功进入登入页面2
     * */
    @RequestMapping("/indexTwoGo")
    public ModelAndView indexTwoGo() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("indexTwo");
        return mv;
    }

    @RequestMapping("/indexOneOne")
    @ResponseBody
    public Map<String,Object> index(String name, String password, String gotoUrl, HttpServletResponse response) {
        boolean ok = Ssocheck.checkLogin(name, password);
        Map<String,Object> map = new HashMap<>();
        map.put("gotoUrl",gotoUrl);
        if (ok) {
            Cookie cookie = new Cookie("ssoname","sso");
            cookie.setPath("/");
            response.addCookie(cookie);
            map.put("flag",true);
            return map;
        }
        map.put("flag",false);
        return map;
    } 
/*
    * index1
    * */
    @RequestMapping("/index1")
    public String index1(HttpServletRequest request) {
        boolean ok = Ssocheck.checkCookies(request);
        if(ok){
            return "indexOne";
        }
        request.getSession().setAttribute("gotoUrl","indexOne");
       return "login";
    }

    /*
     * index2
     * */
    @RequestMapping("/index2")
    public String index2(HttpServletRequest request) {
        boolean ok = Ssocheck.checkCookies(request);
        if(ok){
            return "indexTwoGo";
        }
        request.getSession().setAttribute("gotoUrl","indexTwo");
        return "login";
    }

四:SSOcheck里再加一个判断:

    public static boolean checkCookies(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                String SOOName = cookie.getName();
                String value = cookie.getValue();
                if(SSOCookie.equals(SOOName) && value.equals(CookieValues)){
                    return true;
                }
            }
        }
        return false;
    }