Java笔记-模拟QQ三方登录(单点登录2.0)

140 篇文章 4 订阅
118 篇文章 4 订阅

本次例子模拟这样的情况:

一共有2个spring boot应用:

一个是某公司的web,第二个是QQ服务端。

某公司的应用为127.0.0.1:8082

QQ服务端为127.0.0.1:8081

演示如下:

点击使用QQ登录:

在弹出的界面输入数据点击登录后:

关键代码如下:

某公司web端:

MyController.java

package cn.it1995.login.controller;

import cn.it1995.login.util.CookieUtil;
import cn.it1995.login.util.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


@RestController
public class MyController {

    @Autowired
    private RestTemplate restTemplate;

    private static final String USER_KEY="user_key";

    private ConcurrentMap<String, Object> user = new ConcurrentHashMap<>();

    @GetMapping("/getUser")
    public Object getUser(HttpServletRequest request, HttpServletResponse response){

        String loginCookie = CookieUtil.getLoginCookie(request, response);
        Object o = user.get(loginCookie);
        return Result.success(o);
    }

    @PostMapping("/loginByQQ")
    public Object loginByQQ(String token, HttpServletResponse response, HttpServletRequest request){

        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap();
        paramMap.add("token", token);
        ResponseEntity<Object> objectResponseEntity = restTemplate.postForEntity("http://127.0.0.7:8081/getLoginInfo", paramMap, Object.class);
        Object body = objectResponseEntity.getBody();
        String uuid = CookieUtil.setLoginCookie(request, response);

        //json标准化
        String newJson = body.toString().replace("=", ":");
        System.out.println(newJson);

        Map map = JSON.parseObject(newJson, Map.class);
        Map data = JSON.parseObject(map.get("data").toString(), Map.class);
        user.put(uuid, data);
        return Result.success();
    }
}

QQ服务端:

UserController.java

package cn.it1995.qqServer.controller;

import cn.it1995.qqServer.util.JwtUtil;
import cn.it1995.qqServer.util.Result;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Controller
public class UserController {

    @ResponseBody
    @RequestMapping("/login")
    public Object login(@RequestParam("username") String username,
                        @RequestParam("password") String password){

        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        String jwt = JwtUtil.createJWT(UUID.randomUUID().toString(), JSON.toJSONString(map), 3600 * 24);
        return jwt;
    }

    @ResponseBody
    @RequestMapping("/getLoginInfo")
    public Object getLoginInfo(String token){

        String subject = JwtUtil.parseJWT(token).getSubject();
        return Result.success().data(subject);
    }

}

项目打包下载地址:

https://github.com/fengfanchen/Java/tree/master/SSODemo

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值