Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

匿名接口注解——匿名注解解析方式优化 #239

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,122 +1,87 @@
package com.ruoyi.framework.config.properties;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import com.ruoyi.common.annotation.Anonymous;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.ruoyi.common.annotation.Anonymous;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* 设置Anonymous注解允许匿名访问的url
*
*
* @author ruoyi
*/
@Configuration
public class PermitAllUrlProperties implements InitializingBean, ApplicationContextAware
{
public class PermitAllUrlProperties implements InitializingBean, ApplicationContextAware {
private List<String> urls = new ArrayList<>();

private ApplicationContext applicationContext;

@Override
public void afterPropertiesSet() throws Exception
{
public void afterPropertiesSet() throws Exception {
Map<String, Object> controllers = applicationContext.getBeansWithAnnotation(Controller.class);
for (Object bean : controllers.values())
{
if (!(bean instanceof Advised))
{
for (Object bean : controllers.values()) {
if (!(bean instanceof Advised)) {
continue;
}
Class<?> beanClass = ((Advised) bean).getTargetSource().getTarget().getClass();
RequestMapping base = beanClass.getAnnotation(RequestMapping.class);
// 在之前的代码下,如果这里不是直接使用RequestMapping注解,而是使用其他带有RequestMapping注解标注的注解(PostMapping)就不会生效
RequestMapping base = AnnotationUtils.getAnnotation(beanClass, RequestMapping.class);
String[] baseUrl = {};
if (Objects.nonNull(base))
{
if (Objects.nonNull(base)) {
baseUrl = base.value();
}
Method[] methods = beanClass.getDeclaredMethods();
for (Method method : methods)
{
if (method.isAnnotationPresent(Anonymous.class) && method.isAnnotationPresent(RequestMapping.class))
{
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
String[] uri = requestMapping.value();
urls.addAll(rebuildUrl(baseUrl, uri));
}
else if (method.isAnnotationPresent(Anonymous.class) && method.isAnnotationPresent(GetMapping.class))
{
GetMapping requestMapping = method.getAnnotation(GetMapping.class);
String[] uri = requestMapping.value();
urls.addAll(rebuildUrl(baseUrl, uri));
}
else if (method.isAnnotationPresent(Anonymous.class) && method.isAnnotationPresent(PostMapping.class))
{
PostMapping requestMapping = method.getAnnotation(PostMapping.class);
String[] uri = requestMapping.value();
urls.addAll(rebuildUrl(baseUrl, uri));
}
else if (method.isAnnotationPresent(Anonymous.class) && method.isAnnotationPresent(PutMapping.class))
{
PutMapping requestMapping = method.getAnnotation(PutMapping.class);
String[] uri = requestMapping.value();
urls.addAll(rebuildUrl(baseUrl, uri));
}
else if (method.isAnnotationPresent(Anonymous.class) && method.isAnnotationPresent(DeleteMapping.class))
{
DeleteMapping requestMapping = method.getAnnotation(DeleteMapping.class);
String[] uri = requestMapping.value();
urls.addAll(rebuildUrl(baseUrl, uri));
for (Method method : methods) {
// 这里判断次数重复,最差的情况下效率较低,同时采用SpringbootAOP中提供的注解解析工具,提高鲁棒性
if (AnnotatedElementUtils.hasAnnotation(method, Anonymous.class)) {
RequestMapping mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(method, RequestMapping.class);
if (Objects.nonNull(mergedAnnotation)) {
String[] uri = mergedAnnotation.value();
urls.addAll(rebuildUrl(baseUrl, uri));
}
}
}

}
}

private List<String> rebuildUrl(String[] bases, String[] uris)
{
private List<String> rebuildUrl(String[] bases, String[] uris) {
List<String> urls = new ArrayList<>();
for (String base : bases)
{
for (String uri : uris)
{
for (String base : bases) {
for (String uri : uris) {
urls.add(prefix(base) + prefix(uri));
}
}
return urls;
}

private String prefix(String seg)
{
private String prefix(String seg) {
return seg.startsWith("/") ? seg : "/" + seg;
}

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException
{
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.applicationContext = context;
}

public List<String> getUrls()
{
public List<String> getUrls() {
return urls;
}

public void setUrls(List<String> urls)
{
public void setUrls(List<String> urls) {
this.urls = urls;
}
}