单点登录教程(二)

单点登录教程(二)——同父域单点登录

这里写图片描述

这里写图片描述

这里写图片描述

现在需求改了,国家规定凡在手机购票了可以到广州塔和北京省博物馆。虽然广州和北京是不同城市,但是同一个国家。我们可以不用到广州塔下买票,也不用到北京去买票,但能随时随地买票。

同父域单点登录
http://guangzhouta.yanhui.com/ssm_crud1/index
http://senbowuguan.yanhui.com/ssm_crud2/index
http://checkcookie.yanhui.com/sso_crud3/doLogin

@RequestMapping(value="/doLogin")
    public ModelAndView doLogin(String gotoUrl, String username, String password,HttpServletResponse response){
        ModelAndView mav = new ModelAndView();
        boolean ok = _ssoUtil.checkLogin(username, password);
        if(ok){
            Cookie cookie = new Cookie("sso_cookie", "xxoo");
            cookie.setDomain(".yanhui.com");
            cookie.setPath("/");
            response.addCookie(cookie);
        }
        mav.setViewName("redirect:"+gotoUrl);
        return mav;
    }

    @ResponseBody
    @RequestMapping(value="/checkCookie")
    public String checkCookie(String cookieName, String cookieValue){
        boolean ok = _ssoUtil.checkCookie2(cookieName, cookieValue);
        String result="0";
        if(ok){
            result="1";
        }
        return result;
    }

doGet方法

public static String doGet(String url, String cookieName, String cookieValue){
        StringBuffer sb = new StringBuffer();
        HttpURLConnection httpURLConnection = null;
        try {
            URL urls = new URL(url+"?cookieName="+cookieName+"&cookieValue="+cookieValue);
            httpURLConnection = (HttpURLConnection) urls.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.connect();
            InputStream in = httpURLConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(isr);
            String temp = null;
            while((temp=br.readLine())!=null){
                sb.append(temp);
            }
            br.close();
            isr.close();
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(httpURLConnection!=null){
                httpURLConnection.disconnect();
            }
        }
        return sb.toString();
    }

注意:需要本文资源可关注微信号艳辉网 回复关键字‘java150’获取。
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值