فهرست منبع

图片验证码基础码

gaodm 6 سال پیش
والد
کامیت
5596949725
20فایلهای تغییر یافته به همراه664 افزوده شده و 6 حذف شده
  1. 5 4
      common/src/main/java/com/diagbot/exception/ErrorCode.java
  2. 1 0
      config-server/src/main/resources/shared/user-service-dev.yml
  3. 1 0
      config-server/src/main/resources/shared/user-service-local.yml
  4. 8 0
      user-service/pom.xml
  5. 17 0
      user-service/src/main/java/com/diagbot/config/AlidayuConfig.java
  6. 28 0
      user-service/src/main/java/com/diagbot/config/AlidayuConfigurer.java
  7. 14 1
      user-service/src/main/java/com/diagbot/config/RedisConfigurer.java
  8. 1 0
      user-service/src/main/java/com/diagbot/config/ResourceServerConfigurer.java
  9. 1 0
      user-service/src/main/java/com/diagbot/config/security/UrlAccessDecisionManager.java
  10. 24 0
      user-service/src/main/java/com/diagbot/entity/ImgVerInfo.java
  11. 35 0
      user-service/src/main/java/com/diagbot/facade/VerFacade.java
  12. 35 0
      user-service/src/main/java/com/diagbot/service/ImgVerService.java
  13. 37 0
      user-service/src/main/java/com/diagbot/service/SmsVerService.java
  14. 182 0
      user-service/src/main/java/com/diagbot/service/impl/ImgVerServiceImpl.java
  15. 224 0
      user-service/src/main/java/com/diagbot/service/impl/SmsVerServiceImpl.java
  16. 0 1
      user-service/src/main/java/com/diagbot/util/VerifyCodeUtils.java
  17. 16 0
      user-service/src/main/java/com/diagbot/vo/ImgVerCreatVO.java
  18. 32 0
      user-service/src/main/java/com/diagbot/web/VerController.java
  19. BIN
      user-service/src/main/libs/taobao-sdk-java-auto_1455552377940-20170303.jar
  20. 3 0
      user-service/src/main/resources/alidayu.properties

+ 5 - 4
common/src/main/java/com/diagbot/exception/ErrorCode.java

@@ -12,12 +12,13 @@ public enum ErrorCode {
     OK(0, "操作成功"),
     FAIL(-1, "操作失败"),
     RPC_ERROR(-2,"远程调度失败"),
-    INSERT_DATA_FAILED(00020001, "数据库写入失败!"),
-    NOT_EXISTS (00020002, "该数据不存在!"),
+    NOT_EXISTS (00020001, "该数据不存在!"),
+    INSERT_DATA_FAILED(00020002, "数据库写入失败!"),
     UPDATE_INFO_FAIL(00020003, "更新数据失败!"),
     PARAM_IS_NULL(00020004, "传入的参数为空!"),
-    STATUS_IS_ERROR(00020005, "参数状态错误!"),
-    VERIFYCODE_ERROR(00020006, "图片验证码生成错误!"),
+    PARAM_IS_ERROR(00020005, "传入的参数为错误!"),
+    STATUS_IS_ERROR(00020006, "参数状态错误!"),
+    VERIFYCODE_ERROR(00020007, "图片验证码生成错误!"),
 
     USER_NOT_FOUND(10020000,"用户不存在"),
     USER_PASSWORD_ERROR(10020001,"密码错误"),

+ 1 - 0
config-server/src/main/resources/shared/user-service-dev.yml

@@ -80,6 +80,7 @@ spring:
     database:
       cache: 0 # Redis缓存索引
       sms: 1 # Redis短信索引
+      img: 2 # Redis图片验证码索引
     host: 192.168.2.236  #Redis服务器地址
     port: 6379 # Redis服务器连接端口
     password: lantone # Redis服务器连接密码(默认为空)

+ 1 - 0
config-server/src/main/resources/shared/user-service-local.yml

@@ -80,6 +80,7 @@ spring:
     database:
       cache: 0 # Redis缓存索引
       sms: 1 # Redis短信索引
+      img: 2 # Redis图片验证码索引
     host: 127.0.0.1  #Redis服务器地址
     port: 6379 # Redis服务器连接端口
     password: # Redis服务器连接密码(默认为空)

+ 8 - 0
user-service/pom.xml

@@ -190,6 +190,14 @@
             <version>2.6.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.taobao</groupId>
+            <artifactId>taobao-sdk</artifactId>
+            <version>1.0</version>
+            <scope>system</scope>
+            <systemPath>${basedir}/src/main/libs/taobao-sdk-java-auto_1455552377940-20170303.jar</systemPath>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 17 - 0
user-service/src/main/java/com/diagbot/config/AlidayuConfig.java

@@ -0,0 +1,17 @@
+package com.diagbot.config;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @Description: 阿里大鱼调用配置参数
+ * @author: gaodm
+ * @time: 2018/9/4 19:23
+ */
+@Getter
+@Setter
+public class AlidayuConfig {
+    private String serverUrl;
+    private String appKey;
+    private String appSecret;
+}

+ 28 - 0
user-service/src/main/java/com/diagbot/config/AlidayuConfigurer.java

@@ -0,0 +1,28 @@
+package com.diagbot.config;
+
+import com.taobao.api.DefaultTaobaoClient;
+import com.taobao.api.TaobaoClient;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+
+/**
+ * @Description: 阿里大鱼配置项
+ * @author: gaodm
+ * @time: 2018/9/4 19:24
+ */
+@Configuration
+@PropertySource("classpath:alidayu.properties")
+public class AlidayuConfigurer {
+    @Bean
+    @ConfigurationProperties(prefix = "alidayu")
+    public AlidayuConfig alidayuConfig() {
+        return new AlidayuConfig();
+    }
+
+    @Bean
+    public TaobaoClient taobaoClient(AlidayuConfig alidayuConfig) {
+        return new DefaultTaobaoClient(alidayuConfig.getServerUrl(), alidayuConfig.getAppKey(), alidayuConfig.getAppSecret());
+    }
+}

+ 14 - 1
user-service/src/main/java/com/diagbot/config/RedisConfigurer.java

@@ -30,6 +30,8 @@ public class RedisConfigurer extends CachingConfigurerSupport {
     private String databaseCache;
     @Value("${spring.redis.database.sms}")
     private String databaseSms;
+    @Value("${spring.redis.database.img}")
+    private String databaseImg;
     @Value("${spring.redis.host}")
     private  String host;
     @Value("${spring.redis.password}")
@@ -135,7 +137,7 @@ public class RedisConfigurer extends CachingConfigurerSupport {
 
 
     /**
-     * sms验证使用的redis
+     * 短信验证使用的redis
      *
      * @param factory
      * @return
@@ -145,6 +147,17 @@ public class RedisConfigurer extends CachingConfigurerSupport {
         return getRedisTemplate(factory, Integer.valueOf(databaseSms));
     }
 
+    /**
+     * 图片验证使用的redis
+     *
+     * @param factory
+     * @return
+     */
+    @Bean(name = "redisTemplateForImg")
+    public RedisTemplate<String, Object> redisTemplateForImg(JedisConnectionFactory factory) {
+        return getRedisTemplate(factory, Integer.valueOf(databaseImg));
+    }
+
     private RedisTemplate<String, Object> getRedisTemplate(JedisConnectionFactory factory, Integer database) {
         JedisConnectionFactory factory2 = new JedisConnectionFactory();
         BeanUtil.copyProperties(factory, factory2);

+ 1 - 0
user-service/src/main/java/com/diagbot/config/ResourceServerConfigurer.java

@@ -26,6 +26,7 @@ public class ResourceServerConfigurer extends ResourceServerConfigurerAdapter {
                 .csrf().disable()
                 .authorizeRequests()
                 .regexMatchers(".*swagger.*",".*v2.*",".*webjars.*","/user/login.*","/user/registry.*","/user/test.*","/druid.*","/actuator.*","/hystrix.*","/hi.*").permitAll()
+                .antMatchers("/userver/getImgVerification").permitAll()
                 .antMatchers("/**").authenticated();
 //        .antMatchers("/**").permitAll();
     }

+ 1 - 0
user-service/src/main/java/com/diagbot/config/security/UrlAccessDecisionManager.java

@@ -39,6 +39,7 @@ public class UrlAccessDecisionManager implements AccessDecisionManager {
                 || matchers("/actuator/**", request)
                 || matchers("/hystrix/**", request)
                 || matchers("/hi/**", request)
+                || matchers("/userver/getImgVerification", request)
                 || matchers("/", request)){
                     return;
                 } else {

+ 24 - 0
user-service/src/main/java/com/diagbot/entity/ImgVerInfo.java

@@ -0,0 +1,24 @@
+package com.diagbot.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.Date;
+
+/**
+ * @Description: 图片验证码验证信息
+ * @author: gaodm
+ * @time: 2018/9/3 15:38
+ */
+@Getter
+@Setter
+public class ImgVerInfo {
+    //图片验证码唯一标志
+    private String imgId;
+    //验证码
+    private String code;
+    //创建时间
+    private Date createTime;
+    //验证码过期时间
+    private Date expireTime;
+}

+ 35 - 0
user-service/src/main/java/com/diagbot/facade/VerFacade.java

@@ -0,0 +1,35 @@
+package com.diagbot.facade;
+
+import com.diagbot.entity.ImgVerInfo;
+import com.diagbot.service.ImgVerService;
+import com.diagbot.service.SmsVerService;
+import com.diagbot.vo.ImgVerCreatVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 用户验证业务层
+ * @author: gaodm
+ * @time: 2018/9/4 19:31
+ */
+@Component
+public class VerFacade {
+    @Autowired
+    private ImgVerService imgVerService;
+    @Autowired
+    private SmsVerService smsVerService;
+
+    /**
+     * 获取用户图片验证信息
+     * @param imgVerCreatVO
+     * @return
+     */
+    public byte[] getImgVerification(ImgVerCreatVO imgVerCreatVO){
+        ImgVerInfo imgVerInfo = new ImgVerInfo();
+        imgVerInfo.setImgId(imgVerCreatVO.getImgId());
+        byte[] res = imgVerService.createImgVerification(imgVerInfo);
+        return res;
+    }
+
+
+}

+ 35 - 0
user-service/src/main/java/com/diagbot/service/ImgVerService.java

@@ -0,0 +1,35 @@
+package com.diagbot.service;
+
+import com.diagbot.entity.ImgVerInfo;
+
+/**
+ * @Description: 图片验证接口
+ * @author: gaodm
+ * @time: 2018/9/4 16:25
+ */
+public interface ImgVerService {
+
+    /**
+     * 创建用户图片验证信息
+     * @param imgVerInfo 图片验证码验证信息
+     * @return 图片验证码数据流
+     */
+    byte[] createImgVerification(ImgVerInfo imgVerInfo);
+
+    /**
+     * 验证用户图片验证码是否有效
+     */
+    Boolean verifyImgVerification(ImgVerInfo imgVerInfo);
+
+    /**
+     * 获取用户图片验证码信息
+     * @param imgId
+     * @return
+     */
+    ImgVerInfo getImgVerification(String imgId);
+
+    /**
+     * 删除用户图片验证码信息
+     */
+    Boolean deleteImgVerification(String imgId);
+}

+ 37 - 0
user-service/src/main/java/com/diagbot/service/SmsVerService.java

@@ -0,0 +1,37 @@
+package com.diagbot.service;
+
+import com.diagbot.entity.SwsVerInfo;
+
+/**
+ * @Description: 短信验证接口
+ * @author: gaodm
+ * @time: 2018/9/4 16:25
+ */
+public interface SmsVerService {
+    /**
+     * 发送短信信息
+     */
+    String smsSend(String mobile, String smsTemplateCode);
+
+    /**
+     * 创建用户短信验证信息
+     */
+    Boolean createSmsVerification(SwsVerInfo swsVerInfo);
+
+    /**
+     * 验证用户短信验证码是否有效
+     */
+    Boolean verifySmsVerification(SwsVerInfo swsVerInfo);
+
+    /**
+     * 获取用户短信验证码信息
+     * @param userId 用户ID
+     * @return 用户信息
+     */
+    SwsVerInfo getSmsVerification(Integer userId);
+
+    /**
+     * 删除用户短信验证码信息
+     */
+    Boolean deleteSmsVerification(Integer userId);
+}

+ 182 - 0
user-service/src/main/java/com/diagbot/service/impl/ImgVerServiceImpl.java

@@ -0,0 +1,182 @@
+package com.diagbot.service.impl;
+
+import com.diagbot.entity.ImgVerInfo;
+import com.diagbot.exception.CommonException;
+import com.diagbot.exception.ErrorCode;
+import com.diagbot.service.ImgVerService;
+import com.diagbot.util.DateUtil;
+import com.diagbot.util.StringUtil;
+import com.diagbot.util.VerifyCodeUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.dao.DataAccessException;
+import org.springframework.data.redis.connection.RedisConnection;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+
+/**
+ * @Description: 图片验证接口实现
+ * @author: gaodm
+ * @time: 2018/9/4 16:25
+ */
+@Service
+@Slf4j
+public class ImgVerServiceImpl implements ImgVerService {
+    @Autowired
+    @Qualifier("redisTemplateForImg")
+    private RedisTemplate redisForImg;
+
+    private byte[] serializeKey(Object o) {
+        return redisForImg.getKeySerializer().serialize(o);
+    }
+
+    private byte[] serializeValue(Object o) {
+        return redisForImg.getValueSerializer().serialize(o);
+    }
+
+    private Object deserializeValue(byte[] b) {
+        return redisForImg.getValueSerializer().deserialize(b);
+    }
+
+    private byte[] getUserImgKey(String imgId) {
+        String userImgFormat = "user_img_%s";
+        return serializeKey(String.format(userImgFormat, imgId));
+    }
+
+
+    /**
+     * 创建用户图片验证信息
+     * @param imgVerInfo 图片验证码验证信息
+     * @return 图片验证码数据流
+     */
+    @Override
+    public byte[] createImgVerification(ImgVerInfo imgVerInfo){
+        if (null == imgVerInfo){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户图片验证信息不能为空!");
+        }
+        if (StringUtil.isBlank(imgVerInfo.getImgId())){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "图片验证码唯一标志不能为空!");
+        }
+        //生成图片验证码
+        Object[] obj = VerifyCodeUtils.createImage();
+        imgVerInfo.setCode(obj[0].toString());
+        imgVerInfo.setCreateTime(DateUtil.now());
+        final Date expireDate = DateUtil.addMinutes(DateUtil.now(), 20);
+        imgVerInfo.setExpireTime(expireDate);
+        final byte[] redis_key = getUserImgKey(imgVerInfo.getImgId());
+        redisForImg.execute(new RedisCallback<Object>() {
+            @Override
+            public Object doInRedis(RedisConnection connection) throws DataAccessException {
+                //获取旧的
+                byte[] bytes = connection.get(redis_key);
+                //删除旧的
+                if (bytes != null) {
+                    connection.del(bytes);
+                }
+                //设置新的
+                connection.setEx(
+                        redis_key,
+                        (expireDate.getTime() - DateUtil.now().getTime()) / 1000,
+                        serializeValue(imgVerInfo)
+                );
+                return true;
+            }
+        });
+        return (byte[]) obj[1];
+    }
+
+    /**
+     * 验证短信验证码是否有效
+     */
+    @Override
+    public Boolean verifyImgVerification(ImgVerInfo imgVerInfo){
+        if (null == imgVerInfo){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户图片验证信息不能为空!");
+        }
+        if (StringUtil.isBlank(imgVerInfo.getImgId())){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "图片验证码唯一标志不能为空!");
+        }
+        if (StringUtil.isBlank(imgVerInfo.getCode())){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "验证码不能为空!");
+        }
+        //从redis中取出
+        final byte[] redis_key = getUserImgKey(imgVerInfo.getImgId());
+        ImgVerInfo imgVerInfoRes = (ImgVerInfo) redisForImg.execute(new RedisCallback<ImgVerInfo>() {
+            @Override
+            public ImgVerInfo doInRedis(RedisConnection connection) throws DataAccessException {
+                byte[] bytes = connection.get(redis_key);
+                if (bytes == null) {
+                    return null;
+                }
+                return (ImgVerInfo) deserializeValue(bytes);
+            }
+        });
+        if (imgVerInfoRes == null) {
+            log.info("用户图片验证信息不存在!");
+            throw new CommonException(ErrorCode.NOT_EXISTS,
+                    "用户短信验证信息不存在!");
+        }
+        //判断是否是想要的对象
+        if (!imgVerInfoRes.getImgId().equals(imgVerInfo.getImgId())) {
+            log.info("图片验证码唯一标志不一致!用户电话:{}", imgVerInfo.getImgId());
+            throw new CommonException(ErrorCode.PARAM_IS_ERROR,
+                    "图片验证码唯一标志不一致!");
+        }
+        if (!imgVerInfoRes.getCode().equals(imgVerInfo.getCode())) {
+            log.info("用户图片验证码不一致!验证码:{}", imgVerInfo.getCode());
+            throw new CommonException(ErrorCode.PARAM_IS_ERROR,
+                    "用户图片验证码不一致!");
+        }
+        //判断是否过期
+        if (!DateUtil.after(imgVerInfoRes.getExpireTime(), DateUtil.now())) {
+            log.info("图片验证码已过期,请重新获取!过期时间:{}", imgVerInfoRes.getExpireTime());
+            throw new CommonException(ErrorCode.PARAM_IS_ERROR,
+                    "图片已过期,请重新获取!");
+        }
+        return true;
+    }
+
+    /**
+     * 获取用户短信验证码信息
+     * @param imgId
+     * @return
+     */
+    @Override
+    public ImgVerInfo getImgVerification(String imgId){
+        return (ImgVerInfo) redisForImg.execute(new RedisCallback<Object>() {
+            @Override
+            public Object doInRedis(RedisConnection connection) throws DataAccessException {
+                byte[] redis_key = getUserImgKey(imgId);
+                byte[] bytes = connection.get(redis_key);
+                if (bytes == null) {
+                    return null;
+                }
+                return deserializeValue(bytes);
+            }
+        });
+    }
+
+    /**
+     * 删除用户短信验证码信息
+     */
+    @Override
+    public Boolean deleteImgVerification(String imgId){
+        final byte[] redis_key = getUserImgKey(imgId);
+        Long l = (Long) redisForImg.execute(new RedisCallback<Long>() {
+            @Override
+            public Long doInRedis(RedisConnection connection) throws DataAccessException {
+                return connection.del(redis_key);
+            }
+        });
+        return l > 0;
+    }
+}

+ 224 - 0
user-service/src/main/java/com/diagbot/service/impl/SmsVerServiceImpl.java

@@ -0,0 +1,224 @@
+package com.diagbot.service.impl;
+
+import com.diagbot.entity.SwsVerInfo;
+import com.diagbot.exception.CommonException;
+import com.diagbot.exception.ErrorCode;
+import com.diagbot.service.SmsVerService;
+import com.diagbot.util.DateUtil;
+import com.diagbot.util.GsonUtil;
+import com.diagbot.util.SmsCodeUtil;
+import com.diagbot.util.StringUtil;
+import com.taobao.api.TaobaoClient;
+import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
+import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.dao.DataAccessException;
+import org.springframework.data.redis.connection.RedisConnection;
+import org.springframework.data.redis.core.RedisCallback;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+
+/**
+ * @Description:
+ * 短信验证接口实现
+ * 阿里大于验证码发送
+ * @author: gaodm
+ * @time: 2018/9/4 16:25
+ */
+@Service
+@Slf4j
+public class SmsVerServiceImpl implements SmsVerService {
+    @Autowired
+    @Qualifier("redisTemplateForSms")
+    private RedisTemplate redisForSms;
+
+    @Autowired
+    private TaobaoClient taobaoClient;
+
+    private byte[] serializeKey(Object o) {
+        return redisForSms.getKeySerializer().serialize(o);
+    }
+
+    private byte[] serializeValue(Object o) {
+        return redisForSms.getValueSerializer().serialize(o);
+    }
+
+    private Object deserializeValue(byte[] b) {
+        return redisForSms.getValueSerializer().deserialize(b);
+    }
+
+    private byte[] getUserSmsKey(int userId) {
+        String userSmsFormat = "user_sms_%d";
+        return serializeKey(String.format(userSmsFormat, userId));
+    }
+
+    public String smsSend(String mobile,String smsTemplateCode){
+        if (StringUtil.isBlank(mobile)){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "电话号码不能为空!");
+        }
+        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
+        req.setSmsType( "normal" );
+        req.setSmsFreeSignName( "朗通云平台" );
+        String code = SmsCodeUtil.getVerCode();
+        String json="{\"code\":\""
+                + code
+                + "\",\"product\":\"【朗通云平台】\"}";
+        req.setSmsParamString(json);
+        req.setRecNum(mobile);
+        req.setSmsTemplateCode(smsTemplateCode);
+        try {
+            AlibabaAliqinFcSmsNumSendResponse rsp = taobaoClient.execute(req);
+            System.out.println(GsonUtil.toJson(rsp));
+            if (null == rsp || !rsp.isSuccess()){
+                throw new CommonException(ErrorCode.SMS_SEND_ERROR);
+            }
+        } catch (Exception e) {
+            throw new CommonException(ErrorCode.SMS_SEND_ERROR);
+        }
+        return code;
+    }
+
+    /**
+     * 创建用户短信验证信息
+     */
+    @Override
+    public Boolean createSmsVerification(SwsVerInfo swsVerInfo){
+        if (null == swsVerInfo){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户短信验证信息不能为空!");
+        }
+        if (null == swsVerInfo.getUserId()){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户ID不能为空!");
+        }
+        if (StringUtil.isBlank(swsVerInfo.getMobile())){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户电话不能为空!");
+        }
+        if (StringUtil.isBlank(swsVerInfo.getCode())){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "验证码不能为空!");
+        }
+        swsVerInfo.setCreateTime(DateUtil.now());
+        final Date expireDate = DateUtil.addMinutes(DateUtil.now(), 20);
+        swsVerInfo.setExpireTime(expireDate);
+        final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
+        redisForSms.execute(new RedisCallback<Object>() {
+            @Override
+            public Object doInRedis(RedisConnection connection) throws DataAccessException {
+                //获取旧的
+                byte[] bytes = connection.get(redis_key);
+                //删除旧的
+                if (bytes != null) {
+                    connection.del(bytes);
+                }
+                //设置新的
+                connection.setEx(
+                        redis_key,
+                        (expireDate.getTime() - DateUtil.now().getTime()) / 1000,
+                        serializeValue(swsVerInfo)
+                );
+                return true;
+            }
+        });
+        return true;
+    }
+
+    /**
+     * 验证短信验证码是否有效
+     */
+    @Override
+    public Boolean verifySmsVerification(SwsVerInfo swsVerInfo){
+        if (null == swsVerInfo){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户短信验证信息不能为空!");
+        }
+        if (null == swsVerInfo.getUserId()){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户ID不能为空!");
+        }
+        if (StringUtil.isBlank(swsVerInfo.getMobile())){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "用户电话不能为空!");
+        }
+        if (StringUtil.isBlank(swsVerInfo.getCode())){
+            throw new CommonException(ErrorCode.PARAM_IS_NULL,
+                    "短信验证码不能为空!");
+        }
+        //从redis中取出
+        final byte[] redis_key = getUserSmsKey(swsVerInfo.getUserId());
+        SwsVerInfo swsVerInfoRes = (SwsVerInfo) redisForSms.execute(new RedisCallback<SwsVerInfo>() {
+            @Override
+            public SwsVerInfo doInRedis(RedisConnection connection) throws DataAccessException {
+                byte[] bytes = connection.get(redis_key);
+                if (bytes == null) {
+                    return null;
+                }
+                return (SwsVerInfo) deserializeValue(bytes);
+            }
+        });
+        if (swsVerInfoRes == null) {
+            log.info("用户短信验证信息不存在!用户ID:{}", swsVerInfo.getUserId());
+            throw new CommonException(ErrorCode.NOT_EXISTS,
+                    "用户短信验证信息不存在!");
+        }
+        //判断是否是想要的对象
+        if (!swsVerInfoRes.getMobile().equals(swsVerInfo.getMobile())) {
+            log.info("用户电话不一致!用户ID:{},用户电话:{}", swsVerInfo.getUserId(), swsVerInfo.getMobile());
+            throw new CommonException(ErrorCode.PARAM_IS_ERROR,
+                    "用户电话不一致!");
+        }
+        if (!swsVerInfoRes.getCode().equals(swsVerInfo.getCode())) {
+            log.info("用户短信验证码不一致!用户ID:{},验证码:{}", swsVerInfo.getUserId(),swsVerInfo.getCode());
+            throw new CommonException(ErrorCode.PARAM_IS_ERROR,
+                    "用户短息验证码不一致!");
+        }
+        //判断是否过期
+        if (!DateUtil.after(swsVerInfoRes.getExpireTime(), DateUtil.now())) {
+            log.info("短息验证码已过期,请重新获取!用户ID:{},过期时间:{}", swsVerInfo.getUserId(), swsVerInfoRes.getExpireTime());
+            throw new CommonException(ErrorCode.PARAM_IS_ERROR,
+                    "短息验证码已过期,请重新获取!");
+        }
+        return true;
+    }
+
+    /**
+     * 获取用户短信验证码信息
+     * @param userId
+     * @return
+     */
+    @Override
+    public SwsVerInfo getSmsVerification(Integer userId){
+        return (SwsVerInfo) redisForSms.execute(new RedisCallback<Object>() {
+            @Override
+            public Object doInRedis(RedisConnection connection) throws DataAccessException {
+                byte[] redis_key = getUserSmsKey(userId);
+                byte[] bytes = connection.get(redis_key);
+                if (bytes == null) {
+                    return null;
+                }
+                return deserializeValue(bytes);
+            }
+        });
+    }
+
+    /**
+     * 删除用户短信验证码信息
+     */
+    @Override
+    public Boolean deleteSmsVerification(Integer userId){
+        final byte[] redis_key = getUserSmsKey(userId);
+        Long l = (Long) redisForSms.execute(new RedisCallback<Long>() {
+            @Override
+            public Long doInRedis(RedisConnection connection) throws DataAccessException {
+                return connection.del(redis_key);
+            }
+        });
+        return l > 0;
+    }
+}

+ 0 - 1
user-service/src/main/java/com/diagbot/util/VerifyCodeUtils.java

@@ -11,7 +11,6 @@ import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.ObjectOutputStream;
 import java.io.OutputStream;
 import java.util.Arrays;
 import java.util.Random;

+ 16 - 0
user-service/src/main/java/com/diagbot/vo/ImgVerCreatVO.java

@@ -0,0 +1,16 @@
+package com.diagbot.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @Description:
+ * @author: gaodm
+ * @time: 2018/9/4 19:26
+ */
+@Getter
+@Setter
+public class ImgVerCreatVO {
+    //图片验证码唯一标志
+    private String imgId;
+}

+ 32 - 0
user-service/src/main/java/com/diagbot/web/VerController.java

@@ -0,0 +1,32 @@
+package com.diagbot.web;
+
+import com.diagbot.annotation.SysLogger;
+import com.diagbot.dto.RespDTO;
+import com.diagbot.facade.VerFacade;
+import com.diagbot.vo.ImgVerCreatVO;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @Description: 用户验证API
+ * @author: gaodm
+ * @time: 2018/9/4 19:30
+ */
+@RestController
+@RequestMapping("/userver")
+public class VerController {
+    @Autowired
+    private VerFacade verFacade;
+
+    @ApiOperation(value = "获取图片验证码", notes = "图片验证码唯一标志为必填项")
+    @GetMapping("/getImgVerification")
+    @SysLogger("getImgVerification")
+    public RespDTO getImgVerification(ImgVerCreatVO imgVerCreatVO){
+        return   RespDTO.onSuc(verFacade.getImgVerification(imgVerCreatVO));
+    }
+}

BIN
user-service/src/main/libs/taobao-sdk-java-auto_1455552377940-20170303.jar


+ 3 - 0
user-service/src/main/resources/alidayu.properties

@@ -0,0 +1,3 @@
+alidayu.serverUrl=http://gw.api.taobao.com/router/rest
+alidayu.appKey=12312321321
+alidayu.appSecret=33213215215365211wqdedsds