java代码防止sql注入_动态Java代码注入
java代碼防止sql注入
在本文中,我們將研究如何將Java代碼動態加載到正在運行的jvm中。 該代碼可能是全新的,或者我們可能想更改程序中某些現有代碼的功能。
(在開始之前,您可能想知道為什么到底有人會這樣做。顯而易見的示例是規則引擎之類的東西。規則引擎希望為用戶提供添加或更改規則的能力,而不必重新啟動規則。您可以通過將DSL腳本作為規則注入規則引擎來執行此操作,這種方法的真正問題是必須對DSL腳本進行解釋,使其運行起來非常緩慢。然后可以像程序中的其他任何代碼一樣編譯和運行該程序,效率將提高幾個數量級。
在《紀事報》中,我們在新的微秒微服務/算法容器的核心中使用了這個想法。
我們將要使用的庫是Chronicle開源庫Java-Runtime-Compiler 。
從下面的代碼中您將看到,該庫的使用極其簡單-實際上,它實際上只需要幾行。 創建一個CachedCompiler,然后調用loadFromJava。 (有關實際最簡單的用例,請參見此處的文檔。)
下面列出的程序執行以下操作:
這是完整的代碼清單:
package test;import net.openhft.compiler.CachedCompiler;/*** Loads the addingStrategy and then after 3s replaces it with the * subtractingStrategy.*/ public class DynamicJavaClassLoading {private final static String className = "test.MyClass";private final static String addingStrategy = "package test;\n" +"import test.DynamicJavaClassLoading.Strategy;\n" +"public class MyClass implements Strategy {\n" +" public int compute(int a, int b) {\n" +" return a+b;\n" +" }\n" +"}\n";private final static String subtractingStrategy = "package test;\n" +"import test.DynamicJavaClassLoading.Strategy;\n" +"public class MyClass implements Strategy {\n" +" public int compute(int a, int b) {\n" +" return a-b;\n" +" }\n" +"}\n";public static void main(String[] args) throws Exception {StrategyProxy strategy = new StrategyProxy();//Thread calling the strategy once a secondThread t = new Thread(() -> {while (true) {System.out.println(strategy.compute(10,20));try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();{ClassLoader cl = new ClassLoader() {};CachedCompiler cc = new CachedCompiler(null, null);Class aClass = cc.loadFromJava(cl, className, addingStrategy);Strategy runner = (Strategy) aClass.newInstance();strategy.setStratgey(runner);}Thread.sleep(3000);{ClassLoader cl = new ClassLoader() {};CachedCompiler cc = new CachedCompiler(null, null);Class aClass = cc.loadFromJava(cl, className, subtractingStrategy);Strategy runner = (Strategy) aClass.newInstance();strategy.setStratgey(runner);}}public interface Strategy{int compute(int a, int b);}public static class StrategyProxy implements Strategy{private volatile Strategy underlying;public void setStratgey(Strategy underlying){this.underlying = underlying;}public int compute(int a, int b){Strategy underlying = this.underlying;return underlying == null ? Integer.MIN_VALUE : underlying.compute(a, b);}} }這是輸出(藍色注釋):
The strategy has not been loaded yet. underlying in the StrategyProxy is null so Integer.MIN_VALUE is returned -2 1 4 7 4 8 3 6 4 8 The adding strategy has been loaded 10+20=30 30 30 30 After 3s the subtracting strategy is loaded. It replaces the adding strategy. 10-20=-10 -10 -10 -10 -10-10注意,在代碼中,每次加載策略時,我們都會創建一個新的ClassLoader和一個CachedCompiler。 這樣做的原因是,ClassLoader一次只能加載一個特定類的一個實例。
如果僅使用此庫來加載新代碼,則可以這樣做,而無需創建ClassLoader(即使用默認的ClassLoader)和CachedCompiler。
Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);翻譯自: https://www.javacodegeeks.com/2015/10/dynamic-java-code-injection.html
java代碼防止sql注入
總結
以上是生活随笔為你收集整理的java代码防止sql注入_动态Java代码注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电脑键盘示意图清晰及用法
- 下一篇: 2w元最强游戏装机配置?