Skambinti iš saugomų ar privačios klasės java: įsitikinkite, kad neįmanoma?
Reklama
Teisingas atsakymas: "priklauso nuo buvimo ar nebuvimo saugumo vadovas."
Iš tiesų, per svarstymo faktiškai galima pereiti prie jėgos apribojimus Java kalbą be didelių sunkumų, ammenochè yra ne tik saugumo valdytojas, tik gebėtų tikrai valdyti ir galiausiai nutraukti šių tipų jėga.
Bet mes dabar pavyzdžiui, kad verta daug diskusijų. Atsižvelgiant į šias klases:
package net.nothing2hide.test; public class ClassWithMethodsToHack { private void sayHello(String name){ System.out.println("Hello " + name + "! You're calling a private method"); } } mes norime parodyti, kaip lengvai būti taikomas ne privataus metodas sayHello kaip aš tikimasi jį naudoti labai atspindys:
package net.nothing2hide.test; public class PrivateMethodInvocationTest { /** * This sample class demonstrates how to * invoke a protected method on another class * * @param args */ public static void main(String[] args) { String myName = "Davide"; Method m; try { //Let'access the private method //by reflection. You could do the same //with a protected method m = ClassWithMethodsToHack.class.getDeclaredMethod( "sayHello", new Class[]{String.class}); if (m != null) { //Let's force the java language access //checks on the reflected method. Note //that if a security manager is defined //this workaround could not be permitted //and a SecurityException could be raised m.setAccessible(true); //And finally let's invoke the protected method m.invoke(new ClassWithMethodsToHack(), new Object[]{myName}); } } catch (Exception e) { e.printStackTrace(); } } } Komentarų nėra »
RSS komentarai šiam įrašui. TrackBack URI








