Skip to content

Commit 6ed0254

Browse files
committed
♻️ 向上抽象一层安全校验注解实现,方便业务模块和底层安全框架解耦
link gh-205
1 parent 8a1afd1 commit 6ed0254

File tree

68 files changed

+1246
-1398
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+1246
-1398
lines changed

ballcat-dependencies/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@
501501
</dependency>
502502
<dependency>
503503
<groupId>org.ballcat</groupId>
504-
<artifactId>ballcat-security-web</artifactId>
504+
<artifactId>ballcat-spring-security</artifactId>
505505
<version>${revision}</version>
506506
</dependency>
507507
<dependency>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<module>redis/ballcat-spring-boot-starter-redis</module>
7373

7474
<module>security/ballcat-security-core</module>
75-
<module>security/ballcat-security-web</module>
75+
<module>security/ballcat-spring-security</module>
7676
<module>security/ballcat-spring-security-oauth2-authorization-server</module>
7777
<module>security/ballcat-spring-security-oauth2-core</module>
7878
<module>security/ballcat-spring-security-oauth2-resource-server</module>

security/ballcat-security-core/pom.xml

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,28 @@
1111
<artifactId>ballcat-security-core</artifactId>
1212

1313
<dependencies>
14+
<!-- slf4j日志 -->
1415
<dependency>
15-
<groupId>org.ballcat</groupId>
16-
<artifactId>ballcat-common-util</artifactId>
17-
</dependency>
18-
<dependency>
19-
<groupId>jakarta.servlet</groupId>
20-
<artifactId>jakarta.servlet-api</artifactId>
21-
<scope>provided</scope>
16+
<groupId>org.slf4j</groupId>
17+
<artifactId>slf4j-api</artifactId>
2218
</dependency>
2319
<dependency>
24-
<groupId>org.springframework.boot</groupId>
25-
<artifactId>spring-boot</artifactId>
26-
<scope>provided</scope>
20+
<groupId>org.ballcat</groupId>
21+
<artifactId>ballcat-common-util</artifactId>
2722
</dependency>
2823
<dependency>
2924
<groupId>org.springframework</groupId>
3025
<artifactId>spring-context</artifactId>
3126
</dependency>
3227
<dependency>
33-
<groupId>org.springframework.boot</groupId>
34-
<artifactId>spring-boot-autoconfigure</artifactId>
28+
<groupId>jakarta.servlet</groupId>
29+
<artifactId>jakarta.servlet-api</artifactId>
30+
<scope>provided</scope>
3531
</dependency>
3632
<dependency>
3733
<groupId>org.springframework.boot</groupId>
38-
<artifactId>spring-boot-configuration-processor</artifactId>
39-
</dependency>
40-
<dependency>
41-
<groupId>org.springframework.security</groupId>
42-
<artifactId>spring-security-crypto</artifactId>
34+
<artifactId>spring-boot-autoconfigure</artifactId>
35+
<scope>provided</scope>
4336
</dependency>
4437
</dependencies>
4538
</project>

security/ballcat-security-core/src/main/java/org/ballcat/security/SecurityConstant.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

security/ballcat-security-core/src/main/java/org/ballcat/security/SecurityStore.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

security/ballcat-security-core/src/main/java/org/ballcat/security/SecurityToken.java

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ballcat.security.access.expression;
17+
18+
import org.aopalliance.intercept.MethodInvocation;
19+
import org.ballcat.security.authorization.SecurityChecker;
20+
import org.springframework.aop.framework.AopProxyUtils;
21+
import org.springframework.aop.support.AopUtils;
22+
import org.springframework.context.ApplicationContext;
23+
import org.springframework.context.ApplicationContextAware;
24+
import org.springframework.context.expression.BeanFactoryResolver;
25+
import org.springframework.context.expression.MethodBasedEvaluationContext;
26+
import org.springframework.core.DefaultParameterNameDiscoverer;
27+
import org.springframework.core.ParameterNameDiscoverer;
28+
import org.springframework.expression.BeanResolver;
29+
import org.springframework.expression.EvaluationContext;
30+
import org.springframework.expression.ExpressionParser;
31+
import org.springframework.expression.spel.standard.SpelExpressionParser;
32+
import org.springframework.expression.spel.support.StandardEvaluationContext;
33+
34+
import java.lang.reflect.Method;
35+
36+
/**
37+
* 默认的鉴权表达式处理器
38+
*
39+
* @author Hccake
40+
* @see org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler
41+
* @since 2.0.0
42+
*/
43+
public class DefaultSecurityExpressionHandler implements SecurityExpressionHandler, ApplicationContextAware {
44+
45+
private ExpressionParser expressionParser = new SpelExpressionParser();
46+
47+
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
48+
49+
private BeanResolver beanResolver;
50+
51+
private final SecurityChecker securityChecker;
52+
53+
public DefaultSecurityExpressionHandler(SecurityChecker securityChecker) {
54+
this.securityChecker = securityChecker;
55+
}
56+
57+
@Override
58+
public ExpressionParser getExpressionParser() {
59+
return this.expressionParser;
60+
}
61+
62+
public void setExpressionParser(ExpressionParser expressionParser) {
63+
this.expressionParser = expressionParser;
64+
}
65+
66+
@Override
67+
public final EvaluationContext createEvaluationContext(MethodInvocation mi) {
68+
StandardEvaluationContext ctx = createEvaluationContextInternal(mi, getParameterNameDiscoverer());
69+
ctx.setBeanResolver(this.beanResolver);
70+
ctx.setRootObject(this.securityChecker);
71+
return ctx;
72+
}
73+
74+
private StandardEvaluationContext createEvaluationContextInternal(MethodInvocation mi,
75+
ParameterNameDiscoverer parameterNameDiscoverer) {
76+
return new MethodBasedEvaluationContext(mi.getThis(), getSpecificMethod(mi), mi.getArguments(),
77+
parameterNameDiscoverer);
78+
}
79+
80+
private Method getSpecificMethod(MethodInvocation mi) {
81+
return AopUtils.getMostSpecificMethod(mi.getMethod(), AopProxyUtils.ultimateTargetClass(mi.getThis()));
82+
}
83+
84+
@Override
85+
public void setApplicationContext(ApplicationContext applicationContext) {
86+
this.beanResolver = new BeanFactoryResolver(applicationContext);
87+
}
88+
89+
/**
90+
* Sets the {@link ParameterNameDiscoverer} to use. The default is
91+
* {@link DefaultParameterNameDiscoverer}.
92+
* @param parameterNameDiscoverer new parameterNameDiscoverer
93+
*/
94+
public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer) {
95+
this.parameterNameDiscoverer = parameterNameDiscoverer;
96+
}
97+
98+
/**
99+
* @return The current {@link ParameterNameDiscoverer}
100+
*/
101+
protected ParameterNameDiscoverer getParameterNameDiscoverer() {
102+
return this.parameterNameDiscoverer;
103+
}
104+
105+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ballcat.security.access.expression;
17+
18+
import org.aopalliance.intercept.MethodInvocation;
19+
import org.springframework.aop.framework.AopInfrastructureBean;
20+
import org.springframework.expression.EvaluationContext;
21+
import org.springframework.expression.ExpressionParser;
22+
23+
/**
24+
* 安全表达式处理器
25+
*
26+
* @author Hccake
27+
* @since 2.0.0
28+
* @see org.springframework.security.access.expression.SecurityExpressionHandler
29+
*/
30+
public interface SecurityExpressionHandler extends AopInfrastructureBean {
31+
32+
/**
33+
* @return an expression parser for the expressions used by the implementation.
34+
*/
35+
ExpressionParser getExpressionParser();
36+
37+
/**
38+
* Provides an evaluation context in which to evaluate security expressions for the
39+
* invocation type.
40+
*/
41+
EvaluationContext createEvaluationContext(MethodInvocation invocation);
42+
43+
}
Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.ballcat.security.annotation;
217

318
import java.lang.annotation.*;
@@ -6,6 +21,7 @@
621
* 鉴权, 默认为登录即可访问
722
*
823
* @author lingting 2023-03-29 20:38
24+
* @author hccake
925
*/
1026
@Target({ ElementType.METHOD, ElementType.TYPE })
1127
@Retention(RetentionPolicy.RUNTIME)
@@ -14,58 +30,9 @@
1430
public @interface Authorize {
1531

1632
/**
17-
* 是否允许匿名, 为true时, 登录和未登录均允许访问. 优先级最高
18-
*/
19-
boolean anyone() default false;
20-
21-
/**
22-
* 是否仅允许系统用户访问 和 normal 同时为true则该接口无法被访问
23-
*/
24-
boolean onlySystem() default false;
25-
26-
/**
27-
* 仅允许普通用户访问 和 sys 同时为true则该接口无法被访问
28-
*/
29-
boolean onlyNormal() default false;
30-
31-
/**
32-
* 必须拥有所有指定角色才可以访问, 为空时允许所有角色访问.
33-
*/
34-
String[] hasRole() default {};
35-
36-
/**
37-
* 必须拥有任一指定角色才可以访问, 为空时允许所有角色访问.
38-
*/
39-
String[] hasAnyRole() default {};
40-
41-
/**
42-
* 必须拥有所有指定权限才可以访问, 为空时允许所有权限访问.
43-
*/
44-
String[] hasPermissions() default {};
45-
46-
/**
47-
* 必须拥有任一指定权限才可以访问, 为空时允许所有权限访问.
48-
*/
49-
String[] hasAnyPermissions() default {};
50-
51-
/**
52-
* 必须未拥有所有指定角色才可以访问, 为空时允许所有角色访问.
53-
*/
54-
String[] notRole() default {};
55-
56-
/**
57-
* 必须未拥有任一指定角色才可以访问, 为空时允许所有角色访问.
58-
*/
59-
String[] notAnyRole() default {};
60-
61-
/**
62-
* 必须未拥有所有指定权限才可以访问, 为空时允许所有权限访问.
63-
*/
64-
String[] notPermissions() default {};
65-
66-
/**
67-
* 必须未拥有任一指定权限才可以访问, 为空时允许所有权限访问.
33+
* @return the Spring-EL expression to be evaluated before invoking the protected
34+
* method
6835
*/
69-
String[] notAnyPermissions() default {};
36+
String value();
7037

7138
}

security/ballcat-security-core/src/main/java/org/ballcat/security/annotation/EnableAuthorizationServer.java

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)