Demo không sử dụng qua LDAP:
package com.example.log4jshell;
import javax.naming.Context;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.tvd12.ezyhttp.core.boot.EzyHttpApplicationBootstrap;
public final class Log4jShellStartup {
private static final Logger LOGGER = LogManager.getLogger(Log4jShellStartup.class);
public static void main(String[] args) throws Exception {
LOGGER.info("start Log4jShell");
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.example.log4jshell.AppContextFactory");
System.setProperty(Context.PROVIDER_URL, "remote+http://localhost:8080");
EzyHttpApplicationBootstrap.start(Log4jShellStartup.class);
}
}
package com.example.log4jshell;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
public class AppContextFactory implements InitialContextFactory {
private final AppInitialContext context = new AppInitialContext();
public AppContextFactory() throws NamingException {}
@Override
public Context getInitialContext(Hashtable environment) throws NamingException {
return context;
}
}
package com.example.log4jshell;
import java.io.File;
import java.util.Hashtable;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import com.tvd12.ezyhttp.client.HttpClient;
public class AppInitialContext extends InitialLdapContext {
private final HttpClient httpClient;
public AppInitialContext() throws NamingException {
httpClient = HttpClient.builder().build();
}
@Override
protected void init(Hashtable environment) throws NamingException { }
@Override
public Object lookup(String name) throws NamingException {
try {
httpClient.download(name, new File("target/classes"));
Thread.sleep(300);
Class clazz = Class.forName("Log4jShell");
return clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.example.log4jshell.controller;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.tvd12.ezyhttp.server.core.annotation.Controller;
import com.tvd12.ezyhttp.server.core.annotation.DoGet;
import com.tvd12.ezyhttp.server.core.annotation.RequestParam;
@Controller
public class HomeController {
private final Logger logger = LogManager.getLogger(getClass());
@DoGet("/hello")
public String hello(@RequestParam String name) {
logger.info("Hello: {}", name);
return "Hello " + name;
}
}