Java Security manager per thread
Asked Answered
S

1

1

I want to run a specific thread-class in a restricted sandbox, while the rest of the application can run unrestricted.

Is it possible to attach a security manager for a specific thread-class only?

--

EDIT: Using Peter's hint, I created the following variable, inside my custom security manager:

private static ThreadLocal<Boolean> isChatbot = new InheritableThreadLocal<Boolean>() {
  @Override protected synchronized Boolean initialValue() {
    boolean value = (Thread.currentThread() instanceof ChatBot);
    return value;
  }
  @Override protected synchronized Boolean childValue(Boolean parentValue) {
    boolean value = (Thread.currentThread() instanceof ChatBot || parentValue);
    return value;
  }
};

ChatBot is my specific class of threads which I want to run restricted. So in initialValue I give the value 'true' to all ChatBot threads, and in childValue I also give the value 'true' to all childs spawned by a ChatBot thread.

Strangely, this doesn't work. I put a breakpoint inside childValue, and I saw that the execution never gets there, so child threads get a value of 'false'.

What am I doing wrong?

Swede answered 19/7, 2011 at 8:53 Comment(0)
P
1

You can create a security manager which only checks one thread (or every thread with an InheritableThreadLocal) The benefit of using an InheritableThreadLocal is that any spawned thread will also be checked.

Penney answered 19/7, 2011 at 9:26 Comment(1)
I've wirrten a blog post containing details of what a per-thread SecurityManager would look like: alphaloop.blogspot.com/2014/08/…Legislate

© 2022 - 2024 — McMap. All rights reserved.