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