帆软报表决策系统自定义登录界面 使用验证码登录 教程二

楼主
插件开发者
前面说到调用获取验证码接口后,会向我们的邮箱发送验证码,并且接口会返回一个captchaSendToken。
下一步就是使用验证码来进行登录,进行登录之前需要调用接口/webroot/decision/captcha/check来验证我们输入的验证码,代码如下:
  1. var checkCaptcha = function () {
  2.          
  3.             $.ajax({
  4.                 url: "/webroot/decision/captcha/check",
  5.                 type: "POST",
  6.                 contentType: "application/json",
  7.                 data: JSON.stringify({ "token": captchaSendToken, "captcha": $("#_capatacode").val(), "validity": -1 }),
  8.                 dataType: "json",
  9.                 cache: !1,
  10.                 async: !0,
  11.                 error: function (err) {
  12.                     console.log(err)
  13.                 },
  14.                 complete: function (e, t) {
  15.                     console.log(e.responseText)
  16.                     if ("success" == t) {
  17.                         data = JSON.parse(e.responseText)
  18.                         console.log(data)

  19.                         loginByCaptcha();
  20.                     }

  21.                 }
  22.             })
  23.         }
复制代码
  /webroot/decision/captcha/check接口对应的后端代码为:
  1. package com.fr.plugin.controller;

  2. import com.fr.decision.webservice.Response;
  3. import com.fr.decision.webservice.annotation.LoginStatusChecker;
  4. import com.fr.decision.webservice.bean.authentication.LoginCaptchaBean;
  5. import com.fr.decision.webservice.v10.login.LoginService;
  6. import com.fr.third.springframework.stereotype.Controller;
  7. import com.fr.third.springframework.web.bind.annotation.RequestBody;
  8. import com.fr.third.springframework.web.bind.annotation.RequestMapping;
  9. import com.fr.third.springframework.web.bind.annotation.RequestMethod;
  10. import com.fr.third.springframework.web.bind.annotation.ResponseBody;

  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;

  13. @Controller
  14. @LoginStatusChecker(
  15.         required = false //不需要验证是否登录
  16. )
  17. public class CaptchaController {

  18.     @RequestMapping(
  19.             value = {"/captcha/info"},
  20.             method = {RequestMethod.GET}
  21.     )
  22.     @ResponseBody
  23.     public Response getLastLoginInfo(HttpServletRequest req, HttpServletResponse res) throws Exception {
  24.         return Response.success();
  25.     }

  26.     @RequestMapping(
  27.             value = {"/captcha/check"},
  28.             method = {RequestMethod.POST}
  29.     )
  30.     @ResponseBody
  31.     public Response checkLoginCaptcha(HttpServletRequest req, HttpServletResponse res, @RequestBody LoginCaptchaBean loginCaptchaBean) throws Exception {
  32.         LoginService.getInstance().checkLoginCaptcha(loginCaptchaBean);
  33.         return Response.success();
  34.     }


  35. }
复制代码
调用LoginService.getInstance().checkLoginCaptcha(loginCaptchaBean)来对验证码进行核对,必须经过这一步,否则登录不成功。

最后进行真正的登录,使用/webroot/decision/login/captcha接口来登录,代码如下:
  1. var loginByCaptcha = function () {
  2.             $.ajax({
  3.                 url: "/webroot/decision/login/captcha",
  4.                 type: "POST",
  5.                 contentType: "application/json",
  6.                 data: JSON.stringify({ "token": captchaSendToken, "captcha": $("#_capatacode").val(), "validity": -1 }),
  7.                 dataType: "json",
  8.                 cache: !1,
  9.                 async: !0,
  10.                 error: function (err) {
  11.                     console.log(err)
  12.                 },
  13.                 complete: function (e, t) {
  14.                     console.log(e.responseText)
  15.                     if ("success" == t) {
  16.                         data = JSON.parse(e.responseText)
  17.                         console.log(data)
  18.                         document.cookie = "fine_auth_token=" + data.data.accessToken + "; path=/";
  19.                         // $.cookie('fine_auth_token', data.data.accessToken, {  path: '/' });
  20.                         window.location.href = data.data.originUrlResponse.originUrl
  21.                     }

  22.                 }
  23.             })

  24.         }
复制代码
登录成功后就会进入主界面了。

最后,验证码登录其实有两种,一种是邮箱接收验证码,一种是手机短信,他俩的不同实现方式两个地方:
1 type不同,邮箱是email,手机是mobile
2 都需要自己来实现发送逻辑,验证码发送到邮箱,验证码发送到手机短信,都需要通过扩展来实现,手机通过扩展SMSServiceProvider,邮箱通过扩展EmailServiceProvider

编辑于 2021-3-26 10:49  
分享扩散:

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

返回顶部 返回列表