init
parent
ba0c83f2ea
commit
b905e695b7
@ -0,0 +1,99 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import com.supervision.constant.UserTokenConstant;
|
||||
import com.supervision.exception.BusinessException;
|
||||
import com.supervision.util.SpringBeanUtil;
|
||||
import com.supervision.util.TokenUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Slf4j
|
||||
public class JwtInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final RedisTemplate<String, String> redisTemplate;
|
||||
|
||||
public JwtInterceptor(RedisTemplate<String, String> redisTemplate) {
|
||||
this.redisTemplate = redisTemplate;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
//请求消息头获取用户ID
|
||||
String token = request.getHeader("token");
|
||||
if (StrUtil.isBlank(token)) {
|
||||
// 如果是swagger来的接口,说明这里是测试的,会伪造一个用户
|
||||
if (StrUtil.isNotBlank(request.getHeader("Knife4j-Gateway-Code"))) {
|
||||
cacheAuth(JWTUtil.parseToken(devActiveUser()));
|
||||
return true;
|
||||
} else {
|
||||
throw new BusinessException("当前用户未登录", HttpStatus.UNAUTHORIZED.value());
|
||||
}
|
||||
}
|
||||
JWT jwt = JWTUtil.parseToken(token);
|
||||
// 校验token是否过期,如果过期了,需要提示过期重新登录
|
||||
checkTokenExpire(jwt);
|
||||
cacheAuth(jwt);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
||||
@Nullable Exception ex) throws Exception {
|
||||
// 请求结束,将用户信息从thread中移除
|
||||
clearAuth();
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
|
||||
|
||||
private void checkTokenExpire(JWT jwt) {
|
||||
Object expireTime = jwt.getPayload("expireTime");
|
||||
long l = Long.parseLong(String.valueOf(expireTime));
|
||||
// 校验是否比当前时间大
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if (currentTimeMillis > l) {
|
||||
throw new BusinessException("用户登录已过期,请重新登录", HttpStatus.UNAUTHORIZED.value());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void cacheAuth(JWT jwt) {
|
||||
try {
|
||||
JSONObject claimsJson = jwt.getPayload().getClaimsJson();
|
||||
ThreadCache.USER.set(claimsJson.toString());
|
||||
} catch (Exception e) {
|
||||
log.error("用户信息异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String devActiveUser() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", "1");
|
||||
map.put("account", "test");
|
||||
map.put("name", "测试账户");
|
||||
return TokenUtil.creatToken(JSONUtil.toJsonStr(map));
|
||||
}
|
||||
|
||||
private void clearAuth() {
|
||||
ThreadCache.USER.remove();
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MinioConfig {
|
||||
|
||||
/**
|
||||
* 创建基于Java端的MinioClient
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnProperty(prefix = "minio",name = "url")
|
||||
public MinioClient minioClient(MinioProperties minioProperties) {
|
||||
return MinioClient.builder().endpoint(minioProperties.getUrl())
|
||||
.credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "minio")
|
||||
public class MinioProperties {
|
||||
|
||||
private String url;
|
||||
|
||||
private String accessKey;
|
||||
|
||||
private String secretKey;
|
||||
|
||||
private String bucketName;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.supervision.util.UserUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class MybatisMetaHandler implements MetaObjectHandler {
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
log.info("填充创建和开始时间以及新增用户");
|
||||
// 填充
|
||||
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
|
||||
this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
|
||||
Optional.ofNullable(UserUtil.getUser()).ifPresent(user -> this.strictInsertFill(metaObject, "createUserId", String.class, user.getId()));
|
||||
Optional.ofNullable(UserUtil.getUser()).ifPresent(user -> this.strictInsertFill(metaObject, "updateUserId", String.class, user.getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
log.info("填充修改时间以及修改用户");
|
||||
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
|
||||
Optional.ofNullable(UserUtil.getUser()).ifPresent(user -> this.strictInsertFill(metaObject, "updateUserId", String.class, user.getId()));
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.filter.SaServletFilter;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class SaTokenConfigure {
|
||||
|
||||
public String[] ignorePathPatterns() {
|
||||
List<String> paths = new ArrayList<>();
|
||||
paths.add("/swagger-resources/**");
|
||||
paths.add("/webjars/**");
|
||||
paths.add("/v2/**");
|
||||
paths.add("/swagger-ui.html/**");
|
||||
paths.add("/doc.html/**");
|
||||
paths.add("/error");
|
||||
paths.add("/favicon.ico");
|
||||
paths.add("/user/login");
|
||||
return ArrayUtil.toArray(paths,String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册 [Sa-Token全局过滤器]
|
||||
*/
|
||||
@Bean
|
||||
public SaServletFilter getSaServletFilter() {
|
||||
return new SaServletFilter()
|
||||
|
||||
// 指定 拦截路由 与 放行路由
|
||||
.addInclude("/**")
|
||||
// 排除掉部分不需要登录的路由
|
||||
.addExclude(ignorePathPatterns())
|
||||
|
||||
// 认证函数: 每次请求执行
|
||||
.setAuth(obj -> {
|
||||
System.out.println("---------- 进入Sa-Token全局认证 -----------");
|
||||
// 登录认证 -- 拦截所有路由,并排除/user/doLogin 用于开放登录
|
||||
SaRouter.match("/**", "/user/doLogin", StpUtil::checkLogin);
|
||||
|
||||
// 更多拦截处理方式,请参考“路由拦截式鉴权”章节 */
|
||||
})
|
||||
|
||||
// 异常处理函数:每次认证函数发生异常时执行此函数
|
||||
.setError(e -> {
|
||||
System.out.println("---------- 进入Sa-Token异常处理 -----------");
|
||||
return SaResult.error(e.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.supervision.util;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.config.ThreadCache;
|
||||
import com.supervision.exception.BusinessException;
|
||||
import lombok.Data;
|
||||
|
||||
public class UserUtil {
|
||||
|
||||
public static UserDTO getUser() {
|
||||
String userStr = ThreadCache.USER.get();
|
||||
UserDTO userDTO = JSONUtil.toBean(userStr, UserDTO.class);
|
||||
if (ObjectUtil.isEmpty(userDTO)) {
|
||||
throw new BusinessException("未获取到用户信息");
|
||||
}
|
||||
return userDTO;
|
||||
}
|
||||
|
||||
public static String getUserToken() {
|
||||
return ThreadCache.USER.get();
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class UserDTO {
|
||||
private String id;
|
||||
|
||||
private String userName;
|
||||
|
||||
private String realName;
|
||||
|
||||
private String account;
|
||||
|
||||
private String roleId;
|
||||
|
||||
private String deptId;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.supervision.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@RequestMapping("user")
|
||||
public class UserController {
|
||||
|
||||
@GetMapping("login")
|
||||
public void login(){
|
||||
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package com.supervision;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class KnowledgeReportApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.supervision;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
public class TableModifyTest {
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package com.supervision.repository;
|
||||
|
||||
import com.supervision.entity.Item;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ItemRepository extends ElasticsearchRepository<Item, Long> {
|
||||
/**
|
||||
* 根据价格区间查询
|
||||
*/
|
||||
List<Item> findByPriceBetween(Double price11, Double price2);
|
||||
}
|
Loading…
Reference in New Issue