2 Commits 5b7f6baef6 ... 6491df27d7

Author SHA1 Message Date
  liulingchao 6491df27d7 Merge remote-tracking branch 'origin/master' 4 years ago
  liulingchao 0979e6fe2a 接口api 4 years ago

+ 11 - 1
dgtly-apiframework/pom.xml

@@ -76,7 +76,17 @@
             <artifactId>swagger-models</artifactId>
             <version>1.5.21</version>
         </dependency>
-
+        <!--验证码 -->
+        <dependency>
+            <groupId>com.github.penggle</groupId>
+            <artifactId>kaptcha</artifactId>
+            <exclusions>
+                <exclusion>
+                    <artifactId>javax.servlet-api</artifactId>
+                    <groupId>javax.servlet</groupId>
+                </exclusion>
+            </exclusions>
+        </dependency>
         <!--jwt-->
         <dependency>
             <groupId>com.auth0</groupId>

+ 83 - 0
dgtly-apiframework/src/main/java/com/dgtly/apiframework/config/CaptchaConfig.java

@@ -0,0 +1,83 @@
+package com.dgtly.apiframework.config;
+
+import java.util.Properties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import com.google.code.kaptcha.impl.DefaultKaptcha;
+import com.google.code.kaptcha.util.Config;
+import static com.google.code.kaptcha.Constants.*;
+
+/**
+ * 验证码配置
+ *
+ * @author ruoyi
+ */
+@Configuration
+public class CaptchaConfig
+{
+    @Bean(name = "captchaProducer")
+    public DefaultKaptcha getKaptchaBean()
+    {
+        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
+        Properties properties = new Properties();
+        // 是否有边框 默认为true 我们可以自己设置yes,no
+        properties.setProperty(KAPTCHA_BORDER, "yes");
+        // 验证码文本字符颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
+        // 验证码图片宽度 默认为200
+        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
+        // 验证码图片高度 默认为50
+        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
+        // 验证码文本字符大小 默认为40
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
+        // KAPTCHA_SESSION_KEY
+        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
+        // 验证码文本字符长度 默认为5
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
+        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
+        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
+        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
+        Config config = new Config(properties);
+        defaultKaptcha.setConfig(config);
+        return defaultKaptcha;
+    }
+
+    @Bean(name = "captchaProducerMath")
+    public DefaultKaptcha getKaptchaBeanMath()
+    {
+        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
+        Properties properties = new Properties();
+        // 是否有边框 默认为true 我们可以自己设置yes,no
+        properties.setProperty(KAPTCHA_BORDER, "yes");
+        // 边框颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_BORDER_COLOR, "105,179,90");
+        // 验证码文本字符颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "blue");
+        // 验证码图片宽度 默认为200
+        properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
+        // 验证码图片高度 默认为50
+        properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
+        // 验证码文本字符大小 默认为40
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "35");
+        // KAPTCHA_SESSION_KEY
+        properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
+        // 验证码文本生成器
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.dgtly.framework.config.KaptchaTextCreator");
+        // 验证码文本字符间距 默认为2
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
+        // 验证码文本字符长度 默认为5
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "6");
+        // 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
+        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
+        // 验证码噪点颜色 默认为Color.BLACK
+        properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
+        // 干扰实现类
+        properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
+        // 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
+        properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
+        Config config = new Config(properties);
+        defaultKaptcha.setConfig(config);
+        return defaultKaptcha;
+    }
+}

+ 92 - 0
dgtly-member/dgtly-member-api/src/main/java/com/dgtly/member/controller/CaptchaController.java

@@ -0,0 +1,92 @@
+package com.dgtly.member.controller;
+
+import com.dgtly.common.core.controller.BaseController;
+import com.google.code.kaptcha.Constants;
+import com.google.code.kaptcha.Producer;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.servlet.ModelAndView;
+
+import javax.annotation.Resource;
+import javax.imageio.ImageIO;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+
+@Controller
+@RequestMapping("/captcha/info")
+public class CaptchaController extends BaseController
+{
+    @Resource(name = "captchaProducer")
+    private Producer captchaProducer;
+
+   /* @Resource(name = "captchaProducerMath")
+    private Producer captchaProducerMath;*/
+
+    /**
+     * 验证码生成
+     */
+    @GetMapping(value = "/captchaImage")
+    public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response)
+    {
+        ServletOutputStream out = null;
+        try
+        {
+            HttpSession session = request.getSession();
+            response.setDateHeader("Expires", 0);
+            response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
+            response.addHeader("Cache-Control", "post-check=0, pre-check=0");
+            response.setHeader("Pragma", "no-cache");
+            response.setContentType("image/jpeg");
+            String capStr = null;
+            String code = null;
+            BufferedImage bi = null;
+            capStr = code = captchaProducer.createText();
+            bi = captchaProducer.createImage(capStr);
+
+
+           /* String type = request.getParameter("type");
+
+            if ("math".equals(type))
+            {
+                String capText = captchaProducerMath.createText();
+                capStr = capText.substring(0, capText.lastIndexOf("@"));
+                code = capText.substring(capText.lastIndexOf("@") + 1);
+                bi = captchaProducerMath.createImage(capStr);
+            }
+            else if ("char".equals(type))
+            {
+                capStr = code = captchaProducer.createText();
+                bi = captchaProducer.createImage(capStr);
+            }*/
+            session.setAttribute(Constants.KAPTCHA_SESSION_KEY, code);
+            out = response.getOutputStream();
+            ImageIO.write(bi, "jpg", out);
+            out.flush();
+
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+        }
+        finally
+        {
+            try
+            {
+                if (out != null)
+                {
+                    out.close();
+                }
+            }
+            catch (IOException e)
+            {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+}

+ 33 - 12
dgtly-member/dgtly-member-api/src/main/java/com/dgtly/member/controller/MemberInfoController.java

@@ -2,15 +2,22 @@ package com.dgtly.member.controller;
 
 import com.auth0.jwt.JWT;
 import com.auth0.jwt.algorithms.Algorithm;
+import com.dgtly.common.annotation.ApiNoCheckSign;
+import com.dgtly.common.annotation.ApiPassToken;
 import com.dgtly.common.core.controller.ApiBaseController;
 import com.dgtly.common.core.domain.AjaxResult;
 import com.dgtly.common.core.domain.ParameterObject;
 import com.dgtly.common.utils.DateUtils;
 import com.dgtly.member.domain.MemberInfo;
 import com.dgtly.member.service.IMemberInfoService;
+import com.google.code.kaptcha.Constants;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
 import java.util.Date;
 
 /**
@@ -22,6 +29,10 @@ import java.util.Date;
  * @date 2020-2-25 14:41
  * @since JDK1.8
  */
+@RestController
+@RequestMapping("/member/info")
+@ApiPassToken/* 不用验证token  方法上可用*/
+@ApiNoCheckSign/* 不用验证 签名 方法上可用*/
 public class MemberInfoController extends ApiBaseController {
 
     @Autowired
@@ -48,7 +59,7 @@ public class MemberInfoController extends ApiBaseController {
         if(memberInfo1 != null){
             return AjaxResult.error(302,"手机号码已存在");
         }
-
+        memberInfo.setLoginTime(DateUtils.getNowDate());
         memberInfo.setCreateTime(DateUtils.getNowDate());
         int flag = iMemberInfoService.insertMemberInfo(memberInfo);
         return toAjax(flag);
@@ -66,19 +77,29 @@ public class MemberInfoController extends ApiBaseController {
     public Object memberLogin(){
         ParameterObject obj =  getParameterObject();
         MemberInfo memberInfo = obj.parseBean(MemberInfo.class);
-        MemberInfo memberInfo1 = iMemberInfoService.memberLogin(memberInfo);
-        if(memberInfo1 == null){
-            return AjaxResult.error(303,"登录账号或密码错误");
+       //获取验证码
+        HttpServletRequest request = getRequest();
+        HttpSession httpSession = request.getSession();
+        String key = httpSession.getAttribute(Constants.KAPTCHA_SESSION_KEY)+"";
+        //验证码验证
+
+        if(null != key && key.equals(memberInfo.getValidateCode())){
+            MemberInfo memberInfo1 = iMemberInfoService.memberLogin(memberInfo);
+            if(memberInfo1 == null){
+                return AjaxResult.error(304,"登录账号或密码错误");
+            }
+            Date start = new Date();
+            long currentTime = System.currentTimeMillis() + 12*60* 60 * 1000;//12小时有效时间
+            Date end = new Date(currentTime);
+            String token = JWT.create().withAudience(memberInfo1.getId()+"")
+                    .withIssuedAt(start)
+                    .withExpiresAt(end)
+                    .sign(Algorithm.HMAC256(memberInfo1.getLoginPassword()));
+
+            return AjaxResult.success().putKV("token",token);
         }
-        Date start = new Date();
-        long currentTime = System.currentTimeMillis() + 12*60* 60 * 1000;//12小时有效时间
-        Date end = new Date(currentTime);
-        String token = JWT.create().withAudience(memberInfo1.getId()+"")
-                .withIssuedAt(start)
-                .withExpiresAt(end)
-                .sign(Algorithm.HMAC256(memberInfo1.getLoginPassword()));
 
-        return AjaxResult.success("登录成功",token);
+       return AjaxResult.error(303,"验证码错误");
 
     }
 }

+ 12 - 1
dgtly-member/dgtly-member-common/src/main/java/com/dgtly/member/domain/MemberInfo.java

@@ -36,7 +36,18 @@ public class MemberInfo extends BaseEntity
     @Excel(name = "登陆时间", width = 30, dateFormat = "yyyy-MM-dd")
     private Date loginTime;
 
-    public void setId(Long id) 
+    //验证码
+    private String validateCode;
+
+    public String getValidateCode() {
+        return validateCode;
+    }
+
+    public void setValidateCode(String validateCode) {
+        this.validateCode = validateCode;
+    }
+
+    public void setId(Long id)
     {
         this.id = id;
     }