permitted security auth Subject - Java Security

Java examples for Security:Permission

Description

permitted security auth Subject

Demo Code


//package com.java2s;
import java.security.Permission;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import javax.security.auth.Subject;

public class Main {
    static SecurityManager sm;

    static public boolean permitted(Subject subj, final Permission p)
            throws PrivilegedActionException {
        if (p == null) {
            return false;
        }/*from  w  ww.  ja  va2 s. c om*/
        if (subj == null) {
            subj = new Subject();
        }

        if (sm == null)
            sm = new SecurityManager();

        Subject.doAsPrivileged(subj,
                new PrivilegedExceptionAction<Object>() {
                    public Object run() {
                        sm.checkPermission(p);
                        return null;
                    }
                }, null);

        return true;
    }
}

Related Tutorials