Getting object executing java.security.PrivilegedAction at runtime

I am trying to get the object that is currently performing the JAAS Preferred Action to be able to retrieve its principles. Basically, I need to check at runtime that the privileged action is actually performed by the director who has the right to do so.

Or, to put it another way: is it possible to get the current LoginContext at run time as a kind of system property (and not by creating a new one)? This will easily allow the extraction of the object.

+3
source share
2 answers

Are you sure you need LoginContext?

( ),

 Subject activeSubject = Subject.getSubject(AccessController.getContext());
+3

, . , -, , . LoginContext . , , - , / / (, ).

public class LoginContextHolder {

    private static ThreadLocal<LoginContext> ctx = new ThreadLocal<LoginContext>();

    public static void set(LoginContext lc) {
        ctx.set(lc);
    }

    public static LoginContext get() {
        return ctx.get();
    }

}


public class LoginContextFilter implements Filter {

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {

       LoginContext ctx = null;
       HttpSession sess = (HttpSession)((HttpRequest)request).getSession(false);
       if (sess != null) {
          ctx = (LoginContext)sess.getAttribute("ctx");
       }

       try {
         LoginContextHolder.set(ctx);
          chain.doFilter(request, response);
       } finally {
         LoginContextHolder.set(null);
       }

    }


}
0

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


All Articles