日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java代码防止sql注入_动态Java代码注入

發(fā)布時間:2023/12/3 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java代码防止sql注入_动态Java代码注入 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

java代碼防止sql注入

在本文中,我們將研究如何將Java代碼動態(tài)加載到正在運行的jvm中。 該代碼可能是全新的,或者我們可能想更改程序中某些現(xiàn)有代碼的功能。

(在開始之前,您可能想知道為什么到底有人會這樣做。顯而易見的示例是規(guī)則引擎之類的東西。規(guī)則引擎希望為用戶提供添加或更改規(guī)則的能力,而不必重新啟動規(guī)則。您可以通過將DSL腳本作為規(guī)則注入規(guī)則引擎來執(zhí)行此操作,這種方法的真正問題是必須對DSL腳本進(jìn)行解釋,使其運行起來非常緩慢。然后可以像程序中的其他任何代碼一樣編譯和運行該程序,效率將提高幾個數(shù)量級。

在《紀(jì)事報》中,我們在新的微秒微服務(wù)/算法容器的核心中使用了這個想法。

我們將要使用的庫是Chronicle開源庫Java-Runtime-Compiler 。

從下面的代碼中您將看到,該庫的使用極其簡單-實際上,它實際上只需要幾行。 創(chuàng)建一個CachedCompiler,然后調(diào)用loadFromJava。 (有關(guān)實際最簡單的用例,請參見此處的文檔。)

下面列出的程序執(zhí)行以下操作:

  • 創(chuàng)建一個線程,該線程每秒調(diào)用一次Strategy上的compute。 該戰(zhàn)略的投入為10和20。
  • 加載將兩個數(shù)字加在一起的策略
  • 等待3秒
  • 加載從另一個數(shù)中減去一個數(shù)的策略
  • 這是完整的代碼清單:

    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);}} }

    這是輸出(藍(lán)色注釋):

    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

    注意,在代碼中,每次加載策略時,我們都會創(chuàng)建一個新的ClassLoader和一個CachedCompiler。 這樣做的原因是,ClassLoader一次只能加載一個特定類的一個實例。

    如果僅使用此庫來加載新代碼,則可以這樣做,而無需創(chuàng)建ClassLoader(即使用默認(rèn)的ClassLoader)和CachedCompiler。

    Class aClass = CompilerUtils.CACHED_COMPILER.loadFromJava(className, javaCode);

    翻譯自: https://www.javacodegeeks.com/2015/10/dynamic-java-code-injection.html

    java代碼防止sql注入

    總結(jié)

    以上是生活随笔為你收集整理的java代码防止sql注入_动态Java代码注入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。