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

[ISSUE-#4286] When auth open, use resource parser cache to parse resource #4287

Merged
merged 1 commit into from
Nov 23, 2020
Merged
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
16 changes: 15 additions & 1 deletion core/src/main/java/com/alibaba/nacos/core/auth/AuthFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Unified filter to handle authentication and authorization.
Expand All @@ -57,6 +59,8 @@ public class AuthFilter implements Filter {
@Autowired
private ControllerMethodsCache methodsCache;

private Map<Class<? extends ResourceParser>, ResourceParser> parserInstance = new ConcurrentHashMap<>();

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
Expand Down Expand Up @@ -96,7 +100,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
String resource = secured.resource();

if (StringUtils.isBlank(resource)) {
ResourceParser parser = secured.parser().newInstance();
ResourceParser parser = getResourceParser(secured.parser());
resource = parser.parseName(req);
}

Expand Down Expand Up @@ -124,4 +128,14 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
return;
}
}

private ResourceParser getResourceParser(Class<? extends ResourceParser> parseClass)
throws IllegalAccessException, InstantiationException {
ResourceParser parser = parserInstance.get(parseClass);
if (parser == null) {
parser = parseClass.newInstance();
parserInstance.put(parseClass, parser);
chuntaojun marked this conversation as resolved.
Show resolved Hide resolved
}
return parser;
}
}