Search in sources :

Example 1 with GeetestLib

use of cn.exrick.common.utils.GeetestLib 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 GeetestLib

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

the class UserController method geetesrInit.

@RequestMapping(value = "/geetestInit", method = RequestMethod.GET)
@ApiOperation(value = "极验初始化")
public String geetesrInit(HttpServletRequest request) {
    GeetestLib gtSdk = new GeetestLib(GeetestLib.id, GeetestLib.key, GeetestLib.newfailback);
    String resStr = "{}";
    // 自定义参数,可选择添加
    HashMap<String, String> param = new HashMap<String, String>();
    // 进行验证预处理
    int gtServerStatus = gtSdk.preProcess(param);
    // 将服务器状态设置到session中
    request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
    resStr = gtSdk.getResponseStr();
    return resStr;
}
Also used : HashMap(java.util.HashMap) GeetestLib(cn.exrick.common.utils.GeetestLib) ApiOperation(io.swagger.annotations.ApiOperation)

Example 3 with GeetestLib

use of cn.exrick.common.utils.GeetestLib 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)

Example 4 with GeetestLib

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

the class MemberController method geetesrInit.

@RequestMapping(value = "/member/geetestInit", method = RequestMethod.GET)
@ApiOperation(value = "极验初始化")
public String geetesrInit(HttpServletRequest request) {
    GeetestLib gtSdk = new GeetestLib(GeetestLib.id, GeetestLib.key, GeetestLib.newfailback);
    String resStr = "{}";
    // 自定义参数,可选择添加
    HashMap<String, String> param = new HashMap<String, String>();
    // 进行验证预处理
    int gtServerStatus = gtSdk.preProcess(param);
    // 将服务器状态设置到session中
    request.getSession().setAttribute(gtSdk.gtServerStatusSessionKey, gtServerStatus);
    resStr = gtSdk.getResponseStr();
    return resStr;
}
Also used : HashMap(java.util.HashMap) GeetestLib(cn.exrick.common.utils.GeetestLib) ApiOperation(io.swagger.annotations.ApiOperation)

Example 5 with GeetestLib

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

the class MemberController method login.

@RequestMapping(value = "/member/login", method = RequestMethod.POST)
@ApiOperation(value = "用户登录")
public Result<Member> login(@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);
    }
    Member member = new Member();
    if (gtResult == 1) {
        // 验证成功
        member = loginService.userLogin(memberLoginRegist.getUserName(), memberLoginRegist.getUserPwd());
    } else {
        // 验证失败
        member.setState(0);
        member.setMessage("验证失败");
    }
    return new ResultUtil<Member>().setData(member);
}
Also used : HashMap(java.util.HashMap) GeetestLib(cn.exrick.common.utils.GeetestLib) Member(cn.exrick.manager.dto.front.Member) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

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