Spring Boot implementa login de código de verificação

Primeiro, precisamos de uma tabela

Para que serve este relógio? Basta registrar quem escaneia o código. quem está logado.

Tabela User_Token

Os campos são os seguintes:

  • uuid : usado para garantir exclusividade
  • userId: quem está logado
  • hora de login: hora de login
  • createTime : O tempo de criação é usado para determinar se expirou
  • estado: se o código QR é inválido 0 válido 1 inválido

2. Quais são os papéis

Ainda precisamos analisá-lo por um tempo. Quais são as funções da lógica de negócios de escanear o código para fazer login?

  • terminal android ou terminal web WeChat: escanear código
  • Lado do PC: Digitalizado. Conecte-se
  • Servidor: Controle a situação geral e forneça interfaces.

3. Quais são as interfaces necessárias?

Existem papéis. Você também pode descobrir a interface com as coxas, certo! !

Então temos 2 interfaces!

  • Gerar interface de código QR: Gere um código QR. Há UUID no código QR.
  • Interface de confirmação de identidade: determine a identidade e determine se o código QR expirou, etc.

4. Etapas

O que essa frase dizia. Quantos passos são necessários para colocar um elefante na geladeira?

  • Abra o lado do PC. Chame a interface de geração de código QR e estabeleça um link com o servidor. Os links são vinculados usando uuid
  • Digitalize o código no terminal web do WeChat. Obtenha o uuid no código QR.
  • Após o terminal web WeChat obter o uuid. Exibe a página de login. Depois de clicar em OK, chame a API Confirm Identity.
  • Depois de confirmar a interface de identidade é passada. O servidor envia informações para o PC. Complete o login. O link está quebrado neste momento.

Tudo bem! Estes são analisados. Você deve estar pensando. . Ainda não acabou. . Não fique mais no BB. . Poste o código agora. .

Autor: senhores da audiência. Estou ensinando você a pensar?

Então comece a postar o código! Espero que você possa pensar por si mesmo quando o vir.

Cinco, código de pasta maluco

Antes de tudo, você precisa pegar o código do QR code, certo! colar!

//获取登录二维码、放入Token
@RequestMapping(value = "/getLoginQr" ,method = RequestMethod.GET)
public void createCodeImg(HttpServletRequest request, HttpServletResponse response){
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");

    response.setDateHeader("Expires", 0);
    response.setContentType("image/jpeg");

    try {
        //这里没啥操作 就是生成一个UUID插入 数据库的表里
        String uuid = userService.createQrImg();
        response.setHeader("uuid", uuid);
        // 这里是开源工具类 hutool里的QrCodeUtil 
        // 网址:http://hutool.mydoc.io/
        QrCodeUtil.generate(uuid, 300, 300, "jpg",response.getOutputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
复制代码

Existe uma interface para obter um código QR. O frontend relativo precisa ser chamado.

Ponto de conhecimento: carregue dinamicamente o fluxo de imagem e retire os parâmetros no cabeçalho

Aqui xmlhttp é usado para processamento.

Por quê?

Porque o back-end retorna um fluxo.

Então flua. É colocar o uuid no código QR. Este uuid é usado como um identificador de sessão.

Então o front-end também precisa obtê-lo. Faça uma conexão webSocket com o back-end.

Depois que alguém escaneia o código. Somente o servidor pode usar o método webSocket para notificar o front-end. Alguém escaneou o código com sucesso. Você faz o seu negócio. Molho roxo.

Portanto, para obter o uuid colocado no cabeçalho da solicitação, ele é processado por meio de xmlhttp

<div class="qrCodeImg-box" id="qrImgDiv"></div>
复制代码

js

$(document).ready(function(){
    initQrImg();
});
 
 
 function initQrImg(){
    $("#qrImgDiv").empty();

    var xmlhttp;
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET",getQrPath,true);
    xmlhttp.responseType = "blob";
    xmlhttp.onload = function(){
        console.log(this);
        uuid = this.getResponseHeader("uuid");

        if (this.status == 200) {
            var blob = this.response;
            var img = document.createElement("img");
            img.className = 'qrCodeBox-img';
            img.onload = function(e) {
                window.URL.revokeObjectURL(img.src);
            };
            img.src = window.URL.createObjectURL(blob);
            document.getElementById("qrImgDiv").appendChild(img);

            initWebSocket();
        }
    }
    xmlhttp.send();
}



var path = "://localhost:8085";
var getQrPath =  "http" + path + "/user/getLoginQr";
var wsPath =     "ws" + path + "/websocket/";



function initWebSocket(){

   if(typeof(WebSocket) == "undefined") {
       console.log("您的浏览器不支持WebSocket");
   }else{
       console.log("您的浏览器支持WebSocket");
       //实现化WebSocket对象,指定要连接的服务器地址与端口  建立连接
       //等同于socket = new WebSocket("ws://localhost:8083/checkcentersys/websocket/20");
       var wsPathStr = wsPath+uuid;
       socket = new WebSocket(wsPathStr);
       //打开事件
       socket.onopen = function() {
           console.log("Socket 已打开");
           //socket.send("这是来自客户端的消息" + location.href + new Date());
       };
       //获得消息事件
       socket.onmessage = function(msg) {
           console.log(msg.data);
           var data = JSON.parse(msg.data);
           if(data.code == 200){
               alert("登录成功!");
               //这里存放自己业务需要的数据。怎么放自己看
               window.sessionStorage.uuid = uuid;
               window.sessionStorage.userId = data.userId;
               window.sessionStorage.projId = data.projId;

               window.location.href = "pages/upload.html"
           }else{
               //如果过期了,关闭连接、重置连接、刷新二维码
               socket.close();
               initQrImg();
           }
           //发现消息进入    开始处理前端触发逻辑
       };
       //关闭事件
       socket.onclose = function() {
           console.log("Socket已关闭");
       };
       //发生了错误事件
       socket.onerror = function() {
           alert("Socket发生了错误");
           //此时可以尝试刷新页面
       }
   }

}
复制代码

好了。上面已经提到了前端如何配置webSocket。

Spring Boot中操作WebSocket

1、增加pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
复制代码

2、增加一个Bean

/**  * WebSocket的支持  * @return  */
@Bean
public ServerEndpointExporter serverEndpointExporter() {
    return new ServerEndpointExporter();
}
复制代码

3、定义WebSocketServer

package com.stylefeng.guns.rest.modular.inve.websocket;
 
/**  * Created by jiangjiacheng on 2019/6/4.  */
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
 
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
 
@ServerEndpoint("/websocket/{sid}")
@Component
public class WebSocketServer {
 
    static Log log=LogFactory.get(WebSocketServer.class);
 
    //静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
    private static int onlineCount = 0;
 
    //concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
    private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
 
    //与某个客户端的连接会话,需要通过它来给客户端发送数据
    private Session session;
 
    //接收sid
    private String sid="";
 
    /**      * 连接建立成功调用的方法*/
    @OnOpen
    public void onOpen(Session session,@PathParam("sid") String sid) {
        this.session = session;
        webSocketSet.add(this);     //加入set中
        addOnlineCount();           //在线数加1
        log.info("有新窗口开始监听:"+sid+",当前在线人数为" + getOnlineCount());
        this.sid=sid;
        /*try {             sendMessage("连接成功");         } catch (IOException e) {             log.error("websocket IO异常");         }*/
    }
 
    /**      * 连接关闭调用的方法      */
    @OnClose
    public void onClose() {
        webSocketSet.remove(this);  //从set中删除
        subOnlineCount();           //在线数减1
        log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
    }
 
    /**      * 收到客户端消息后调用的方法      *      * @param message 客户端发送过来的消息*/
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("收到来自窗口"+sid+"的信息:"+message);
        //群发消息
        for (WebSocketServer item : webSocketSet) {
            try {
                item.sendMessage(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**      *      * @param session      * @param error      */
    @OnError
    public void onError(Session session, Throwable error) {
        log.error("发生错误");
        error.printStackTrace();
    }
    /**      * 实现服务器主动推送      */
    public void sendMessage(String message) throws IOException {
        this.session.getBasicRemote().sendText(message);
    }
 
 
    /**      * 群发自定义消息      * */
    public static void sendInfo(String message,@PathParam("sid") String sid) throws IOException {
        log.info("推送消息到窗口"+sid+",推送内容:"+message);
        for (WebSocketServer item : webSocketSet) {
            try {
                //这里可以设定只推送给这个sid的,为null则全部推送
                if(sid == null) {
                    item.sendMessage(message);
                }else if(item.sid.equals(sid)){
                    item.sendMessage(message);
                }
            } catch (IOException e) {
                continue;
            }
        }
    }
 
    public static synchronized int getOnlineCount() {
        return onlineCount;
    }
 
    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount++;
    }
 
    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount--;
    }
}
复制代码

这样就增加了webSocket的支持啦。

1、首先PC端调用接口展示出来了二维码。

2、请求二维码中的http请求。就有uuid在 header中。直接取到uuid 作为webSocket的标识sid进行连接。

3、然后手机端使用相机拿到二维码中的uuid。使用uuid + userid 请求 扫码成功接口。

贴扫码成功接口

Controller代码:

/**  * 确认身份接口:确定身份以及判断是否二维码过期等  * @param token  * @param userId  * @return  */
@RequestMapping(value = "/bindUserIdAndToken" ,method = RequestMethod.GET)
@ResponseBody
public Object bindUserIdAndToken(@RequestParam("token") String token ,                                  @RequestParam("userId") Integer userId,                                  @RequestParam(required = false,value = "projId") Integer projId){

    try {
        return new SuccessTip(userService.bindUserIdAndToken(userId,token,projId));
    } catch (Exception e) {
        e.printStackTrace();
        return new ErrorTip(500,e.getMessage());
    }

}
复制代码

Service代码

@Override
public String bindUserIdAndToken(Integer userId, String token,Integer projId) throws Exception {

    QrLoginToken qrLoginToken = new QrLoginToken();
    qrLoginToken.setToken(token);
    qrLoginToken = qrLoginTokenMapper.selectOne(qrLoginToken);

    if(null == qrLoginToken){
        throw  new Exception("错误的请求!");
    }

    Date createDate = new Date(qrLoginToken.getCreateTime().getTime() + (1000 * 60 * Constant.LOGIN_VALIDATION_TIME));
    Date nowDate = new Date();
    if(nowDate.getTime() > createDate.getTime()){//当前时间大于校验时间

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",500);
        jsonObject.put("msg","二维码失效!");
        WebSocketServer.sendInfo(jsonObject.toJSONString(),token);

        throw  new Exception("二维码失效!");
    }

    qrLoginToken.setLoginTime(new Date());
    qrLoginToken.setUserId(userId);

    int i = qrLoginTokenMapper.updateById(qrLoginToken);

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("code",200);
    jsonObject.put("msg","ok");
    jsonObject.put("userId",userId);
    if(ToolUtil.isNotEmpty(projId)){
        jsonObject.put("projId",projId);
    }
    WebSocketServer.sendInfo(jsonObject.toJSONString(),token);

    if(i > 0 ){
        return null;
    }else{
        throw  new Exception("服务器异常!");
    }
}
复制代码

逻辑大概就是判断一下 token对不对。

如果对的话。时间是否过期。如果没有过期进行业务逻辑操作

//这句话比较关键
WebSocketServer.sendInfo(jsonObject.toJSONString(),token);
复制代码

就是通知前端已经登录成功了。并且给他业务所需要的内容。

然后前端代码接收到了。就进行业务逻辑操作就可以啦。

Acho que você gosta

Origin juejin.im/post/7082396409080578061
Recomendado
Clasificación