Example usage for javax.security.auth Subject Subject

List of usage examples for javax.security.auth Subject Subject

Introduction

In this page you can find the example usage for javax.security.auth Subject Subject.

Prototype

public Subject() 

Source Link

Document

Create an instance of a Subject with an empty Set of Principals and empty Sets of public and private credentials.

Usage

From source file:org.apache.storm.hdfs.security.AutoHDFS.java

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    Map conf = new HashMap();
    conf.put(Config.TOPOLOGY_SUBMITTER_PRINCIPAL, args[0]); //with realm e.g. storm@WITZEND.COM
    conf.put(STORM_USER_NAME_KEY, args[1]); //with realm e.g. hdfs@WITZEND.COM
    conf.put(STORM_KEYTAB_FILE_KEY, args[2]);// /etc/security/keytabs/storm.keytab

    Configuration configuration = new Configuration();
    AutoHDFS autoHDFS = new AutoHDFS();
    autoHDFS.prepare(conf);/*from   w  ww.j a  va2s .  co  m*/

    Map<String, String> creds = new HashMap<String, String>();
    autoHDFS.populateCredentials(creds, conf);
    LOG.info("Got HDFS credentials", autoHDFS.getCredentials(creds));

    Subject s = new Subject();
    autoHDFS.populateSubject(s, creds);
    LOG.info("Got a Subject " + s);

    autoHDFS.renew(creds, conf);
    LOG.info("renewed credentials", autoHDFS.getCredentials(creds));
}

From source file:org.apache.hadoop.security.SecureClientLogin.java

public synchronized static Subject loginUserFromKeytab(String user, String path) throws IOException {
    try {//from   w w  w.  j  ava2 s.com
        Subject subject = new Subject();
        SecureClientLoginConfiguration loginConf = new SecureClientLoginConfiguration(true, user, path);
        LoginContext login = new LoginContext("hadoop-keytab-kerberos", subject, null, loginConf);
        subject.getPrincipals().add(new User(user, AuthenticationMethod.KERBEROS, login));
        login.login();
        return login.getSubject();
    } catch (LoginException le) {
        throw new IOException("Login failure for " + user + " from keytab " + path, le);
    }
}

From source file:org.apache.storm.hive.security.AutoHive.java

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    Map<String, Object> conf = new HashMap();
    conf.put(Config.TOPOLOGY_SUBMITTER_PRINCIPAL, args[0]); //with realm e.g. storm@WITZEND.COM
    conf.put(HIVE_PRINCIPAL_KEY, args[1]); // hive principal storm-hive@WITZEN.COM
    conf.put(HIVE_KEYTAB_FILE_KEY, args[2]); // storm hive keytab /etc/security/keytabs/storm-hive.keytab
    conf.put(HiveConf.ConfVars.METASTOREURIS.varname, args[3]); // hive.metastore.uris : "thrift://pm-eng1-cluster1.field.hortonworks.com:9083"

    AutoHive autoHive = new AutoHive();
    autoHive.prepare(conf);//  w ww . j  ava2s.com

    Map<String, String> creds = new HashMap<String, String>();
    autoHive.populateCredentials(creds, conf);
    LOG.info("Got Hive credentials" + autoHive.getCredentials(creds));

    Subject subject = new Subject();
    autoHive.populateSubject(subject, creds);
    LOG.info("Got a Subject " + subject);

    //autoHive.renew(creds, conf);
    //LOG.info("Renewed credentials" + autoHive.getCredentials(creds));
}

From source file:uk.org.openeyes.oink.security.TestSimpleIdentityService.java

@Test
public void testGetOrganizationWorksForValidSubject() {
    SimpleIdentityService identityService = new SimpleIdentityService();
    Subject s = new Subject();
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken("bob@moorfields",
            "password");
    s.getPrincipals().add(token);//  www. jav  a2s . co  m

    String organisation = identityService.getOrganisation(s);

    String expectedOrganisation = "moorfields";
    assertEquals(expectedOrganisation, organisation);
}

From source file:org.fornax.cartridges.sculptor.smartclient.server.util.ServiceContextFactoryForSpring.java

@Override
protected Subject activeSubject() {
    if (SecurityContextHolder.getContext() != null
            && SecurityContextHolder.getContext().getAuthentication() != null) {
        return new Subject();
    } else {/* w  w w  .j a  va2  s .c o  m*/
        return null;
    }
}

From source file:org.camelcookbook.security.springsecurity.SecuritySubjectLoader.java

@Override
public void process(Exchange exchange) throws Exception {
    Message in = exchange.getIn();//ww  w .ja v a  2 s  .c om
    String username = in.getHeader("username", String.class);
    String password = in.getHeader("password", String.class);

    Authentication authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
    Subject subject = new Subject();
    subject.getPrincipals().add(authenticationToken);
    in.setHeader(Exchange.AUTHENTICATION, subject);
}

From source file:org.apache.hadoop.security.SecureClientLogin.java

public synchronized static Subject loginUserFromKeytab(String user, String path, String nameRules)
        throws IOException {
    try {//  ww  w  .  j a va2s. c o m
        Subject subject = new Subject();
        SecureClientLoginConfiguration loginConf = new SecureClientLoginConfiguration(true, user, path);
        LoginContext login = new LoginContext("hadoop-keytab-kerberos", subject, null, loginConf);
        KerberosName.setRules(nameRules);
        subject.getPrincipals().add(new User(user, AuthenticationMethod.KERBEROS, login));
        login.login();
        return login.getSubject();
    } catch (LoginException le) {
        throw new IOException("Login failure for " + user + " from keytab " + path, le);
    }
}

From source file:org.apache.storm.blobstore.BlobStoreUtils.java

public static Subject getNimbusSubject() {
    Subject subject = new Subject();
    subject.getPrincipals().add(new NimbusPrincipal());
    return subject;
}

From source file:com.muk.services.processor.BearerTokenAuthPrincipalProcessor.java

@Override
public void process(Exchange exchange) throws Exception {

    String bearerToken = RestConstants.Rest.anonymousToken;

    if (exchange.getIn().getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
        bearerToken = StringUtils//from   w ww.  j  a va  2  s  . co  m
                .substringAfter(exchange.getIn().getHeader(HttpHeaders.AUTHORIZATION, String.class), "Bearer ");
    }

    // create an Authentication object
    // build a new bearer token type
    final BearerAuthenticationToken authToken = new BearerAuthenticationToken(bearerToken);

    // wrap it in a Subject
    final Subject subject = new Subject();
    subject.getPrincipals().add(authToken);

    // place the Subject in the In message
    exchange.getIn().setHeader(Exchange.AUTHENTICATION, subject);
}

From source file:uk.org.openeyes.oink.security.TestSimpleIdentityService.java

@Test
public void testGetOrganizationReturnsNullForSubjectWithMissingPrincipal() {
    SimpleIdentityService identityService = new SimpleIdentityService();
    Subject s = new Subject();

    String organisation = identityService.getOrganisation(s);

    assertNull(organisation);/*w  ww .  j  a v  a 2 s .c o  m*/
}