Search in sources :

Example 1 with ResultUtil

use of cn.exrick.common.utils.ResultUtil in project xmall by Exrick.

the class UserController method login.

@RequestMapping(value = "/user/login", method = RequestMethod.POST)
@ApiOperation(value = "用户登录")
@SystemControllerLog(description = "登录系统")
public Result<Object> login(String username, String password, String challenge, String validate, String seccode, HttpServletRequest request) {
    // 极验验证
    GeetestLib gtSdk = new GeetestLib(GeetestLib.id, GeetestLib.key, GeetestLib.newfailback);
    // 从session中获取gt-server状态
    int gt_server_status_code = (Integer) request.getSession().getAttribute(gtSdk.gtServerStatusSessionKey);
    // 自定义参数,可选择添加
    HashMap<String, String> param = new HashMap<String, String>();
    int gtResult = 0;
    if (gt_server_status_code == 1) {
        // gt-server正常,向gt-server进行二次验证
        gtResult = gtSdk.enhencedValidateRequest(challenge, validate, seccode, param);
        System.out.println(gtResult);
    } else {
        // gt-server非正常情况下,进行failback模式验证
        System.out.println("failback:use your own server captcha validate");
        gtResult = gtSdk.failbackValidateRequest(challenge, validate, seccode);
        System.out.println(gtResult);
    }
    if (gtResult == 1) {
        // 验证成功
        Subject subject = SecurityUtils.getSubject();
        // MD5加密
        String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        UsernamePasswordToken token = new UsernamePasswordToken(username, md5Pass);
        try {
            subject.login(token);
            return new ResultUtil<Object>().setData(null);
        } catch (Exception e) {
            return new ResultUtil<Object>().setErrorMsg("用户名或密码错误");
        }
    } else {
        // 验证失败
        return new ResultUtil<Object>().setErrorMsg("验证失败");
    }
}
Also used : ResultUtil(cn.exrick.common.utils.ResultUtil) HashMap(java.util.HashMap) GeetestLib(cn.exrick.common.utils.GeetestLib) Subject(org.apache.shiro.subject.Subject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) ApiOperation(io.swagger.annotations.ApiOperation) SystemControllerLog(cn.exrick.manager.annotation.SystemControllerLog)

Example 2 with ResultUtil

use of cn.exrick.common.utils.ResultUtil in project xmall by Exrick.

the class MemberController method register.

@RequestMapping(value = "/member/register", method = RequestMethod.POST)
@ApiOperation(value = "用户注册")
public Result<Object> register(@RequestBody MemberLoginRegist memberLoginRegist, HttpServletRequest request) {
    // 极验验证
    GeetestLib gtSdk = new GeetestLib(GeetestLib.id, GeetestLib.key, GeetestLib.newfailback);
    String challenge = memberLoginRegist.getChallenge();
    String validate = memberLoginRegist.getValidate();
    String seccode = memberLoginRegist.getSeccode();
    // 从session中获取gt-server状态
    int gt_server_status_code = (Integer) request.getSession().getAttribute(gtSdk.gtServerStatusSessionKey);
    // 自定义参数,可选择添加
    HashMap<String, String> param = new HashMap<String, String>();
    int gtResult = 0;
    if (gt_server_status_code == 1) {
        // gt-server正常,向gt-server进行二次验证
        gtResult = gtSdk.enhencedValidateRequest(challenge, validate, seccode, param);
        System.out.println(gtResult);
    } else {
        // gt-server非正常情况下,进行failback模式验证
        System.out.println("failback:use your own server captcha validate");
        gtResult = gtSdk.failbackValidateRequest(challenge, validate, seccode);
        System.out.println(gtResult);
    }
    if (gtResult == 1) {
        // 验证成功
        int result = registerService.register(memberLoginRegist.getUserName(), memberLoginRegist.getUserPwd());
        if (result == 0) {
            return new ResultUtil<Object>().setErrorMsg("该用户名已被注册");
        } else if (result == -1) {
            return new ResultUtil<Object>().setErrorMsg("用户名密码不能为空");
        }
        return new ResultUtil<Object>().setData(result);
    } else {
        // 验证失败
        return new ResultUtil<Object>().setErrorMsg("验证失败");
    }
}
Also used : ResultUtil(cn.exrick.common.utils.ResultUtil) HashMap(java.util.HashMap) GeetestLib(cn.exrick.common.utils.GeetestLib) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

GeetestLib (cn.exrick.common.utils.GeetestLib)2 ResultUtil (cn.exrick.common.utils.ResultUtil)2 ApiOperation (io.swagger.annotations.ApiOperation)2 HashMap (java.util.HashMap)2 SystemControllerLog (cn.exrick.manager.annotation.SystemControllerLog)1 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)1 Subject (org.apache.shiro.subject.Subject)1