Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the AccessControlContext in Java?

I'm fairly new to Java security stuff and I'm having a hard time figuring out how to google the right answer to this.

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

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

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

Update

Just to clarify, I know how to mock interfaces. AccessController is a Java security feature. I'm having trouble finding out how to set my own context so that when a method calls AccessController.getContext() it returns a AccessControlContext of my choosing.

like image 916
Jason Thompson Avatar asked Oct 07 '22 05:10

Jason Thompson


1 Answers

Okay, I have this figured out. Basically what I was trying to do was this:

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

In order to create an AccessControlContext that contains a Subject, you have to call the method that attempts to obtain the subject like so:

Subject subject = new Subject();  //Set Principles here
Subject.doAs(subject, new PrivilegedAction<Void>()
{
    public Void run()
    {
        Foo.methodThatCaresAboutSubject();
    }
});
like image 185
Jason Thompson Avatar answered Oct 10 '22 02:10

Jason Thompson