Commit c11a31f4 by 张晋雄

短信验证登陆

parent ff564007
...@@ -6,7 +6,9 @@ import cn.meteor.beyondclouds.core.api.Response; ...@@ -6,7 +6,9 @@ import cn.meteor.beyondclouds.core.api.Response;
import cn.meteor.beyondclouds.core.bean.Subject; import cn.meteor.beyondclouds.core.bean.Subject;
import cn.meteor.beyondclouds.modules.user.bean.AuthenticationResult; import cn.meteor.beyondclouds.modules.user.bean.AuthenticationResult;
import cn.meteor.beyondclouds.modules.user.exception.AuthenticationServiceException; import cn.meteor.beyondclouds.modules.user.exception.AuthenticationServiceException;
import cn.meteor.beyondclouds.modules.user.exception.UserServiceException;
import cn.meteor.beyondclouds.modules.user.form.LocalAuthFrom; import cn.meteor.beyondclouds.modules.user.form.LocalAuthFrom;
import cn.meteor.beyondclouds.modules.user.form.SmsAuthFrom;
import cn.meteor.beyondclouds.modules.user.service.IAuthenticationService; import cn.meteor.beyondclouds.modules.user.service.IAuthenticationService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -46,6 +48,20 @@ public class AuthenticationApi { ...@@ -46,6 +48,20 @@ public class AuthenticationApi {
} }
@Anonymous @Anonymous
@ApiOperation(value = "短信验证登陆")
@PostMapping("/sms")
public Response<AuthenticationResult> smsAuth(@RequestBody @Valid @ApiParam("短信认证表单") SmsAuthFrom smsAuthFrom) throws UserServiceException {
AuthenticationResult authenticationResult = null;
try {
authenticationResult = authenticationService.smsAuthentication(smsAuthFrom.getMobile(), smsAuthFrom.getVerifyCode());
return Response.success(authenticationResult);
} catch (AuthenticationServiceException e) {
e.printStackTrace();
return Response.error(e);
}
}
@Anonymous
@ApiOperation(value = "QQ认证") @ApiOperation(value = "QQ认证")
@GetMapping("/qq") @GetMapping("/qq")
public Response<AuthenticationResult> qqAuth(@RequestParam("code") String code) { public Response<AuthenticationResult> qqAuth(@RequestParam("code") String code) {
......
package cn.meteor.beyondclouds.modules.user.form;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @program: beyond-clouds
* @description: 短信认证表单
* @author: Mr.Zhang
* @create: 2020-02-03 09:49
**/
@ApiModel("短信验证表单")
@Data
public class SmsAuthFrom {
@ApiModelProperty("手机号")
private String mobile;
@ApiModelProperty("验证码")
private String verifyCode;
}
...@@ -2,6 +2,7 @@ package cn.meteor.beyondclouds.modules.user.service; ...@@ -2,6 +2,7 @@ package cn.meteor.beyondclouds.modules.user.service;
import cn.meteor.beyondclouds.modules.user.bean.AuthenticationResult; import cn.meteor.beyondclouds.modules.user.bean.AuthenticationResult;
import cn.meteor.beyondclouds.modules.user.exception.AuthenticationServiceException; import cn.meteor.beyondclouds.modules.user.exception.AuthenticationServiceException;
import cn.meteor.beyondclouds.modules.user.exception.UserServiceException;
/** /**
* 认证服务 * 认证服务
...@@ -23,4 +24,12 @@ public interface IAuthenticationService { ...@@ -23,4 +24,12 @@ public interface IAuthenticationService {
* @return * @return
*/ */
AuthenticationResult qqAuthentication(String code) throws AuthenticationServiceException; AuthenticationResult qqAuthentication(String code) throws AuthenticationServiceException;
/**
* 短信验证登陆
* @param mobile
* @param verifyCode
* @return
*/
AuthenticationResult smsAuthentication(String mobile, String verifyCode) throws UserServiceException, AuthenticationServiceException;
} }
...@@ -3,9 +3,13 @@ package cn.meteor.beyondclouds.modules.user.service.impl; ...@@ -3,9 +3,13 @@ package cn.meteor.beyondclouds.modules.user.service.impl;
import cn.meteor.beyondclouds.common.bean.QQAuthResult; import cn.meteor.beyondclouds.common.bean.QQAuthResult;
import cn.meteor.beyondclouds.common.exception.QQAuthenticationException; import cn.meteor.beyondclouds.common.exception.QQAuthenticationException;
import cn.meteor.beyondclouds.common.helper.IQQAuthenticationHelper; import cn.meteor.beyondclouds.common.helper.IQQAuthenticationHelper;
import cn.meteor.beyondclouds.common.helper.IRedisHelper;
import cn.meteor.beyondclouds.core.redis.RedisKey;
import cn.meteor.beyondclouds.modules.user.entity.UserAuthApp; import cn.meteor.beyondclouds.modules.user.entity.UserAuthApp;
import cn.meteor.beyondclouds.modules.user.entity.UserAuthLocal; import cn.meteor.beyondclouds.modules.user.entity.UserAuthLocal;
import cn.meteor.beyondclouds.modules.user.enums.ThirdPartyAppType; import cn.meteor.beyondclouds.modules.user.enums.ThirdPartyAppType;
import cn.meteor.beyondclouds.modules.user.enums.UserErrorCode;
import cn.meteor.beyondclouds.modules.user.exception.UserServiceException;
import cn.meteor.beyondclouds.modules.user.service.IUserAuthAppService; import cn.meteor.beyondclouds.modules.user.service.IUserAuthAppService;
import cn.meteor.beyondclouds.modules.user.service.IUserService; import cn.meteor.beyondclouds.modules.user.service.IUserService;
import cn.meteor.beyondclouds.util.JsonUtils; import cn.meteor.beyondclouds.util.JsonUtils;
...@@ -19,6 +23,7 @@ import cn.meteor.beyondclouds.util.Md5Utils; ...@@ -19,6 +23,7 @@ import cn.meteor.beyondclouds.util.Md5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.HashMap; import java.util.HashMap;
...@@ -38,12 +43,19 @@ public class AuthenticationServiceImpl implements IAuthenticationService { ...@@ -38,12 +43,19 @@ public class AuthenticationServiceImpl implements IAuthenticationService {
private IUserService userService; private IUserService userService;
private IRedisHelper redisHelper;
@Autowired @Autowired
public AuthenticationServiceImpl(IUserAuthLocalService userAuthLocalService, IQQAuthenticationHelper iqqAuthenticationHelper, IUserAuthAppService userAuthAppService, IUserService userService) { public AuthenticationServiceImpl(IUserAuthLocalService userAuthLocalService,
IQQAuthenticationHelper iqqAuthenticationHelper,
IUserAuthAppService userAuthAppService,
IUserService userService,
IRedisHelper redisHelper) {
this.userAuthLocalService = userAuthLocalService; this.userAuthLocalService = userAuthLocalService;
this.iqqAuthenticationHelper = iqqAuthenticationHelper; this.iqqAuthenticationHelper = iqqAuthenticationHelper;
this.userAuthAppService = userAuthAppService; this.userAuthAppService = userAuthAppService;
this.userService = userService; this.userService = userService;
this.redisHelper = redisHelper;
} }
@Override @Override
...@@ -93,6 +105,29 @@ public class AuthenticationServiceImpl implements IAuthenticationService { ...@@ -93,6 +105,29 @@ public class AuthenticationServiceImpl implements IAuthenticationService {
} }
} }
@Override
public AuthenticationResult smsAuthentication(String mobile, String verifyCode) throws UserServiceException, AuthenticationServiceException {
//1.检查验证码是否正确
String realVerifyCode = redisHelper.get(RedisKey.VERIFY_CODE_KEY(mobile));
if (StringUtils.isEmpty(realVerifyCode) || !realVerifyCode.equals(verifyCode)) {
throw new UserServiceException(UserErrorCode.REG_VERIFY_CODE_ERROR);
}
// 删除验证码
redisHelper.del(RedisKey.VERIFY_CODE_KEY(mobile));
//2. 查找用户是否存在
UserAuthLocal userAuthLocal = userAuthLocalService.getByMobile(mobile);
if (null == userAuthLocal) {
throw new AuthenticationServiceException(AuthenticationErrorCode.USER_NOT_FOUND);
}
// 根据userId生成token并返回
return makeAuthenticationResult(userAuthLocal.getUserId());
}
/** /**
* 制造认证结果 * 制造认证结果
* @param userId * @param userId
......
...@@ -41,10 +41,10 @@ public class UserServiceImpl extends ServiceImpl<IUserMapper, User> implements I ...@@ -41,10 +41,10 @@ public class UserServiceImpl extends ServiceImpl<IUserMapper, User> implements I
} }
@Override @Override
public void register(String mobile, String password, String verifyCOde) throws UserServiceException { public void register(String mobile, String password, String verifyCode) throws UserServiceException {
//1.检查验证码是否正确 //1.检查验证码是否正确
String realVerifyCode = redisHelper.get(RedisKey.VERIFY_CODE_KEY(mobile)); String realVerifyCode = redisHelper.get(RedisKey.VERIFY_CODE_KEY(mobile));
if (StringUtils.isEmpty(realVerifyCode) || !realVerifyCode.equals(verifyCOde)) { if (StringUtils.isEmpty(realVerifyCode) || !realVerifyCode.equals(verifyCode)) {
throw new UserServiceException(UserErrorCode.REG_VERIFY_CODE_ERROR); throw new UserServiceException(UserErrorCode.REG_VERIFY_CODE_ERROR);
} }
......
...@@ -3,7 +3,7 @@ spring: ...@@ -3,7 +3,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: root username: root
password: 100Centa30821%mysql password: 2018006709
swagger: swagger:
enable: true enable: true
......
...@@ -3,7 +3,7 @@ spring: ...@@ -3,7 +3,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true url: jdbc:mysql://127.0.0.1:3306/beyond_clouds?useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowPublicKeyRetrieval=true
username: root username: root
password: 100Centa30821%mysql password: 2018006709
swagger: swagger:
enable: true enable: true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment