huwhois 4 lat temu
rodzic
commit
7ab67594b3

+ 11 - 0
src/main/java/io/renren/common/validator/Assert.java

9
package io.renren.common.validator;
9
package io.renren.common.validator;
10
10
11
import io.renren.common.exception.RRException;
11
import io.renren.common.exception.RRException;
12
13
import java.util.regex.Pattern;
14
12
import org.apache.commons.lang.StringUtils;
15
import org.apache.commons.lang.StringUtils;
13
16
14
/**
17
/**
29
            throw new RRException(message);
32
            throw new RRException(message);
30
        }
33
        }
31
    }
34
    }
35
36
    public static void isNotPhone(String phone) {
37
        String phone_regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
38
        Pattern p = Pattern.compile(phone_regex);
39
        if (phone.length() != 11 || !p.matcher(phone).matches()) {
40
            throw new RRException("手机号格式不正确");
41
        }
42
    }
32
}
43
}

+ 53 - 7
src/main/java/io/renren/modules/app/controller/AppRegisterController.java

9
package io.renren.modules.app.controller;
9
package io.renren.modules.app.controller;
10
10
11
import io.renren.common.utils.R;
11
import io.renren.common.utils.R;
12
import io.renren.common.validator.Assert;
12
import io.renren.common.validator.ValidatorUtils;
13
import io.renren.common.validator.ValidatorUtils;
13
import io.renren.modules.app.entity.UserEntity;
14
import io.renren.modules.app.entity.UserEntity;
14
import io.renren.modules.app.form.RegisterForm;
15
import io.renren.modules.app.form.RegisterForm;
15
import io.renren.modules.app.service.SmsCodeService;
16
import io.renren.modules.app.service.SmsCodeService;
16
import io.renren.modules.app.service.UserService;
17
import io.renren.modules.app.service.UserService;
17
import io.renren.common.utils.JsonUtils;
18
import io.renren.modules.app.entity.SmsStatusEntity;
19
import io.swagger.annotations.Api;
18
import io.swagger.annotations.Api;
20
import io.swagger.annotations.ApiOperation;
19
import io.swagger.annotations.ApiOperation;
21
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.beans.factory.annotation.Autowired;
41
    private SmsCodeService smsCodeService;
40
    private SmsCodeService smsCodeService;
42
41
43
    /**
42
    /**
44
     * 短信验证码
43
     * 注册短信验证码
45
     */
44
     */
46
    @PostMapping("/mobilecode")
45
    @PostMapping("/mobilecode")
47
    public R mobileCode(String uuid, String phone) {
46
    public R mobileCode(String uuid, String phone) {
48
        // 获取短信验证码
49
        String code = smsCodeService.getCode(uuid, phone);
47
        Assert.isNotPhone(phone);
48
49
        UserEntity user = userService.queryByUsername(phone);
50
        if (user != null) {
51
            return R.error("手机号已注册");
52
        }
53
50
        try {
54
        try {
51
            String result = smsCodeService.sendCode(code, phone);
55
            String result = smsCodeService.getCode(uuid, phone);
52
            return R.ok().put("result", result);
56
            return R.ok().put("result", result);
53
        } catch (Exception e) {
57
        } catch (Exception e) {
54
            e.printStackTrace();
58
            e.printStackTrace();
59
    @PostMapping("register")
63
    @PostMapping("register")
60
    @ApiOperation("注册")
64
    @ApiOperation("注册")
61
    public R register(@RequestBody RegisterForm form){
65
    public R register(@RequestBody RegisterForm form){
62
        boolean captcha = smsCodeService.validate(form.getUuid(), form.getCode());
66
        boolean captcha = smsCodeService.validate(form.getUuid(), form.getCode(), form.getPhone());
63
		if(!captcha){
67
		if(!captcha){
64
			return R.error("验证码不正确");
68
			return R.error("验证码不正确");
65
        }
69
        }
75
79
76
        return R.ok();
80
        return R.ok();
77
    }
81
    }
82
    
83
    /**
84
     * 找回密码短信验证码
85
     */
86
    @PostMapping("/phonecode")
87
    public R phoneCode(String uuid, String phone) {
88
        UserEntity user = userService.queryByUsername(phone);
89
        if (user == null) {
90
            return R.error("手机号未注册");
91
        }
92
93
        try {
94
            String result = smsCodeService.getCode(uuid, phone);
95
            return R.ok().put("result", result);
96
        } catch (Exception e) {
97
            e.printStackTrace();
98
            return R.error();
99
        }
100
    }
101
102
    /**
103
     * 通过手机验证码重置密码
104
     * @param form
105
     * @return
106
     */
107
    @PostMapping("repassword")
108
    @ApiOperation("通过手机验证码重置密码")
109
    public R rePassword(@RequestBody RegisterForm form){
110
        boolean captcha = smsCodeService.validate(form.getUuid(), form.getCode(), form.getPhone());
111
		if(!captcha){
112
			return R.error("验证码不正确");
113
        }
114
        
115
        //表单校验
116
        ValidatorUtils.validateEntity(form);
117
118
        UserEntity user = userService.queryByUsername(form.getPhone());
119
        Long userId = user.getUserId();
120
        userService.updatePassWord(userId, form.getPassword());
121
122
        return R.ok();
123
    }
78
}
124
}

+ 0 - 13
src/main/java/io/renren/modules/app/entity/SmsStatusEntity.java

1
package io.renren.modules.app.entity;
2
import lombok.Data;
3
4
/**
5
 * 短信发送返回值实体类
6
 */
7
@Data
8
public class SmsStatusEntity {
9
    private String respCode;
10
    private String respDesc;
11
    private String smsId;
12
    private String[] failList;
13
}

+ 2 - 14
src/main/java/io/renren/modules/app/service/SmsCodeService.java

1
package io.renren.modules.app.service;
1
package io.renren.modules.app.service;
2
2
3
import com.baomidou.mybatisplus.extension.service.IService;
3
import com.baomidou.mybatisplus.extension.service.IService;
4
import io.renren.common.utils.PageUtils;
5
import io.renren.modules.app.entity.SmsCodeEntity;
4
import io.renren.modules.app.entity.SmsCodeEntity;
6
5
7
import java.util.Map;
8
9
/**
6
/**
10
 * 短信验证码
7
 * 短信验证码
11
 *
8
 *
17
    /**
14
    /**
18
     * 获取短信验证码
15
     * 获取短信验证码
19
     */
16
     */
20
    String getCode(String uuid, String phone);
17
    String getCode(String uuid, String phone) throws Exception;
21
18
22
    /**
19
    /**
23
     * 验证码效验
20
     * 验证码效验
25
     * @param code  验证码
22
     * @param code  验证码
26
     * @return  true:成功  false:失败
23
     * @return  true:成功  false:失败
27
     */
24
     */
28
    boolean validate(String uuid, String code);
29
30
    /**
31
     *  发送短信验证码
32
     * @param code
33
     * @param phone
34
     * @return
35
     * @throws Exception
36
     */
37
	String sendCode(String code, String phone) throws Exception;
25
    boolean validate(String uuid, String code, String phone);
38
}
26
}

+ 15 - 18
src/main/java/io/renren/modules/app/service/impl/SmsCodeServiceImpl.java

21
public class SmsCodeServiceImpl extends ServiceImpl<SmsCodeDao, SmsCodeEntity> implements SmsCodeService {
21
public class SmsCodeServiceImpl extends ServiceImpl<SmsCodeDao, SmsCodeEntity> implements SmsCodeService {
22
22
23
    @Override
23
    @Override
24
    public String getCode(String uuid, String phone){
24
    public String getCode(String uuid, String phone) throws Exception {
25
        if (StringUtils.isBlank(uuid)) {
25
        if (StringUtils.isBlank(uuid)) {
26
            throw new RRException("uuid不能为空");
26
            throw new RRException("uuid不能为空");
27
        }
27
        }
32
        smsCodeEntity.setPhone(phone);
32
        smsCodeEntity.setPhone(phone);
33
        smsCodeEntity.setCode(code);
33
        smsCodeEntity.setCode(code);
34
        smsCodeEntity.setExpireTime(DateUtils.addDateMinutes(new Date(), ConfigConstant.EXPIRE_TIME));
34
        smsCodeEntity.setExpireTime(DateUtils.addDateMinutes(new Date(), ConfigConstant.EXPIRE_TIME));
35
35
        
36
        // 发送验证码
37
        StringBuilder sb = new StringBuilder();
38
		sb.append("accountSid").append("=").append(ConfigConstant.ACCOUNT_SID);
39
		sb.append("&to").append("=").append(phone);
40
        sb.append("&param").append("=").append(URLEncoder.encode(code + "," + ConfigConstant.EXPIRE_TIME, "UTF-8"));
41
		sb.append("&templateid").append("=").append(ConfigConstant.TEMPLATE_ID);
42
		String body = sb.toString() + HttpRequestUtil.createCommonParam(ConfigConstant.ACCOUNT_SID, ConfigConstant.AUTH_TOKEN);
43
		String result = HttpRequestUtil.post(ConfigConstant.BASE_URL, body);
44
		System.out.println(result);
45
        
36
        this.save(smsCodeEntity);
46
        this.save(smsCodeEntity);
37
        return code;
47
        return result;
38
    }
48
    }
39
49
40
    @Override
50
    @Override
41
    public boolean validate(String uuid, String code) {
51
    public boolean validate(String uuid, String code, String phone) {
42
        SmsCodeEntity smsCodeEntity = this.getOne(new QueryWrapper<SmsCodeEntity>().eq("uuid", uuid));
52
        SmsCodeEntity smsCodeEntity = this.getOne(new QueryWrapper<SmsCodeEntity>().eq("uuid", uuid));
43
        if(smsCodeEntity == null){
53
        if(smsCodeEntity == null){
44
            return false;
54
            return false;
47
        //删除验证码
57
        //删除验证码
48
        this.removeById(uuid);
58
        this.removeById(uuid);
49
59
50
        if(smsCodeEntity.getCode().equalsIgnoreCase(code) && smsCodeEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
60
        if(smsCodeEntity.getCode().equalsIgnoreCase(code) && smsCodeEntity.getPhone().equalsIgnoreCase(phone) && smsCodeEntity.getExpireTime().getTime() >= System.currentTimeMillis()){
51
            return true;
61
            return true;
52
        }
62
        }
53
        return false;
63
        return false;
54
    }
64
    }
55
56
    @Override
57
    public String sendCode(String code, String phone) throws Exception {
58
        StringBuilder sb = new StringBuilder();
59
		sb.append("accountSid").append("=").append(ConfigConstant.ACCOUNT_SID);
60
		sb.append("&to").append("=").append(phone);
61
        sb.append("&param").append("=").append(URLEncoder.encode(code + "," + ConfigConstant.EXPIRE_TIME, "UTF-8"));
62
		sb.append("&templateid").append("=").append(ConfigConstant.TEMPLATE_ID);
63
		String body = sb.toString() + HttpRequestUtil.createCommonParam(ConfigConstant.ACCOUNT_SID, ConfigConstant.AUTH_TOKEN);
64
		String result = HttpRequestUtil.post(ConfigConstant.BASE_URL, body);
65
		System.out.println(result);
66
        return result;
67
    }
68
}
65
}

+ 8 - 10
src/main/java/io/renren/modules/app/service/impl/UserServiceImpl.java

20
import io.renren.modules.app.entity.UserEntity;
20
import io.renren.modules.app.entity.UserEntity;
21
import io.renren.modules.app.form.LoginForm;
21
import io.renren.modules.app.form.LoginForm;
22
import io.renren.modules.app.service.UserService;
22
import io.renren.modules.app.service.UserService;
23
import org.apache.commons.codec.digest.DigestUtils;
24
import org.apache.commons.lang.RandomStringUtils;
23
import org.apache.commons.lang.RandomStringUtils;
25
import org.apache.shiro.crypto.hash.Sha256Hash;
24
import org.apache.shiro.crypto.hash.Sha256Hash;
26
import org.springframework.stereotype.Service;
25
import org.springframework.stereotype.Service;
40
		UserEntity user = queryByUsername(form.getUsername());
39
		UserEntity user = queryByUsername(form.getUsername());
41
		Assert.isNull(user, "账号或密码错误");
40
		Assert.isNull(user, "账号或密码错误");
42
		//密码错误
41
		//密码错误
43
		// if(!user.getPassword().equals(DigestUtils.sha256Hex(form.getPassword()))){
44
		if(!user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())){
42
		if(!user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())){
45
			throw new RRException("账号或密码错误");
43
			throw new RRException("账号或密码错误");
46
		}
44
		}
59
		this.save(user); 
57
		this.save(user); 
60
	}
58
	}
61
59
60
	@Override
61
	public int updatePassWord(Long id, String password) {
62
		String salt = RandomStringUtils.randomAlphanumeric(20);
63
		password = new Sha256Hash(password, salt).toHex();
64
		
65
		return baseMapper.updatePassWord(id, password, salt);
66
	}
67
	
62
	@Override
68
	@Override
63
	public List<Map<String, Object>> queryOrderList(Long memberId) {
69
	public List<Map<String, Object>> queryOrderList(Long memberId) {
64
		return baseMapper.queryOrderList(memberId);
70
		return baseMapper.queryOrderList(memberId);
68
	public Map<String, Object> queryOrderInfoByAttendersId(Long attendersId) {
74
	public Map<String, Object> queryOrderInfoByAttendersId(Long attendersId) {
69
		return baseMapper.queryOrderInfoByAttendersId(attendersId);
75
		return baseMapper.queryOrderInfoByAttendersId(attendersId);
70
	}
76
	}
71
72
	@Override
73
	public int updatePassWord(Long id, String password) {
74
		String salt = RandomStringUtils.randomAlphanumeric(20);
75
		password = new Sha256Hash(password, salt).toHex();
76
77
		return baseMapper.updatePassWord(id, password, salt);
78
	}
79
}
77
}