zouxuan 5 lat temu
rodzic
commit
77ca7f49d0

+ 1 - 1
edu-datasource/src/main/java/com/keao/edu/datasource/DataSourceConfig.java

@@ -47,7 +47,7 @@ public class DataSourceConfig {
 		// 配置动态数据源,如果没有将 dynamicDataSource 作为数据源则不能实现切换
 		sqlSessionFactoryBean.setDataSource(dynamicDataSource());
 		sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:config/mybatis/*.xml"));
-		sqlSessionFactoryBean.setTypeAliasesPackage("com.keao.edu.*.dal.entity");
+		sqlSessionFactoryBean.setTypeAliasesPackage("com.keao.edu.*.dal.entity,com.keao.edu.*.entity");
 
 		return sqlSessionFactoryBean;
 	}

+ 1 - 3
edu-datasource/src/main/resources/config/mybatis/DatabaseSourceMapper.xml

@@ -86,12 +86,10 @@
 	<!-- 分页查询 -->
 	<select id="queryPage" resultMap="DatabaseSource" parameterType="map">
 		SELECT * FROM database_source ORDER BY id_
-		<include refid="global.limit" />
 	</select>
 
 	<!-- 查询当前表的总记录数 -->
 	<select id="queryCount" resultType="int">
-		SELECT COUNT(*) FROM
-		database_source
+		SELECT COUNT(*) FROM database_source
 	</select>
 </mapper>

+ 4 - 0
edu-user/edu-user-server/pom.xml

@@ -80,5 +80,9 @@
             <version>1.0</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>com.keao.edu</groupId>
+            <artifactId>edu-datasource</artifactId>
+        </dependency>
     </dependencies>
 </project>

+ 1 - 1
edu-user/edu-user-server/src/main/java/com/keao/edu/user/UserServerApplication.java

@@ -16,7 +16,7 @@ import org.springframework.web.client.RestTemplate;
 @SpringBootApplication
 @EnableDiscoveryClient
 @EnableFeignClients({"com.keao.edu"})
-@MapperScan("com.keao.edu.user.dao")
+@MapperScan({"com.keao.edu.user.dao"})
 @ComponentScan(basePackages="com.keao.edu")
 @Configuration
 @EnableSwagger2Doc

+ 49 - 0
edu-user/edu-user-server/src/main/java/com/keao/edu/user/config/PermissionCheckService.java

@@ -0,0 +1,49 @@
+package com.keao.edu.user.config;
+
+import com.keao.edu.auth.api.client.SysUserFeignService;
+import com.keao.edu.auth.api.entity.SysUser;
+import com.keao.edu.auth.api.util.SecurityUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.GrantedAuthority;
+import org.springframework.stereotype.Component;
+
+import java.util.Collection;
+
+@Component("pcs")
+public class PermissionCheckService {
+
+	@Autowired
+	private SysUserFeignService sysUserFeignService;
+
+	public boolean hasPermissions(String... permissions) {
+		Authentication authentication = SecurityUtils.getAuthentication();
+		if (authentication == null) {
+			return false;
+		}
+
+		SysUser user = sysUserFeignService.queryUserInfo();
+		if (user.getIsSuperAdmin()) {
+			return true;
+		}
+
+		Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
+
+		for (String perm : permissions) {
+			for (GrantedAuthority authority : authorities) {
+				if (StringUtils.equalsIgnoreCase(perm, authority.getAuthority())) {
+					return true;
+				}
+			}
+		}
+
+		return false;
+	}
+
+	public boolean hasRoles(String... roles) {
+
+		return hasPermissions(roles);
+	}
+
+}

+ 13 - 0
edu-user/edu-user-server/src/main/java/com/keao/edu/user/config/WebMvcConfig.java

@@ -3,11 +3,13 @@ package com.keao.edu.user.config;
 import java.util.ArrayList;
 import java.util.List;
 
+import com.keao.edu.datasource.interceptor.DataSourceInterceptor;
 import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.format.FormatterRegistry;
 import org.springframework.http.MediaType;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
 import com.keao.edu.common.config.EnumConverterFactory;
@@ -23,6 +25,17 @@ public class WebMvcConfig implements WebMvcConfigurer {
 	public void addFormatters(FormatterRegistry registry) {
 		registry.addConverterFactory(new EnumConverterFactory());
 	}
+
+	@Override
+	public void addInterceptors(InterceptorRegistry registry) {
+
+		registry.addInterceptor(getDataSrouceInterceptor()).addPathPatterns("/**");
+	}
+
+	@Bean
+	public DataSourceInterceptor getDataSrouceInterceptor() {
+		return new DataSourceInterceptor();
+	}
 	
 	@Bean
     public HttpMessageConverters fastJsonHttpMessageConverters(){

+ 21 - 0
edu-user/edu-user-server/src/main/resources/config.mybatis/Global.mapper.xml

@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
+"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="global">
+
+	<sql id="limit">
+		<if test="offset != null">
+			 limit #{offset},#{rows}
+		</if>
+	</sql>
+
+	<sql id="orderby">
+		<if test="sort != null and sort != ''">
+			 ORDER BY ${sort}
+			 <if test="order != null and order != ''">
+			 	${order}
+			 </if>
+		</if>
+	</sql>	
+ </mapper>