Helius
2022-07-04 2736d16a8b1804f7291a56a147f76ef3584d6619
src/main/java/com/xcong/farmer/cms/modules/system/service/Impl/CommonServiceImpl.java
@@ -8,17 +8,24 @@
import com.xcong.farmer.cms.common.response.Result;
import com.xcong.farmer.cms.configurations.properties.ApplicationProperties;
import com.xcong.farmer.cms.configurations.properties.SecurityProperties;
import com.xcong.farmer.cms.modules.member.entity.MemberEntity;
import com.xcong.farmer.cms.modules.system.dto.AdminLoginDto;
import com.xcong.farmer.cms.modules.system.entity.UserEntity;
import com.xcong.farmer.cms.modules.system.mapper.UserMapper;
import com.xcong.farmer.cms.modules.system.service.ICommonService;
import com.xcong.farmer.cms.modules.system.util.CaptchaUtil;
import com.xcong.farmer.cms.modules.system.util.LoginUserUtil;
import com.xcong.farmer.cms.utils.MessageSourceUtils;
import com.xcong.farmer.cms.modules.system.util.UUIDUtil;
import com.xcong.farmer.cms.utils.RedisUtils;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@@ -40,9 +47,33 @@
    private ApplicationProperties applicationProperties;
    @Resource
    private SecurityProperties securityProperties;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private UUIDUtil uuidUtil;
    @Autowired
    private CaptchaUtil captchaUtil;
    //从SpringBoot的配置文件中取出过期时间
    @Value("${server.servlet.session.timeout}")
    private Integer timeout;
    @Override
    public Result login(AdminLoginDto adminLoginDto) {
        //根据前端传回的token在redis中找对应的value
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        String codeToken = adminLoginDto.getCodeToken();
        String codeValue = adminLoginDto.getCodeValue();
        if (redisTemplate.hasKey(codeToken)) {
            //验证通过, 删除对应的key
            if (valueOperations.get(codeToken).equals(codeValue)) {
                redisTemplate.delete(codeToken);
            } else {
                return Result.fail("验证码不正确");
            }
        } else {
            return Result.fail("验证码已过期");
        }
        String username = adminLoginDto.getUsername();
        String password = adminLoginDto.getPassword();
        UserEntity userEntity = userMapper.selectByUserNameAndPassword(username, SecureUtil.md5(password));
@@ -93,6 +124,26 @@
        return Result.ok("退出成功");
    }
    @Override
    public Map<String, Object> createToken(String captcha) {
        //生成一个token
        String key = uuidUtil.getUUID32();
        //生成验证码对应的token  以token为key  验证码为value存在redis中
        ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
        valueOperations.set(key, captcha);
        //设置验证码过期时间
        redisTemplate.expire(key, timeout, TimeUnit.MINUTES);
        Map<String, Object> map = new HashMap<>();
        map.put("token", key);
        map.put("expire", timeout);
        return map;
    }
    @Override
    public Result captchaCreator() throws IOException {
        return captchaUtil.catchaImgCreator();
    }
    public String generateAsaToken(String token) {
        RSA rsa = new RSA(null, securityProperties.getPublicKey());
        return rsa.encryptBase64(token + "_" + System.currentTimeMillis(), KeyType.PublicKey);