Skip to content

Commit 3edf79c

Browse files
committed
refactor(extension/crud): 重构 BaseController 内权限校验
1.移除 SaToken 依赖 2.移除 checkPermission 方法 3.新增 CrudApi 注解
1 parent 6b3bc83 commit 3edf79c

File tree

11 files changed

+344
-90
lines changed

11 files changed

+344
-90
lines changed

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@
3030
<artifactId>continew-starter-web</artifactId>
3131
</dependency>
3232

33-
<!-- 认证模块 - SaToken -->
34-
<dependency>
35-
<groupId>top.continew</groupId>
36-
<artifactId>continew-starter-auth-satoken</artifactId>
37-
<exclusions>
38-
<exclusion>
39-
<groupId>org.springframework.boot</groupId>
40-
<artifactId>spring-boot-starter-web</artifactId>
41-
</exclusion>
42-
</exclusions>
43-
</dependency>
44-
4533
<!-- 数据访问模块 - 核心模块 -->
4634
<dependency>
4735
<groupId>top.continew</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
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+
17+
package top.continew.starter.extension.crud.annotation;
18+
19+
import top.continew.starter.extension.crud.enums.Api;
20+
21+
import java.lang.annotation.*;
22+
23+
/**
24+
* CRUD(增删改查)API
25+
*
26+
* @author Charles7c
27+
* @since 2.7.5
28+
*/
29+
@Target(ElementType.METHOD)
30+
@Retention(RetentionPolicy.RUNTIME)
31+
@Documented
32+
public @interface CrudApi {
33+
34+
/**
35+
* API 类型
36+
*/
37+
Api value() default Api.LIST;
38+
}

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/annotation/EnableCrudRestController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package top.continew.starter.extension.crud.annotation;
1818

1919
import org.springframework.context.annotation.Import;
20+
import top.continew.starter.extension.crud.autoconfigure.CrudRequestMappingAutoConfiguration;
2021
import top.continew.starter.extension.crud.autoconfigure.CrudRestControllerAutoConfiguration;
2122

2223
import java.lang.annotation.*;
@@ -30,5 +31,5 @@
3031
@Target({ElementType.TYPE})
3132
@Retention(RetentionPolicy.RUNTIME)
3233
@Documented
33-
@Import({CrudRestControllerAutoConfiguration.class})
34+
@Import({CrudRequestMappingAutoConfiguration.class, CrudRestControllerAutoConfiguration.class})
3435
public @interface EnableCrudRestController {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
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+
17+
package top.continew.starter.extension.crud.aop;
18+
19+
import org.aopalliance.aop.Advice;
20+
import org.springframework.aop.Pointcut;
21+
import org.springframework.aop.support.AbstractPointcutAdvisor;
22+
import org.springframework.aop.support.ComposablePointcut;
23+
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
24+
import org.springframework.beans.BeansException;
25+
import org.springframework.beans.factory.BeanFactory;
26+
import org.springframework.beans.factory.BeanFactoryAware;
27+
28+
import java.lang.annotation.Annotation;
29+
30+
/**
31+
* CRUD API 注解通知
32+
*
33+
* @author Charles7c
34+
* @since 2.7.5
35+
*/
36+
public class CrudApiAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {
37+
38+
private final Advice advice;
39+
private final Pointcut pointcut;
40+
41+
public CrudApiAnnotationAdvisor(CrudApiAnnotationInterceptor advice, Class<? extends Annotation> annotation) {
42+
this.advice = advice;
43+
this.pointcut = new ComposablePointcut(AnnotationMatchingPointcut.forMethodAnnotation(annotation));
44+
}
45+
46+
@Override
47+
public Pointcut getPointcut() {
48+
return this.pointcut;
49+
}
50+
51+
@Override
52+
public Advice getAdvice() {
53+
return this.advice;
54+
}
55+
56+
@Override
57+
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
58+
if (this.advice instanceof BeanFactoryAware beanFactoryAware) {
59+
beanFactoryAware.setBeanFactory(beanFactory);
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
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+
17+
package top.continew.starter.extension.crud.aop;
18+
19+
import org.aopalliance.intercept.MethodInterceptor;
20+
import org.aopalliance.intercept.MethodInvocation;
21+
import org.springframework.aop.support.AopUtils;
22+
import org.springframework.core.BridgeMethodResolver;
23+
import org.springframework.core.annotation.AnnotatedElementUtils;
24+
import org.springframework.util.ClassUtils;
25+
import top.continew.starter.extension.crud.annotation.CrudApi;
26+
import top.continew.starter.extension.crud.handler.CrudApiHandler;
27+
28+
import java.lang.reflect.Method;
29+
import java.util.Objects;
30+
31+
/**
32+
* CRUD API 注解拦截器
33+
*
34+
* @author Charles7c
35+
* @since 2.7.5
36+
*/
37+
public class CrudApiAnnotationInterceptor implements MethodInterceptor {
38+
39+
private final CrudApiHandler handler;
40+
41+
public CrudApiAnnotationInterceptor(CrudApiHandler handler) {
42+
this.handler = handler;
43+
}
44+
45+
@Override
46+
public Object invoke(MethodInvocation invocation) throws Throwable {
47+
// 获取目标类
48+
Class<?> targetClass = AopUtils.getTargetClass(Objects.requireNonNull(invocation.getThis()));
49+
// 获取目标方法
50+
Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
51+
Method targetMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
52+
// 获取 @CrudApi 注解
53+
CrudApi crudApi = AnnotatedElementUtils.findMergedAnnotation(targetMethod, CrudApi.class);
54+
handler.preHandle(crudApi, targetMethod, targetClass);
55+
return invocation.proceed();
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
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+
17+
package top.continew.starter.extension.crud.autoconfigure;
18+
19+
import org.springframework.beans.factory.annotation.Qualifier;
20+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.context.annotation.Primary;
24+
import org.springframework.format.support.FormattingConversionService;
25+
import org.springframework.web.accept.ContentNegotiationManager;
26+
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
27+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
28+
import org.springframework.web.servlet.resource.ResourceUrlProvider;
29+
30+
/**
31+
* CRUD Request Mapping 自动配置
32+
*
33+
* @author Charles7c
34+
* @since 1.0.0
35+
*/
36+
@Configuration
37+
@EnableConfigurationProperties(CrudProperties.class)
38+
public class CrudRequestMappingAutoConfiguration extends DelegatingWebMvcConfiguration {
39+
40+
/**
41+
* CRUD 请求映射器处理器映射器(覆盖默认 RequestMappingHandlerMapping)
42+
*/
43+
@Override
44+
public RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
45+
return new CrudRequestMappingHandlerMapping();
46+
}
47+
48+
@Bean
49+
@Primary
50+
@Override
51+
public RequestMappingHandlerMapping requestMappingHandlerMapping(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
52+
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
53+
@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
54+
return super.requestMappingHandlerMapping(contentNegotiationManager, conversionService, resourceUrlProvider);
55+
}
56+
}

continew-starter-extension/continew-starter-extension-crud/continew-starter-extension-crud-core/src/main/java/top/continew/starter/extension/crud/autoconfigure/CrudRestControllerAutoConfiguration.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,53 @@
1919
import jakarta.annotation.PostConstruct;
2020
import org.slf4j.Logger;
2121
import org.slf4j.LoggerFactory;
22-
import org.springframework.beans.factory.annotation.Qualifier;
22+
import org.springframework.boot.autoconfigure.AutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2324
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2425
import org.springframework.context.annotation.Bean;
25-
import org.springframework.context.annotation.Configuration;
26-
import org.springframework.context.annotation.Primary;
27-
import org.springframework.format.support.FormattingConversionService;
28-
import org.springframework.web.accept.ContentNegotiationManager;
29-
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
30-
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
31-
import org.springframework.web.servlet.resource.ResourceUrlProvider;
26+
import top.continew.starter.extension.crud.annotation.CrudApi;
27+
import top.continew.starter.extension.crud.aop.CrudApiAnnotationAdvisor;
28+
import top.continew.starter.extension.crud.aop.CrudApiAnnotationInterceptor;
29+
import top.continew.starter.extension.crud.handler.CrudApiHandler;
30+
import top.continew.starter.extension.crud.handler.DefaultCrudApiHandler;
3231

3332
/**
3433
* CRUD REST Controller 自动配置
3534
*
3635
* @author Charles7c
37-
* @since 1.0.0
36+
* @since 2.7.5
3837
*/
39-
@Configuration
38+
@AutoConfiguration
4039
@EnableConfigurationProperties(CrudProperties.class)
41-
public class CrudRestControllerAutoConfiguration extends DelegatingWebMvcConfiguration {
40+
public class CrudRestControllerAutoConfiguration {
4241

4342
private static final Logger log = LoggerFactory.getLogger(CrudRestControllerAutoConfiguration.class);
4443

4544
/**
46-
* CRUD 请求映射器处理器映射器(覆盖默认 RequestMappingHandlerMapping)
45+
* CRUD API 注解通知
4746
*/
48-
@Override
49-
public RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
50-
return new CrudRequestMappingHandlerMapping();
47+
@Bean
48+
@ConditionalOnMissingBean
49+
public CrudApiAnnotationAdvisor crudApiAnnotationAdvisor(CrudApiAnnotationInterceptor crudApiAnnotationInterceptor) {
50+
return new CrudApiAnnotationAdvisor(crudApiAnnotationInterceptor, CrudApi.class);
51+
}
52+
53+
/**
54+
* CRUD API 注解拦截器
55+
*/
56+
@Bean
57+
@ConditionalOnMissingBean
58+
public CrudApiAnnotationInterceptor crudApiAnnotationInterceptor(CrudApiHandler crudApiHandler) {
59+
return new CrudApiAnnotationInterceptor(crudApiHandler);
5160
}
5261

62+
/**
63+
* CRUD API 处理器(默认)
64+
*/
5365
@Bean
54-
@Primary
55-
@Override
56-
public RequestMappingHandlerMapping requestMappingHandlerMapping(@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
57-
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
58-
@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {
59-
return super.requestMappingHandlerMapping(contentNegotiationManager, conversionService, resourceUrlProvider);
66+
@ConditionalOnMissingBean
67+
public CrudApiHandler crudApiHandler() {
68+
return new DefaultCrudApiHandler();
6069
}
6170

6271
@PostConstruct
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
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+
17+
package top.continew.starter.extension.crud.handler;
18+
19+
import top.continew.starter.extension.crud.annotation.CrudApi;
20+
21+
import java.lang.reflect.Method;
22+
23+
/**
24+
* CRUD API 处理器
25+
*
26+
* @author Charles7c
27+
* @since 2.7.5
28+
*/
29+
public interface CrudApiHandler {
30+
31+
/**
32+
* 前置处理
33+
*
34+
* @param crudApi CRUD API 注解
35+
* @param targetMethod 目标方法
36+
* @param targetClass 目标类
37+
* @throws Exception 处理异常
38+
*/
39+
void preHandle(CrudApi crudApi, Method targetMethod, Class<?> targetClass) throws Exception;
40+
}

0 commit comments

Comments
 (0)