工具类
package com. wego. controller ; import cn. dev33. satoken. annotation. SaIgnore ;
import cn. dev33. satoken. stp. StpUtil ;
import com. baomidou. mybatisplus. core. conditions. query. QueryWrapper ;
import com. baomidou. mybatisplus. core. toolkit. Wrappers ;
import com. google. code. kaptcha. impl. DefaultKaptcha ;
import com. wego. bean. ResultBean ;
import com. wego. constant. CommonState ;
import com. wego. constant. WeGoConstant ;
import com. wego. converter. UserConverter ;
import com. wego. dto. UserLoginDTO ;
import com. wego. dto. UserRegisterDTO ;
import com. wego. entity. User ;
import com. wego. service. UserService ;
import com. wego. toolkits. redis. util. JsonRedisUtil ;
import com. wego. toolkits. validator. ValidatorUtil ;
import com. wego. utils. JsonUtil ;
import com. wego. utils. PasswordUtil ;
import com. wego. utils. RandomUtil ;
import com. wego. utils. RequestUtil ;
import com. wego. utils. ResultBeanUtil ;
import com. wego. vo. UserTokenVO ;
import jakarta. annotation. Resource ;
import jakarta. validation. constraints. NotBlank ;
import java. awt. image. BufferedImage ;
import java. io. ByteArrayOutputStream ;
import java. io. IOException ;
import java. time. LocalDateTime ;
import java. util. Base64 ;
import javax. imageio. ImageIO ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. PostMapping ;
import org. springframework. web. bind. annotation. RequestBody ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. ResponseBody ;
import org. springframework. web. bind. annotation. RestController ;
@RestController
@RequestMapping ( "/user" )
public class UserController { @Resource private DefaultKaptcha defaultKaptcha; @Resource private UserService userService; @Resource private UserConverter userConverter; @Resource private JsonRedisUtil < String > jsonRedisUtil; @SaIgnore @PostMapping ( "/v1/login" ) public ResultBean < UserTokenVO > login ( @RequestBody UserLoginDTO userLoginDTO) { ValidatorUtil . validateEntity ( userLoginDTO) ; final QueryWrapper < User > queryWrapper = Wrappers . < User > query ( ) . eq ( "account" , userLoginDTO. getAccount ( ) ) . eq ( "state" , CommonState . POSITIVE ) ; final User user = userService. getOne ( queryWrapper) ; if ( user == null ) { return ResultBeanUtil . < UserTokenVO > error ( ) . setMsg ( "账户不存在或账户被禁用" ) ; } final String encryptPassword = PasswordUtil . encrypt ( userLoginDTO. getPassword ( ) , user. getSalt ( ) ) ; if ( encryptPassword. equals ( user. getPassword ( ) ) ) { final UserTokenVO userTokenVO = userConverter. user2userTokenVO ( user) ; StpUtil . login ( JsonUtil . obj2String ( userTokenVO) ) ; final String tokenValue = StpUtil . getTokenValue ( ) ; return ResultBeanUtil . < UserTokenVO > success ( ) . setMsg ( "登录成功!" ) . addData ( "token" , tokenValue, "user" , userTokenVO) ; } return ResultBeanUtil . < UserTokenVO > error ( ) . setMsg ( "登录失败!" ) ; } @GetMapping ( "/v1/verifyCode" ) public ResultBean < String > getVerifyCode ( ) throws IOException { String verifyCode = defaultKaptcha. createText ( ) ; final String ip = RequestUtil . getIp ( ) ; jsonRedisUtil. set ( ip + "-verifyCode" , verifyCode, WeGoConstant . VERIFY_CODE_TTL ) ; BufferedImage image = defaultKaptcha. createImage ( verifyCode) ; ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ; ImageIO . write ( image, "jpg" , out) ; return ResultBeanUtil . success ( Base64 . getEncoder ( ) . encodeToString ( out. toByteArray ( ) ) ) ; } @ResponseBody @GetMapping ( "/v1/checkVerifyCode" ) public ResultBean < String > checkVerifyCode ( @NotBlank ( message = "校验码不能为空" ) String verifyCode) { final String ip = RequestUtil . getIp ( ) ; final String redisVerifyCode = jsonRedisUtil. get ( ip + "-verifyCode" ) ; if ( redisVerifyCode == null ) { return ResultBeanUtil . error ( "请刷新页面生成验证码后再次请求!" ) ; } else { if ( redisVerifyCode. equalsIgnoreCase ( verifyCode) ) { return ResultBeanUtil . success ( "验证码正确!" ) ; } else { return ResultBeanUtil . error ( "请刷新页面生成验证码后再次请求!" ) ; } } } }
测试代码
@PostMapping ( "/v1/register" ) public ResultBean < String > register ( @RequestBody UserRegisterDTO userRegisterDTO) { ValidatorUtil . validateEntity ( userRegisterDTO) ; if ( ! userRegisterDTO. getPassword1 ( ) . equals ( userRegisterDTO. getPassword2 ( ) ) ) { return ResultBeanUtil . error ( "两次密码不一致" ) ; } boolean res; final String ip = RequestUtil . getIp ( ) ; final String veriyCode = jsonRedisUtil. get ( ip + "-verifyCode" ) ; if ( veriyCode == null ) { res = false ; } else { res = veriyCode. equalsIgnoreCase ( userRegisterDTO. getVerifyCode ( ) ) ; } final String salt = RandomUtil . genString ( 5 , 22 ) ; final String encryptPassword = PasswordUtil . encrypt ( userRegisterDTO. getPassword1 ( ) , salt) ; final User user = User . builder ( ) . account ( userRegisterDTO. getAccount ( ) ) . salt ( salt) . password ( encryptPassword) . state ( CommonState . POSITIVE ) . createTime ( LocalDateTime . now ( ) ) . updateTime ( LocalDateTime . now ( ) ) . build ( ) ; res = res && userService. save ( user) ; if ( res) { return ResultBeanUtil . success ( "用户注册成功!" ) ; } return ResultBeanUtil . error ( "用户注册失败!" ) ; }
测试结果