2025.4.22 redis 微信登录
This commit is contained in:
parent
c79dc93ba4
commit
0b58fa9969
@ -74,11 +74,13 @@ public class FileBrowserUtil {
|
||||
|
||||
/**
|
||||
* —— 第二步:上传文件 ——
|
||||
* curl -v -X POST \
|
||||
* "$DOMAIN/api/resources/$REMOTE_PATH?override=true" \ //服务器上相对路径
|
||||
* -H "X-Auth: $TOKEN" \
|
||||
* -F "data=@/path/to/local/photo.jpg" //photo.jpg 以 multipart/form-data 的格式上传
|
||||
+ * curl -v -X POST \
|
||||
+ * "$DOMAIN/api/resources/$REMOTE_PATH?override=true" \ // 服务器上相对路径
|
||||
+ * -H "X-Auth: $TOKEN" \
|
||||
+ * -H "Content-Type: image/jpeg" \ // 根据文件类型替换
|
||||
+ * --data-binary "@/path/to/local/photo.jpg" // 以 raw body 方式上传
|
||||
*/
|
||||
|
||||
public String uploadAndGetUrl(byte[] fileBytes, String fileName) throws IOException {
|
||||
// 1. 登录拿 token
|
||||
String token = login();
|
||||
@ -97,7 +99,7 @@ public class FileBrowserUtil {
|
||||
// 4. 构造上传 URL
|
||||
String uploadUrl = domain + "/api/resources/" + encodedPath + "?override=true";
|
||||
|
||||
// 5. 执行 Multipart upload
|
||||
// 5. 执行 upload
|
||||
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
||||
HttpPost post = new HttpPost(uploadUrl);
|
||||
post.setHeader("X-Auth", token);
|
||||
|
@ -12,11 +12,11 @@ import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
public class RedisConfiguration {
|
||||
@Bean
|
||||
public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
|
||||
log.info("开始创建redis模板对象");
|
||||
RedisTemplate redisTemplate=new RedisTemplate();
|
||||
//设置redis的连接工厂对象 连接工厂负责创建与 Redis 服务器的连接
|
||||
log.info("开始创建redis模板对象...");
|
||||
RedisTemplate redisTemplate = new RedisTemplate();
|
||||
//设置redis的连接工厂对象
|
||||
redisTemplate.setConnectionFactory(redisConnectionFactory);
|
||||
//设置redis key的序列化器 这意味着所有通过这个RedisTemplate实例存储的键都将被转换为字符串格式存储在Redis中
|
||||
//设置redis key的序列化器
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
return redisTemplate;
|
||||
}
|
||||
|
@ -31,11 +31,15 @@ public class UserController implements Serializable {
|
||||
@ApiOperation("微信登录")
|
||||
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
|
||||
log.info("微信用户登录:{}",userLoginDTO.getCode());
|
||||
|
||||
//微信登录
|
||||
User user=userService.wxLogin(userLoginDTO);
|
||||
|
||||
//生成JWT令牌
|
||||
Map<String,Object>claims=new HashMap<>();
|
||||
claims.put(JwtClaimsConstant.USER_ID,user.getId());
|
||||
String token=JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),claims);
|
||||
|
||||
UserLoginVO userLoginVO = UserLoginVO.builder().id(user.getId()).openid(user.getOpenid()).token(token).build();
|
||||
return Result.success(userLoginVO);
|
||||
}
|
||||
|
@ -15,5 +15,6 @@ public interface UserMapper {
|
||||
*/
|
||||
void insert(User user);
|
||||
@Select("select * from user where id=#{userId}")
|
||||
|
||||
User getById(Long userId);
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import java.util.Map;
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UserServiceImpl implements UserService {
|
||||
//微信服务接口地址
|
||||
public static final String WX_LOGIN = "https://api.weixin.qq.com/sns/jscode2session";
|
||||
@Autowired
|
||||
private WeChatProperties weChatProperties;
|
||||
@ -35,6 +36,7 @@ public class UserServiceImpl implements UserService {
|
||||
public User wxLogin(UserLoginDTO userLoginDTO) {
|
||||
//调用微信接口服务,获得当前微信用户的openid
|
||||
String openid=getOpenid(userLoginDTO.getCode());
|
||||
|
||||
//判断openid是否为空,如果空则登录失败
|
||||
if(openid==null)
|
||||
throw new LoginFailedException(MessageConstant.LOGIN_FAILED);
|
||||
@ -65,6 +67,7 @@ public class UserServiceImpl implements UserService {
|
||||
map.put("js_code",code);
|
||||
map.put("grant_type","authorization_code");
|
||||
String json=HttpClientUtil.doGet(WX_LOGIN,map);
|
||||
|
||||
JSONObject jsonObject = JSON.parseObject(json);
|
||||
String openid = jsonObject.getString("openid");
|
||||
return openid;
|
||||
|
@ -9,7 +9,7 @@ sky:
|
||||
redis:
|
||||
host: localhost
|
||||
port: 6379
|
||||
auth: 123456
|
||||
password: 123456
|
||||
database: 0
|
||||
|
||||
alioss:
|
||||
|
@ -15,7 +15,7 @@ spring:
|
||||
redis:
|
||||
host: ${sky.redis.host}
|
||||
port: ${sky.redis.port}
|
||||
auth: ${sky.redis.auth}
|
||||
password: ${sky.redis.password}
|
||||
database: ${sky.redis.database}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user