Commit 5b5d46ca authored by dmy's avatar dmy

复杂密码以及验证码

parent 25226f47
......@@ -47,6 +47,11 @@
<groupId>com.pangding.web</groupId>
<artifactId>pd-partner-ref</artifactId>
</dependency>
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.yanzuoguang</groupId>
<artifactId>yzg-util-redis</artifactId>
......
......@@ -2,6 +2,7 @@ package com.pangding.web;
import com.alicp.jetcache.anno.config.EnableCreateCacheAnnotation;
import com.alicp.jetcache.anno.config.EnableMethodCache;
import com.yanzuoguang.util.helper.YzgLanguage;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
......@@ -42,5 +43,6 @@ public class UserApp implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
YzgLanguage.get().setDefaultLanguage(YzgLanguage.LANGUAGE_CH);
}
}
\ No newline at end of file
......@@ -2,6 +2,8 @@ package com.pangding.web.authority.controller;
import com.pangding.web.authority.service.RegisterService;
import com.pangding.web.authority.service.VerifyCodeService;
import com.pangding.web.authority.vo.code.CodeService;
import com.pangding.web.authority.vo.code.CodeVo;
import com.pangding.web.vo.system.req.LoginReqVo;
import com.pangding.web.vo.system.res.LoginResVo;
import com.yanzuoguang.util.helper.CheckerHelper;
......@@ -10,10 +12,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
......@@ -29,6 +28,8 @@ public class LoginController {
RegisterService registerServiceImpl;
@Autowired
VerifyCodeService verifyCodeServiceImpl;
@Autowired
CodeService codeService;
/**
* 登陆,验证用户名或手机号,密码是否正确
......@@ -69,4 +70,15 @@ public class LoginController {
verifyCodeServiceImpl.getImage(request,response);
return new ResponseResult();
}*/
/**
* 获取验证码
*
* @return 登录结果
*/
@GetMapping(value = "/getCode", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiOperation(value = "获取验证码", notes = "获取验证码")
public ResponseResult<CodeVo> getCode() {
return ResponseResult.result(codeService.getCode());
}
}
......@@ -66,6 +66,7 @@ public class CompanyMoneyDaoImpl extends BaseDaoImpl implements CompanyMoneyDao
}
@Override
public PageSizeData<CompanyMoneyVo> findCompanyMoneyPage(CompanyMoneyReqVo req) {
return this.queryPage(CompanyMoneyVo.class, req, QUERY_LIST, req);
......
......@@ -16,8 +16,6 @@ public class CompanyMsgDaoImpl extends BaseDaoImpl implements CompanyMsgDao {
private static final String QUERY_LIST_PAGE = "QUERY_LIST_PAGE";
private static final String QUERY_LIST = "QUERY_LIST";
@Override
protected void init() {
......
......@@ -8,6 +8,7 @@ import com.pangding.web.authority.feign.PayFeign;
import com.pangding.web.authority.feign.StoreFeign;
import com.pangding.web.authority.service.*;
import com.pangding.web.authority.util.*;
import com.pangding.web.authority.vo.code.CodeService;
import com.pangding.web.authority.vo.reqvo.*;
import com.pangding.web.authority.vo.resvo.SaveCompanyResVo;
import com.pangding.web.constant.CompanyConstant;
......@@ -80,6 +81,8 @@ public class RegisterServiceImpl implements RegisterService {
private TokenService tokenService;
@Autowired
private CompanyService companyService;
@Autowired
private CodeService codeService;
/**
* 判断注册编码是否存在
......@@ -393,6 +396,11 @@ public class RegisterServiceImpl implements RegisterService {
*/
@Override
public LoginResVo login(LoginReqVo reqVo) throws Exception {
// 当输入用户名和密码时,检测验证码
List<String> items = new ArrayList<>();
items.add(reqVo.getAccountOrPhone());
items.add(reqVo.getPassword());
codeService.validate(reqVo.getCodeId(), reqVo.getCodeValue(), items);
UserReqVo userReqVo = new UserReqVo();
userReqVo.setLoginAccount(reqVo.getAccountOrPhone());
......
......@@ -413,7 +413,7 @@ public class UserServiceImpl implements UserService {
userVo.setPassword(this.passwordEncoder(webUserReqVo.getPassword()));
}
//短信验证,通过后才可以
String verifyCode = verifyCodeCache.get(userVo.getPhone());
String verifyCode = verifyCodeCache.get(webUserReqVo.getPhone());
if (!webUserReqVo.getVerifyCode().equals(verifyCode)) {
throw new CodeException("验证码错误");
}
......
package com.pangding.web.authority.vo;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.Properties;
/**
* 验证码生成配置
*
* @author 颜佐光
*/
@Component
public class KaptchConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
Properties properties = new Properties();
// 图片边框
properties.setProperty("kaptcha.border", "no");
// 边框颜色
properties.setProperty("kaptcha.border.color", "black");
//边框厚度
properties.setProperty("kaptcha.border.thickness", "1");
// 图片宽
properties.setProperty("kaptcha.image.width", "120");
// 图片高
properties.setProperty("kaptcha.image.height", "60");
//图片实现类
properties.setProperty("kaptcha.producer.impl", "com.google.code.kaptcha.impl.DefaultKaptcha");
//文本实现类
properties.setProperty("kaptcha.textproducer.impl", "com.google.code.kaptcha.text.impl.DefaultTextCreator");
//文本集合,验证码值从此集合中获取
// properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
properties.setProperty("kaptcha.textproducer.char.string", "23456789ABCDEFGHJKMNPQRSTUVWXYZ");
//验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
//字体
properties.setProperty("kaptcha.textproducer.font.names", "宋体");
//字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "black");
//文字间隔
properties.setProperty("kaptcha.textproducer.char.space", "2");
//干扰实现类
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise");
//干扰颜色
properties.setProperty("kaptcha.noise.color", "blue");
//干扰图片样式
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
//背景实现类
properties.setProperty("kaptcha.background.impl", "com.google.code.kaptcha.impl.DefaultBackground");
// 验证码背景颜色(开始颜色)
properties.setProperty("kaptcha.background-color.from", "lightGray");
// 验证码背景颜色(结束颜色)
properties.setProperty("kaptcha.background-color.to", "white");
//背景颜色渐变,结束颜色
properties.setProperty("kaptcha.background.clear.to", "white");
//文字渲染器
properties.setProperty("kaptcha.word.impl", "com.google.code.kaptcha.text.impl.DefaultWordRenderer");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
package com.pangding.web.authority.vo.code;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.pangding.web.cache.CacheName;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 验证码服务
*
*/
@Component
public class CodeService {
private final DefaultKaptcha defaultKaptcha;
@CreateCache(name = CacheName.USER_PASSWORD_CODE, cacheType = CacheType.REMOTE, expire = 60, timeUnit = TimeUnit.MINUTES)
private Cache<String, String> cache;
public CodeService(DefaultKaptcha defaultKaptcha) {
this.defaultKaptcha = defaultKaptcha;
}
/**
* 生成验证码
*
* @return 验证码效果
*/
public CodeVo getCode() {
// 生成文字验证码
String text = defaultKaptcha.createText();
// 生成图片验证码
BufferedImage image = defaultKaptcha.createImage(text);
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, "jpg", out);
} catch (IOException e) {
throw new RuntimeException(e);
}
// 对字节组Base64编码
String img = Base64.getEncoder().encodeToString(out.toByteArray());
CodeVo code = new CodeVo(StringHelper.getNewID(), img);
// if (debugConfig.isDebug()) {
// code.setCode(text);
// }
// 验证验证码文字
cache.put(code.getId(), text);
return code;
}
public void validate(String codeId, String codeValue, boolean validate) {
if (!validate) {
return;
}
if (StringHelper.isEmpty(codeId)) {
throw new CodeException("验证码不能为空");
}
String text = cache.get(codeId);
if (StringHelper.isEmpty(text)) {
throw new CodeException("验证码失效");
}
cache.remove(codeId);
if (StringHelper.compare(text, codeValue, true)) {
return;
}
throw new CodeException("验证码错误");
}
public void validate(String codeId, String codeValue, List<String> names) {
boolean validate = false;
for (String name : names) {
if (!StringHelper.isEmpty(name)) {
validate = true;
break;
}
}
this.validate(codeId, codeValue, validate);
}
}
package com.pangding.web.authority.vo.code;
import io.swagger.annotations.ApiModelProperty;
/**
* 返回实体
*
* */
public class CodeVo {
@ApiModelProperty(notes = "验证码序号")
private String id;
@ApiModelProperty(notes = "验证码图片,base64的图片")
private String img;
@ApiModelProperty(notes = "二维码内容,仅仅在调试模式下生效")
private String code;
public CodeVo() {
}
public CodeVo(String id, String img) {
this.id = id;
this.img = img;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
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