Commit a557ec87 authored by tangtuo's avatar tangtuo

Merge branch 'dev_1.0.0' into test_v1.0.0

parents ec934149 3a0a4f59
......@@ -12,7 +12,8 @@ public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authenticationInterceptor())
.addPathPatterns("/**"); // 拦截所有请求,通过判断是否有 @LoginRequired 注解 决定是否需要登录
.excludePathPatterns("/user/login", "/admin/login", "/verificationCode/send/sms")
.addPathPatterns("/**");
}
@Bean
public AuthenticationInterceptor authenticationInterceptor() {
......
......@@ -56,6 +56,9 @@ public class NftVo {
@ApiModelProperty("授权阅读 0-不需要授权 1-需要授权")
private Integer isGrant;
@ApiModelProperty("是否收藏")
private Boolean collection;
@ApiModelProperty("关于nft")
private String aboutNft;
......@@ -73,5 +76,6 @@ public class NftVo {
this.publishTime = DateUtil.format(nft.getPublishTime(), "yyyy/MM/dd HH:mm:ss");
this.isArchives = nft.getIsArchives();
this.isGrant = nft.getIsGrant();
this.collection = false;
}
}
......@@ -56,10 +56,13 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
if (!token.startsWith(TokenConstant.TOKEN_PREFIX)) {
throw GlobalException.newException(ResultCode.UNAUTHORIZED, "非法token");
}
String realToken = token.substring(TokenConstant.TOKEN_PREFIX.length());
if (StringUtils.isBlank(realToken) || "null".equals(realToken)) {
throw GlobalException.newException(ResultCode.UNAUTHORIZED, "请先登录");
}
if (JwtUtil.isTokenExpired(token)) {
throw GlobalException.newException(ResultCode.UNAUTHORIZED, "登录已过期");
}
String realToken = token.substring(TokenConstant.TOKEN_PREFIX.length());
String appId = JwtUtil.getAppIdFromToken(token);
Integer userId = JwtUtil.getUserIdFromToken(token);
AbstractUser user;
......@@ -109,7 +112,8 @@ public class AuthenticationInterceptor implements HandlerInterceptor {
}
public static void main(String[] args) {
String token = "Bearer token121323433 ";
String token = "Bearer null ";
System.out.println(token.substring("Bearer ".length()));
System.out.println(StringUtils.isBlank(token.substring("Bearer ".length())));
}
}
......@@ -140,7 +140,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
case "voice":
return smsProperties.getVoiceLoginCodetype();
default:
throw GlobalException.newException(ResultCode.CODE_ERROR);
throw GlobalException.newException(ResultCode.VALIDATE_FAILED, "短信类型【" + type + "】的传参有误,取值范围是{sms,email,voice}");
}
}
......
......@@ -55,6 +55,7 @@ public class JwtUtil {
.getBody();
} catch (Exception e) {
log.info("JWT格式验证失败:{}", token);
throw GlobalException.newException(ResultCode.UNAUTHORIZED, e.getMessage());
}
return claims;
}
......@@ -80,7 +81,7 @@ public class JwtUtil {
Claims claims = getClaimsFromToken(token);
return claims.get(CLAIM_KEY_CREATED, Date.class);
} catch (Exception e) {
throw GlobalException.newException(ResultCode.TOKEN_VALID_ERROR, e.getMessage());
throw GlobalException.newException(ResultCode.UNAUTHORIZED, e.getMessage());
}
}
......@@ -92,7 +93,7 @@ public class JwtUtil {
Claims claims = getClaimsFromToken(token);
return claims.get(CLAIM_KEY_USERID, Integer.class);
} catch (Exception e) {
throw GlobalException.newException(ResultCode.TOKEN_VALID_ERROR, e.getMessage());
throw GlobalException.newException(ResultCode.UNAUTHORIZED, e.getMessage());
}
}
......@@ -104,7 +105,7 @@ public class JwtUtil {
Claims claims = getClaimsFromToken(token);
return claims.get(CLAIM_KEY_APP_ID, String.class);
} catch (Exception e) {
throw GlobalException.newException(ResultCode.TOKEN_VALID_ERROR, e.getMessage());
throw GlobalException.newException(ResultCode.UNAUTHORIZED, e.getMessage());
}
}
......
......@@ -142,6 +142,7 @@ public class OssUtil {
if (in != null) {
in.close();
}
ossClient.shutdown();
} catch (Exception e) {
log.error(e.getMessage(), e);
throw GlobalException.newException(ResultCode.FILE_DOWNLOAD_ERROR, e.getMessage());
......
......@@ -2,6 +2,7 @@ package com.fzm.portal.controller;
import cn.hutool.crypto.SecureUtil;
import com.fzm.common.annotation.Authentication;
import com.fzm.common.constant.RedisConstant;
import com.fzm.common.constant.SystemConstant;
import com.fzm.common.entity.Nft;
import com.fzm.common.entity.NftDto;
......@@ -18,6 +19,7 @@ import com.fzm.common.service.UserService;
import com.fzm.common.utils.JwtUtil;
import com.fzm.common.utils.OssUtil;
import com.fzm.common.utils.QRCodeUtil;
import com.fzm.common.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
......@@ -58,6 +60,9 @@ public class NftController {
private CategoryService categoryService;
@Resource
private RedisUtil redisUtil;
@Resource
private HttpServletRequest request;
/**
......@@ -101,7 +106,7 @@ public class NftController {
@GetMapping("get/{id}")
@ApiOperation(value = "获取nft详情")
public ResponseModel<NftVo> getById(@PathVariable Integer id) {
public ResponseModel<NftVo> getById(@PathVariable Integer id, @RequestHeader(required = false) String Authorization) {
Nft nft = nftService.getById(id);
if (nft == null) {
throw GlobalException.newException(ResultCode.DATA_ERROR, "没找到此nft的详情");
......@@ -112,6 +117,11 @@ public class NftController {
}
NftVo nftVo = new NftVo(nft, user);
nftVo.setCategory(categoryService.getById(nft.getCategoryId()).getCategoryName());
if (StringUtils.isNotBlank(Authorization)) {
Integer userId = JwtUtil.getUserIdFromToken(Authorization);
Boolean collection = redisUtil.sIsMember(RedisConstant.COLLECTION_USER_PREFIX + userId, id.toString());
nftVo.setCollection(collection);
}
return ResponseModel.success(nftVo);
}
......
......@@ -81,6 +81,15 @@ public class UserController {
return ResponseModel.success(user != null && StringUtils.isNotBlank(user.getPassword()));
}
@Authentication
@PostMapping(value = "/logout")
@ApiOperation(value = "判断用户是否已注册过")
public ResponseModel<Boolean> logout(@RequestHeader String Authorization) {
Integer userId = JwtUtil.getUserIdFromToken(Authorization);
redisUtil.delete(RedisConstant.PORTAL_USER_TOKEN_PREFIX + userId);
return ResponseModel.success();
}
//@ApiOperation(value = "设置密码")
public ResponseModel<Boolean> setPassword() {
return ResponseModel.success();
......
package com.fzm.portal;
import cn.fzm.chain.simplesdk.client.ParaChainClient;
import com.fzm.common.constant.RedisConstant;
import com.fzm.common.utils.JsonUtil;
import com.fzm.common.utils.RedisUtil;
import org.junit.jupiter.api.Test;
......@@ -29,8 +30,7 @@ class LyPortalApplicationTests {
@Test
void contextLoads() {
System.out.println(redisUtil.setMembers("user:collect:2"));
System.out.println(redisUtil.sSize("user:collect:2"));
System.out.println(redisUtil.sIsMember(RedisConstant.COLLECTION_USER_PREFIX + 9, "2"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment