Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
ne-shield-auth-client
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit bd77431b
authored
Mar 16, 2022
by
levphon
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
共享数据拦截器
1 parent
54b4049e
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
49 additions
and
25 deletions
pom.xml
src/main/java/cn/com/glsx/auth/interceptor/DataOnlySharedInterceptor.java
src/main/java/cn/com/glsx/auth/interceptor/DataPermissionInterceptor.java
src/main/java/cn/com/glsx/auth/interceptor/DataPermitLinkInterceptor.java
src/main/java/cn/com/glsx/auth/listener/DataCommandLineRunner.java
pom.xml
View file @
bd77431
...
@@ -9,7 +9,8 @@
...
@@ -9,7 +9,8 @@
</parent>
</parent>
<modelVersion>
4.0.0
</modelVersion>
<modelVersion>
4.0.0
</modelVersion>
<artifactId>
glsx-ne-shield-auth-client
</artifactId>
<artifactId>
ne-shield-auth-client
</artifactId>
<version>
1.1.0-SNAPSHOT
</version>
<dependencies>
<dependencies>
<!-- feign -->
<!-- feign -->
...
...
src/main/java/cn/com/glsx/auth/interceptor/DataOnlySharedInterceptor.java
0 → 100644
View file @
bd77431
This diff is collapsed.
Click to expand it.
src/main/java/cn/com/glsx/auth/interceptor/DataPermissionInterceptor.java
View file @
bd77431
...
@@ -3,6 +3,7 @@ package cn.com.glsx.auth.interceptor;
...
@@ -3,6 +3,7 @@ package cn.com.glsx.auth.interceptor;
import
cn.com.glsx.admin.common.constant.UserConstants
;
import
cn.com.glsx.admin.common.constant.UserConstants
;
import
cn.com.glsx.auth.utils.OriginSqlHolder
;
import
cn.com.glsx.auth.utils.OriginSqlHolder
;
import
cn.com.glsx.auth.utils.ShieldContextHolder
;
import
cn.com.glsx.auth.utils.ShieldContextHolder
;
import
com.glsx.plat.common.annotation.DataOnlyShared
;
import
com.glsx.plat.common.annotation.DataPerm
;
import
com.glsx.plat.common.annotation.DataPerm
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.glsx.plat.common.utils.StringUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -75,17 +76,28 @@ public class DataPermissionInterceptor implements Interceptor {
...
@@ -75,17 +76,28 @@ public class DataPermissionInterceptor implements Interceptor {
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
}
//没自定义注解直接执行
//超级管理员/全部数据权限 不控制数据权限
DataPerm
dataAuth
=
getDataPerm
(
mappedStatement
);
if
(
ShieldContextHolder
.
isSuperAdmin
()
||
UserConstants
.
RolePermitCastType
.
all
.
getCode
().
equals
(
ShieldContextHolder
.
getRolePermissionType
()))
{
if
(
dataAuth
==
null
)
{
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
}
//超级管理员/全部数据权限 不控制数据权限
Method
method
=
getMapperExecutingMethod
(
mappedStatement
);
if
(
ShieldContextHolder
.
isSuperAdmin
()
||
UserConstants
.
RolePermitCastType
.
all
.
getCode
().
equals
(
ShieldContextHolder
.
getRolePermissionType
()))
{
if
(
method
==
null
)
{
throw
new
Exception
(
"Mybatis拦截器执行异常"
);
}
//如果方法有共享数据注解,直接跳过
if
(
method
.
isAnnotationPresent
(
DataOnlyShared
.
class
))
{
return
invocation
.
proceed
();
}
//没自定义注解直接执行
if
(!
method
.
isAnnotationPresent
(
DataPerm
.
class
))
{
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
}
DataPerm
dataAuth
=
method
.
getAnnotation
(
DataPerm
.
class
);
//拼装sql
//拼装sql
String
originSql
=
boundSql
.
getSql
();
//获取到当前需要被执行的SQL
String
originSql
=
boundSql
.
getSql
();
//获取到当前需要被执行的SQL
...
@@ -126,25 +138,23 @@ public class DataPermissionInterceptor implements Interceptor {
...
@@ -126,25 +138,23 @@ public class DataPermissionInterceptor implements Interceptor {
}
}
/**
/**
* 获取
方法上的数据权限注解
* 获取
当前mapper执行方法
*
*
* @param mappedStatement
* @param mappedStatement
* @return
* @return
* @throws ClassNotFoundException
* @throws ClassNotFoundException
*/
*/
private
DataPerm
getDataPerm
(
MappedStatement
mappedStatement
)
throws
ClassNotFoundException
{
private
Method
getMapperExecutingMethod
(
MappedStatement
mappedStatement
)
throws
ClassNotFoundException
{
DataPerm
dataPerm
=
null
;
String
id
=
mappedStatement
.
getId
();
String
id
=
mappedStatement
.
getId
();
String
className
=
id
.
substring
(
0
,
id
.
lastIndexOf
(
"."
));
String
className
=
id
.
substring
(
0
,
id
.
lastIndexOf
(
"."
));
String
methodName
=
id
.
substring
(
id
.
lastIndexOf
(
"."
)
+
1
);
String
methodName
=
id
.
substring
(
id
.
lastIndexOf
(
"."
)
+
1
);
final
Method
[]
method
=
Class
.
forName
(
className
).
getDeclaredMethods
();
final
Method
[]
method
=
Class
.
forName
(
className
).
getDeclaredMethods
();
for
(
Method
me
:
method
)
{
for
(
Method
me
:
method
)
{
if
(
me
.
getName
().
equals
(
methodName
)
&&
me
.
isAnnotationPresent
(
DataPerm
.
class
))
{
if
(
me
.
getName
().
equals
(
methodName
))
{
dataPerm
=
me
.
getAnnotation
(
DataPerm
.
class
);
return
me
;
break
;
}
}
}
}
return
dataPerm
;
return
null
;
}
}
/**
/**
...
...
src/main/java/cn/com/glsx/auth/interceptor/DataPermitLinkInterceptor.java
View file @
bd77431
...
@@ -4,6 +4,7 @@ import cn.com.glsx.admin.common.constant.UserConstants;
...
@@ -4,6 +4,7 @@ import cn.com.glsx.admin.common.constant.UserConstants;
import
cn.com.glsx.auth.utils.OriginSqlHolder
;
import
cn.com.glsx.auth.utils.OriginSqlHolder
;
import
cn.com.glsx.auth.utils.ShieldContextHolder
;
import
cn.com.glsx.auth.utils.ShieldContextHolder
;
import
com.alibaba.nacos.common.utils.CollectionUtils
;
import
com.alibaba.nacos.common.utils.CollectionUtils
;
import
com.glsx.plat.common.annotation.DataOnlyShared
;
import
com.glsx.plat.common.annotation.DataPermShare
;
import
com.glsx.plat.common.annotation.DataPermShare
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.glsx.plat.common.utils.StringUtils
;
import
com.google.common.collect.Lists
;
import
com.google.common.collect.Lists
;
...
@@ -82,17 +83,29 @@ public class DataPermitLinkInterceptor implements Interceptor {
...
@@ -82,17 +83,29 @@ public class DataPermitLinkInterceptor implements Interceptor {
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
}
//没自定义注解直接按通过
//超级管理员/全部数据权限 不控制数据权限
DataPermShare
dataAuth
=
getAnnotation
(
mappedStatement
);
if
(
ShieldContextHolder
.
isSuperAdmin
()
||
UserConstants
.
RolePermitCastType
.
all
.
getCode
().
equals
(
ShieldContextHolder
.
getRolePermissionType
()))
{
if
(
dataAuth
==
null
)
{
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
}
//超级管理员/全部数据权限 不控制数据权限
Method
method
=
getMapperExecutingMethod
(
mappedStatement
);
if
(
ShieldContextHolder
.
isSuperAdmin
()
||
UserConstants
.
RolePermitCastType
.
all
.
getCode
().
equals
(
ShieldContextHolder
.
getRolePermissionType
()))
{
if
(
method
==
null
)
{
throw
new
Exception
(
"Mybatis拦截器执行异常"
);
}
//如果方法有共享数据注解,直接跳过
if
(
method
.
isAnnotationPresent
(
DataOnlyShared
.
class
))
{
return
invocation
.
proceed
();
}
//没自定义注解直接执行
if
(!
method
.
isAnnotationPresent
(
DataPermShare
.
class
))
{
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
}
//没自定义注解直接按通过
DataPermShare
dataAuth
=
method
.
getAnnotation
(
DataPermShare
.
class
);
//拼装sql
//拼装sql
String
originSql
=
OriginSqlHolder
.
getSql
();
//获取到当前需要被执行的原始SQL
String
originSql
=
OriginSqlHolder
.
getSql
();
//获取到当前需要被执行的原始SQL
String
permitSql
=
boundSql
.
getSql
();
//获取到当前需要被执行的权限SQL
String
permitSql
=
boundSql
.
getSql
();
//获取到当前需要被执行的权限SQL
...
@@ -322,25 +335,23 @@ public class DataPermitLinkInterceptor implements Interceptor {
...
@@ -322,25 +335,23 @@ public class DataPermitLinkInterceptor implements Interceptor {
}
}
/**
/**
* 获取
方法上的数据权限注解
* 获取
当前mapper执行方法
*
*
* @param mappedStatement
* @param mappedStatement
* @return
* @return
* @throws ClassNotFoundException
* @throws ClassNotFoundException
*/
*/
private
DataPermShare
getAnnotation
(
MappedStatement
mappedStatement
)
throws
ClassNotFoundException
{
private
Method
getMapperExecutingMethod
(
MappedStatement
mappedStatement
)
throws
ClassNotFoundException
{
DataPermShare
dataAuth
=
null
;
String
id
=
mappedStatement
.
getId
();
String
id
=
mappedStatement
.
getId
();
String
className
=
id
.
substring
(
0
,
id
.
lastIndexOf
(
"."
));
String
className
=
id
.
substring
(
0
,
id
.
lastIndexOf
(
"."
));
String
methodName
=
id
.
substring
(
id
.
lastIndexOf
(
"."
)
+
1
);
String
methodName
=
id
.
substring
(
id
.
lastIndexOf
(
"."
)
+
1
);
final
Method
[]
method
=
Class
.
forName
(
className
).
getDeclaredMethods
();
final
Method
[]
method
=
Class
.
forName
(
className
).
getDeclaredMethods
();
for
(
Method
me
:
method
)
{
for
(
Method
me
:
method
)
{
if
(
me
.
getName
().
equals
(
methodName
)
&&
me
.
isAnnotationPresent
(
DataPermShare
.
class
))
{
if
(
me
.
getName
().
equals
(
methodName
))
{
dataAuth
=
me
.
getAnnotation
(
DataPermShare
.
class
);
return
me
;
break
;
}
}
}
}
return
dataAuth
;
return
null
;
}
}
private
MappedStatement
newMappedStatement
(
MappedStatement
ms
,
SqlSource
newSqlSource
)
{
private
MappedStatement
newMappedStatement
(
MappedStatement
ms
,
SqlSource
newSqlSource
)
{
...
...
src/main/java/cn/com/glsx/auth/listener/DataCommandLineRunner.java
View file @
bd77431
package
cn
.
com
.
glsx
.
auth
.
listener
;
package
cn
.
com
.
glsx
.
auth
.
listener
;
import
cn.com.glsx.auth.interceptor.DataOnlySharedInterceptor
;
import
cn.com.glsx.auth.interceptor.DataPermissionInterceptor
;
import
cn.com.glsx.auth.interceptor.DataPermissionInterceptor
;
import
cn.com.glsx.auth.interceptor.DataPermitLinkInterceptor
;
import
cn.com.glsx.auth.interceptor.DataPermitLinkInterceptor
;
import
lombok.extern.slf4j.Slf4j
;
import
lombok.extern.slf4j.Slf4j
;
...
@@ -31,6 +32,7 @@ public class DataCommandLineRunner implements CommandLineRunner {
...
@@ -31,6 +32,7 @@ public class DataCommandLineRunner implements CommandLineRunner {
sqlSessionFactory
.
getConfiguration
().
addInterceptor
(
new
DataPermitLinkInterceptor
());
sqlSessionFactory
.
getConfiguration
().
addInterceptor
(
new
DataPermitLinkInterceptor
());
sqlSessionFactory
.
getConfiguration
().
addInterceptor
(
new
DataPermissionInterceptor
());
sqlSessionFactory
.
getConfiguration
().
addInterceptor
(
new
DataPermissionInterceptor
());
sqlSessionFactory
.
getConfiguration
().
addInterceptor
(
new
DataOnlySharedInterceptor
());
List
<
Interceptor
>
interceptorList
=
sqlSessionFactory
.
getConfiguration
().
getInterceptors
();
List
<
Interceptor
>
interceptorList
=
sqlSessionFactory
.
getConfiguration
().
getInterceptors
();
interceptorList
.
forEach
(
interceptor
->
{
interceptorList
.
forEach
(
interceptor
->
{
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment