|
|
@@ -0,0 +1,186 @@
|
|
|
+package sm.cloud.admin.server.core.config;
|
|
|
+
|
|
|
+import cn.dev33.satoken.exception.NotPermissionException;
|
|
|
+import cn.dev33.satoken.interceptor.SaInterceptor;
|
|
|
+import cn.dev33.satoken.router.SaRouter;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.http.converter.HttpMessageConverter;
|
|
|
+import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
|
|
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
|
|
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
|
|
+import sm.cloud.admin.server.core.exception.CustomBusinessException;
|
|
|
+import sm.cloud.admin.server.core.interfaces.ICacheService;
|
|
|
+import sm.cloud.admin.server.core.util.CacheInfoUtil;
|
|
|
+import sm.cloud.admin.server.dao.domain.AdminUser;
|
|
|
+import sm.cloud.admin.server.dao.domain.ClientAccount;
|
|
|
+import sm.cloud.admin.server.dao.domain.ClientUnion;
|
|
|
+import sm.cloud.admin.server.dao.repository.AdminUserRepository;
|
|
|
+import sm.cloud.admin.server.dao.repository.ClientAccountRepository;
|
|
|
+import sm.cloud.admin.server.dao.repository.ClientUnionRepository;
|
|
|
+import sm.cloud.admin.server.manager.RoleManager;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 参考:https://blog.csdn.net/z17806289513/article/details/123659224
|
|
|
+ * 放行 knife4j
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Configuration
|
|
|
+public class WebMvcConfig extends WebMvcConfigurationSupport {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private HttpServletRequest request;
|
|
|
+ @Resource
|
|
|
+ private ICacheService cacheService;
|
|
|
+ @Resource
|
|
|
+ private ClientUnionRepository unionRepository;
|
|
|
+ @Resource
|
|
|
+ private AdminUserRepository userRepository;
|
|
|
+ @Resource
|
|
|
+ private CacheInfoUtil cacheInfoUtil;
|
|
|
+ @Resource
|
|
|
+ private ClientAccountRepository accountRepository;
|
|
|
+
|
|
|
+ //获取配置文件中图片的路径
|
|
|
+ @Value("${file.imagesPath}")
|
|
|
+ private String mImagesPath;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
|
|
+ registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
|
|
+ registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
|
|
+
|
|
|
+ //配置图片代理
|
|
|
+ registry.addResourceHandler("/images/**").addResourceLocations(mImagesPath);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
|
|
|
+ MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter =
|
|
|
+ new MappingJackson2HttpMessageConverter();
|
|
|
+ ObjectMapper objectMapper = new ObjectMapper();
|
|
|
+ SimpleModule simpleModule = new SimpleModule();
|
|
|
+ simpleModule.addSerializer(Long.class, ToStringSerializer.instance)
|
|
|
+ .addSerializer(Long.TYPE, ToStringSerializer.instance);
|
|
|
+ objectMapper.registerModule(simpleModule);
|
|
|
+ //JsonInclude.Include.NON_NULL 属性为NULL 不序列化
|
|
|
+ objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
+ //JsonInclude.Include.NON_EMPTY 属性为空字符串 不序列化
|
|
|
+// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
|
|
|
+
|
|
|
+ mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);
|
|
|
+ converters.add(mappingJackson2HttpMessageConverter);
|
|
|
+ super.configureMessageConverters(converters);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void addInterceptors(InterceptorRegistry registry) {
|
|
|
+ registry.addInterceptor(new SaInterceptor(handler -> {
|
|
|
+ //权限
|
|
|
+ String xPermission = request.getHeader("X-Permission");
|
|
|
+ if (StringUtils.isBlank(xPermission)) {
|
|
|
+ throw new CustomBusinessException("X-Permission必填");
|
|
|
+ }
|
|
|
+ //角色
|
|
|
+ String xRole = request.getHeader("X-Role");
|
|
|
+ SaRouter.match("/**").notMatch(a -> {
|
|
|
+ //放行不需要验证
|
|
|
+ String whiteList = cacheService.getCache(RoleManager.W_PREFIX);
|
|
|
+ if (StringUtils.isBlank(whiteList)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ List<String> list = Arrays.asList(whiteList.split(","));
|
|
|
+
|
|
|
+ return list.contains(xPermission.trim());
|
|
|
+ }).notMatch(b -> {
|
|
|
+ //验证是否登录
|
|
|
+ StpUtil.checkLogin();
|
|
|
+ //放行 只需要登录的
|
|
|
+ String whiteList = cacheService.getCache(RoleManager.LOGIN_PREFIX);
|
|
|
+ if (StringUtils.isBlank(whiteList)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ List<String> list = Arrays.asList(whiteList.split(","));
|
|
|
+
|
|
|
+ return list.contains(xPermission.trim());
|
|
|
+ }).check(c -> {
|
|
|
+ //缓存中
|
|
|
+ Object roleIdCache = StpUtil.getSession().get("roleId_cache");
|
|
|
+ //角色没有变化
|
|
|
+
|
|
|
+ if ((StringUtils.isNotBlank(xRole) && Objects.nonNull(roleIdCache) && !xRole.equalsIgnoreCase(roleIdCache.toString()))
|
|
|
+ || (StringUtils.isBlank(xRole) && Objects.nonNull(roleIdCache))
|
|
|
+ || (StringUtils.isNotBlank(xRole) && Objects.isNull(roleIdCache))) {
|
|
|
+ String roleIdStr = null;
|
|
|
+ //对比角色
|
|
|
+ String loginDevice = StpUtil.getLoginDevice();
|
|
|
+ if (StringUtils.isNotBlank(loginDevice) && loginDevice.equalsIgnoreCase("manager")) {
|
|
|
+ AdminUser adminUser = userRepository.getById(StpUtil.getLoginIdAsLong());
|
|
|
+ roleIdStr = adminUser.getRoleId();
|
|
|
+ } else {
|
|
|
+ String unionId = cacheInfoUtil.getUnionId() + "";
|
|
|
+ if (StringUtils.isBlank(unionId)) {
|
|
|
+ ClientAccount clientAccount = accountRepository.getById(StpUtil.getLoginIdAsLong());
|
|
|
+ if (Objects.isNull(clientAccount.getUnionId())) {
|
|
|
+ throw new CustomBusinessException("用户信息有误!");
|
|
|
+ }
|
|
|
+ unionId = clientAccount.getUnionId() + "";
|
|
|
+ }
|
|
|
+ ClientUnion clientUnion = unionRepository.getById(unionId);
|
|
|
+ roleIdStr = clientUnion.getRoleId();
|
|
|
+ }
|
|
|
+
|
|
|
+ //与数据库同步
|
|
|
+ StpUtil.getSession().set("roleId", roleIdStr);
|
|
|
+ //与使用端同步
|
|
|
+ if (StringUtils.isNotBlank(xRole)) {
|
|
|
+ StpUtil.getSession().set("roleId_cache", xRole);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //标识
|
|
|
+ boolean hasPermit = false;
|
|
|
+ if (Objects.nonNull(StpUtil.getSession().get("roleId"))) {
|
|
|
+ String[] roleIdList = StpUtil.getSession().getString("roleId").split(",");
|
|
|
+ for (int i = 0; i < roleIdList.length; i++) {
|
|
|
+ String permitList = cacheService.getCache(RoleManager.PREFIX + roleIdList[i]);
|
|
|
+ if (StringUtils.isNotBlank(permitList) && permitList.contains(xPermission)) {
|
|
|
+ hasPermit = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //没有权限
|
|
|
+ if (!hasPermit) {
|
|
|
+ throw new NotPermissionException("权限不足");
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ })).addPathPatterns("/**").excludePathPatterns(
|
|
|
+ "/inner/**",
|
|
|
+ "/images/**",
|
|
|
+ "/**/doc.*",
|
|
|
+ "/**/swagger-ui.*",
|
|
|
+ "/**/swagger-resources",
|
|
|
+ "/**/webjars/**",
|
|
|
+ "/**/v2/api-docs/**");
|
|
|
+ super.addInterceptors(registry);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|