How to set AccessControlContext in Java?

I'm new to Java security, and it's hard for me to figure out how to correctly answer this Google question.

I have a line of code that reads AccessController.getContext() .

I want to run a test where I mock the result. Unfortunately, I have no idea what I'm doing.

How to set the AccessControlContext returned by AccessController.getContext() ?

Update

Just to clarify, I know how to simulate interfaces. AccessController is a Java security feature. I'm having trouble figuring out how to set up my own context so that when I call the AccessController.getContext() method, it will return the AccessControlContext of its choice.

+4
source share
1 answer

Ok, I figured it out. I basically tried to do this:

 Subject subject = Subject.getSubject(AccessController.getContext()); 

To create an AccessControlContext that contains a Subject , you must call a method that tries to retrieve the item as follows:

 Subject subject = new Subject(); //Set Principles here Subject.doAs(subject, new PrivilegedAction<Void>() { public Void run() { Foo.methodThatCaresAboutSubject(); } }); 
+4
source

Source: https://habr.com/ru/post/1441363/


All Articles