LoginCommonController.java 3.74 KB
package com.glsx.usedcar.web.api;

import com.glsx.usedcar.web.WebConstants;
import com.glsx.usedcar.web.dto.LoginRequest;
import jakarta.security.auth.message.AuthException;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.summerframework.core.common.ResponseResult;


public class LoginCommonController {
    public static final int EXPIRE_SECOND = 120;


    //    @Value("${spring.code.default}")
    private boolean defaultCode = false;


    /**
     * 获取短信验证码
     *
     * @param loginRequest
     * @param httpSession
     * @return
     */
//    @PostMapping(value = "smsCode")
//    @ResponseBody
//    public ResponseResult smsCode(@RequestBody LoginRequest loginRequest, HttpSession httpSession) {
//        ResponseResult result;
//        try {
//            validateCode(httpSession, loginRequest.getImgCode());
//            final String code = defaultCode ? "111111" : generateCode(6);
//            result = smsProvider.sendCommonCode(loginRequest.getMobile(), code);
//            if (result.getResult() == ResponseResult.Result.SUCCESS) {
//                httpSession.setAttribute(loginRequest.getMobile(), code);
//                // 5分钟内有效
//                httpSession.setAttribute(WebConstants.VALID_CODE_EXPIRE_TIME, System.currentTimeMillis() + 60 * 5000);
//            }
//        } catch (AuthException authException) {
//            result = new ResponseResult(ResponseResult.Result.FAILURE, authException.getMessage());
//        }
//        return result;
//    }
    public void validateCode(HttpSession session, String code) throws AuthException {
        String codeInSession = (String) session.getAttribute(WebConstants.CODE_VALID);
        long expireTime = 0L;
        Long val = (Long) session.getAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
        if (val != null) {
            expireTime = val;
        }
        session.removeAttribute(WebConstants.CODE_VALID);
        session.removeAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
        if (defaultCode) {
            return;
        }
        if (StringUtils.isBlank(code)) {
            throw new AuthException("图形验证码不存在");
        } else if (codeInSession == null) {
            throw new AuthException("请先获取图形验证码");
        } else if (System.currentTimeMillis() - expireTime > 0L) {
            throw new AuthException("图形验证码已过期");
        } else if (!codeInSession.equalsIgnoreCase(code)) {
            throw new AuthException("图形验证码不正确");
        }
    }

    public void validateSmsCode(HttpSession session, String mobile, String smsCode) throws AuthException {
        String codeInSession = (String) session.getAttribute(mobile);
        long expireTime = 0L;
        Long val = (Long) session.getAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
        if (val != null) {
            expireTime = val;
        }
        session.removeAttribute(WebConstants.VALID_CODE_EXPIRE_TIME);
        if (defaultCode) {
            return;
        }
        if (StringUtils.isBlank(smsCode)) {
            throw new AuthException("短信验证码不存在");
        } else if (codeInSession == null) {
            throw new AuthException("请先获取短信验证码");
        } else if (System.currentTimeMillis() - expireTime > 0L) {
            throw new AuthException("短信验证码已过期");
        } else if (!codeInSession.equalsIgnoreCase(smsCode)) {
            throw new AuthException("短信验证码不正确");
        }
    }

}