-
Notifications
You must be signed in to change notification settings - Fork 15k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6220 from EightMonth/master
- Loading branch information
Showing
8 changed files
with
179 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...g-boot-base-core/src/main/java/org/jeecg/config/shiro/ignore/IgnoreAuthPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.jeecg.config.shiro.ignore; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.jeecg.config.shiro.IgnoreAuth; | ||
import org.springframework.aop.framework.Advised; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.util.StopWatch; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.*; | ||
|
||
/** | ||
* 在spring boot初始化时,根据@RestController注解获取当前spring容器中的bean | ||
* @author [email protected] | ||
* @date 2024/4/18 11:35 | ||
*/ | ||
@Component | ||
@AllArgsConstructor | ||
public class IgnoreAuthPostProcessor implements ApplicationListener<ContextRefreshedEvent> { | ||
|
||
private ApplicationContext applicationContext; | ||
|
||
@Override | ||
public void onApplicationEvent(ContextRefreshedEvent event) { | ||
List<String> ignoreAuthUrls = new ArrayList<>(); | ||
if (event.getApplicationContext().getParent() == null) { | ||
// 只处理根应用上下文的事件,避免在子上下文中重复处理 | ||
Map<String, Object> restControllers = applicationContext.getBeansWithAnnotation(RestController.class); | ||
for (Object restController : restControllers.values()) { | ||
// 如 online系统的controller并不是spring 默认生成 | ||
if (restController instanceof Advised) { | ||
ignoreAuthUrls.addAll(postProcessRestController(restController)); | ||
} | ||
} | ||
} | ||
|
||
if (!CollectionUtils.isEmpty(ignoreAuthUrls)) { | ||
InMemoryIgnoreAuth.set(ignoreAuthUrls); | ||
} | ||
} | ||
|
||
private List<String> postProcessRestController(Object restController) { | ||
List<String> ignoreAuthUrls = new ArrayList<>(); | ||
Class<?> clazz = ((Advised) restController).getTargetClass(); | ||
RequestMapping base = clazz.getAnnotation(RequestMapping.class); | ||
String[] baseUrl = Objects.nonNull(base) ? base.value() : new String[]{}; | ||
Method[] methods = clazz.getDeclaredMethods(); | ||
|
||
for (Method method : methods) { | ||
if (method.isAnnotationPresent(IgnoreAuth.class) && method.isAnnotationPresent(RequestMapping.class)) { | ||
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class); | ||
String[] uri = requestMapping.value(); | ||
ignoreAuthUrls.addAll(rebuildUrl(baseUrl, uri)); | ||
} else if (method.isAnnotationPresent(IgnoreAuth.class) && method.isAnnotationPresent(GetMapping.class)) { | ||
GetMapping requestMapping = method.getAnnotation(GetMapping.class); | ||
String[] uri = requestMapping.value(); | ||
ignoreAuthUrls.addAll(rebuildUrl(baseUrl, uri)); | ||
} else if (method.isAnnotationPresent(IgnoreAuth.class) && method.isAnnotationPresent(PostMapping.class)) { | ||
PostMapping requestMapping = method.getAnnotation(PostMapping.class); | ||
String[] uri = requestMapping.value(); | ||
ignoreAuthUrls.addAll(rebuildUrl(baseUrl, uri)); | ||
} else if (method.isAnnotationPresent(IgnoreAuth.class) && method.isAnnotationPresent(PutMapping.class)) { | ||
PutMapping requestMapping = method.getAnnotation(PutMapping.class); | ||
String[] uri = requestMapping.value(); | ||
ignoreAuthUrls.addAll(rebuildUrl(baseUrl, uri)); | ||
} else if (method.isAnnotationPresent(IgnoreAuth.class) && method.isAnnotationPresent(DeleteMapping.class)) { | ||
DeleteMapping requestMapping = method.getAnnotation(DeleteMapping.class); | ||
String[] uri = requestMapping.value(); | ||
ignoreAuthUrls.addAll(rebuildUrl(baseUrl, uri)); | ||
} else if (method.isAnnotationPresent(IgnoreAuth.class) && method.isAnnotationPresent(PatchMapping.class)) { | ||
PatchMapping requestMapping = method.getAnnotation(PatchMapping.class); | ||
String[] uri = requestMapping.value(); | ||
ignoreAuthUrls.addAll(rebuildUrl(baseUrl, uri)); | ||
} | ||
} | ||
|
||
return ignoreAuthUrls; | ||
} | ||
|
||
private List<String> rebuildUrl(String[] bases, String[] uris) { | ||
List<String> urls = new ArrayList<>(); | ||
if (bases.length > 0) { | ||
for (String base : bases) { | ||
for (String uri : uris) { | ||
urls.add(prefix(base) + prefix(uri)); | ||
} | ||
} | ||
} else { | ||
Arrays.stream(uris).forEach(uri -> { | ||
urls.add(prefix(uri)); | ||
}); | ||
} | ||
return urls; | ||
} | ||
|
||
private String prefix(String seg) { | ||
return seg.startsWith("/") ? seg : "/"+seg; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ignore/InMemoryIgnoreAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.jeecg.config.shiro.ignore; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* 使用内存存储通过@IgnoreAuth注解的url,配合JwtFilter进行免登录校验 | ||
* PS:无法使用ThreadLocal进行存储,因为ThreadLocal装载时,JwtFilter已经初始化完毕,导致该类获取ThreadLocal为空 | ||
* @author [email protected] | ||
* @date 2024/4/18 15:02 | ||
*/ | ||
public class InMemoryIgnoreAuth { | ||
private static final List<String> IGNORE_AUTH_LIST = new ArrayList<>(); | ||
|
||
public InMemoryIgnoreAuth() {} | ||
|
||
public static void set(List<String> list) { | ||
IGNORE_AUTH_LIST.addAll(list); | ||
} | ||
|
||
public static List<String> get() { | ||
return IGNORE_AUTH_LIST; | ||
} | ||
|
||
public static void clear() { | ||
IGNORE_AUTH_LIST.clear(); | ||
} | ||
|
||
public static boolean contains(String url) { | ||
for (String ignoreAuth : IGNORE_AUTH_LIST) { | ||
if (url.endsWith(ignoreAuth)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters