|
@@ -10,11 +10,14 @@ import com.futu.course.common.utils.TokenUtils;
|
|
|
import com.futu.course.user.domain.User;
|
|
|
import com.futu.course.user.dto.LoginDTO;
|
|
|
import com.futu.course.user.dto.UserDTO;
|
|
|
+import com.futu.course.user.dto.UserLoginDto;
|
|
|
+import com.futu.course.user.dto.UserRegisterDto;
|
|
|
import com.futu.course.user.service.UserService;
|
|
|
import com.futu.course.user.mapper.UserMapper;
|
|
|
import io.jsonwebtoken.Jwts;
|
|
|
import io.jsonwebtoken.SignatureAlgorithm;
|
|
|
import io.jsonwebtoken.security.Keys;
|
|
|
+import io.lettuce.core.RedisClient;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
@@ -23,10 +26,13 @@ import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
+import org.redisson.api.RLock;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.DigestUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.crypto.Cipher;
|
|
@@ -51,6 +57,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
|
|
|
@Autowired
|
|
|
private RedisTemplate redisTemplate;
|
|
|
+ @Autowired
|
|
|
+ private RedissonClient redissonClient;
|
|
|
|
|
|
// 填写上你的AppID,如何获取AppID自行百度,这步骤很简单
|
|
|
private final static String APP_ID = "wxdbcbc020c8e4b0d9";
|
|
@@ -146,6 +154,65 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
|
|
return R.ok("登出成功");
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public R login(UserLoginDto dto) throws InterruptedException {
|
|
|
+ RLock lock = redissonClient.getLock(dto.getMobile());
|
|
|
+ boolean b = lock.tryLock(3, TimeUnit.SECONDS);
|
|
|
+ if (!b)
|
|
|
+ {
|
|
|
+ return R.failed("请勿重复提交");
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getMobile, dto.getMobile()));
|
|
|
+ if (user == null)
|
|
|
+ {
|
|
|
+ return R.restResult(null,500,"用户不存在");
|
|
|
+ }
|
|
|
+ String salt = user.getSalt();
|
|
|
+ String password = dto.getPassword();
|
|
|
+ String hex = DigestUtils.md5DigestAsHex((salt + password).getBytes());
|
|
|
+ if (hex.equals(user.getPassword()))
|
|
|
+ {
|
|
|
+ // 生成一个 256 位(32 字节)的安全密钥
|
|
|
+ SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
|
|
|
+ // 使用密钥生成 JWT
|
|
|
+ String jws = Jwts.builder()
|
|
|
+ .setSubject(user.getId().toString())
|
|
|
+ .signWith(key)
|
|
|
+ .compact();
|
|
|
+ user.setToken(jws);
|
|
|
+ redisTemplate.opsForValue().set("user",user,1, TimeUnit.DAYS);
|
|
|
+ return R.ok(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ }catch (Exception e)
|
|
|
+ {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw e;
|
|
|
+ }finally {
|
|
|
+ if (lock.isLocked()&&lock.isHeldByCurrentThread())
|
|
|
+ {
|
|
|
+ lock.unlock();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+// @Override
|
|
|
+// public R register(UserRegisterDto dto) {
|
|
|
+// SnowflakeIdWorker worker=new SnowflakeIdWorker(1,1);
|
|
|
+// if (redisTemplate.opsForValue().get("user") != null)
|
|
|
+// {
|
|
|
+// return R.failed("用户已存在");
|
|
|
+// }
|
|
|
+// try {
|
|
|
+// User user = new User();
|
|
|
+// user.setId(worker.nextId());
|
|
|
+// }
|
|
|
+//
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+
|
|
|
|
|
|
public static String decryptData(String encryptedData, String sessionKey, String iv) throws Exception {
|
|
|
// Base64 解码
|