Search in sources :

Example 21 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project neo4j by neo4j.

the class ShiroSubjectFactory method createSubject.

@Override
public Subject createSubject(SubjectContext context) {
    SecurityManager securityManager = context.resolveSecurityManager();
    Session session = context.resolveSession();
    boolean sessionCreationEnabled = context.isSessionCreationEnabled();
    PrincipalCollection principals = context.resolvePrincipals();
    boolean authenticated = context.resolveAuthenticated();
    String host = context.resolveHost();
    ShiroAuthenticationInfo authcInfo = (ShiroAuthenticationInfo) context.getAuthenticationInfo();
    return new ShiroSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager, authcInfo.getAuthenticationResult());
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) Session(org.apache.shiro.session.Session)

Example 22 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project perry by ca-cwds.

the class AbstractApiSecurityTest method tearDownShiro.

@AfterClass
public static void tearDownShiro() {
    doClearSubject();
    try {
        SecurityManager securityManager = getSecurityManager();
        LifecycleUtils.destroy(securityManager);
    } catch (UnavailableSecurityManagerException e) {
    // we don't care about this when cleaning up the test environment
    // (for example, maybe the subclass is a unit test and it didn't
    // need a SecurityManager instance because it was using only
    // mock Subject instances)
    }
    setSecurityManager(null);
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) UnavailableSecurityManagerException(org.apache.shiro.UnavailableSecurityManagerException) AfterClass(org.junit.AfterClass)

Example 23 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project tutorials by eugenp.

the class Main method main.

public static void main(String[] args) {
    Realm realm = new MyCustomRealm();
    SecurityManager securityManager = new DefaultSecurityManager(realm);
    SecurityUtils.setSecurityManager(securityManager);
    Subject currentUser = SecurityUtils.getSubject();
    if (!currentUser.isAuthenticated()) {
        UsernamePasswordToken token = new UsernamePasswordToken("user", "password");
        token.setRememberMe(true);
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            log.error("Username Not Found!", uae);
        } catch (IncorrectCredentialsException ice) {
            log.error("Invalid Credentials!", ice);
        } catch (LockedAccountException lae) {
            log.error("Your Account is Locked!", lae);
        } catch (AuthenticationException ae) {
            log.error("Unexpected Error!", ae);
        }
    }
    log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");
    if (currentUser.hasRole("admin")) {
        log.info("Welcome Admin");
    } else if (currentUser.hasRole("editor")) {
        log.info("Welcome, Editor!");
    } else if (currentUser.hasRole("author")) {
        log.info("Welcome, Author");
    } else {
        log.info("Welcome, Guest");
    }
    if (currentUser.isPermitted("articles:compose")) {
        log.info("You can compose an article");
    } else {
        log.info("You are not permitted to compose an article!");
    }
    if (currentUser.isPermitted("articles:save")) {
        log.info("You can save articles");
    } else {
        log.info("You can not save articles");
    }
    if (currentUser.isPermitted("articles:publish")) {
        log.info("You can publish articles");
    } else {
        log.info("You can not publish articles");
    }
    Session session = currentUser.getSession();
    session.setAttribute("key", "value");
    String value = (String) session.getAttribute("key");
    if (value.equals("value")) {
        log.info("Retrieved the correct value! [" + value + "]");
    }
    currentUser.logout();
    System.exit(0);
}
Also used : SecurityManager(org.apache.shiro.mgt.SecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) IniRealm(org.apache.shiro.realm.text.IniRealm) Realm(org.apache.shiro.realm.Realm) Subject(org.apache.shiro.subject.Subject) Session(org.apache.shiro.session.Session)

Example 24 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class IniSecurityManagerFactory method createSecurityManager.

@SuppressWarnings({ "unchecked" })
private SecurityManager createSecurityManager(Ini ini, Ini.Section mainSection) {
    getReflectionBuilder().setObjects(createDefaults(ini, mainSection));
    Map<String, ?> objects = buildInstances(mainSection);
    SecurityManager securityManager = getSecurityManagerBean();
    boolean autoApplyRealms = isAutoApplyRealms(securityManager);
    if (autoApplyRealms) {
        // realms and realm factory might have been created - pull them out first so we can
        // initialize the securityManager:
        Collection<Realm> realms = getRealms(objects);
        // set them on the SecurityManager
        if (!CollectionUtils.isEmpty(realms)) {
            applyRealmsToSecurityManager(realms, securityManager);
        }
    }
    return securityManager;
}
Also used : DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) RealmSecurityManager(org.apache.shiro.mgt.RealmSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) IniRealm(org.apache.shiro.realm.text.IniRealm) Realm(org.apache.shiro.realm.Realm)

Example 25 with SecurityManager

use of org.apache.shiro.mgt.SecurityManager in project shiro by apache.

the class DelegatingSubjectTest method testRunAs.

@Test
public void testRunAs() {
    Ini ini = new Ini();
    Ini.Section users = ini.addSection("users");
    users.put("user1", "user1,role1");
    users.put("user2", "user2,role2");
    users.put("user3", "user3,role3");
    IniSecurityManagerFactory factory = new IniSecurityManagerFactory(ini);
    SecurityManager sm = factory.getInstance();
    // login as user1
    Subject subject = new Subject.Builder(sm).buildSubject();
    subject.login(new UsernamePasswordToken("user1", "user1"));
    assertFalse(subject.isRunAs());
    assertEquals("user1", subject.getPrincipal());
    assertTrue(subject.hasRole("role1"));
    assertFalse(subject.hasRole("role2"));
    assertFalse(subject.hasRole("role3"));
    // no previous principals since we haven't called runAs yet
    assertNull(subject.getPreviousPrincipals());
    // runAs user2:
    subject.runAs(new SimplePrincipalCollection("user2", IniSecurityManagerFactory.INI_REALM_NAME));
    assertTrue(subject.isRunAs());
    assertEquals("user2", subject.getPrincipal());
    assertTrue(subject.hasRole("role2"));
    assertFalse(subject.hasRole("role1"));
    assertFalse(subject.hasRole("role3"));
    // assert we still have the previous (user1) principals:
    PrincipalCollection previous = subject.getPreviousPrincipals();
    assertFalse(previous == null || previous.isEmpty());
    assertTrue(previous.getPrimaryPrincipal().equals("user1"));
    // test the stack functionality:  While as user2, run as user3:
    subject.runAs(new SimplePrincipalCollection("user3", IniSecurityManagerFactory.INI_REALM_NAME));
    assertTrue(subject.isRunAs());
    assertEquals("user3", subject.getPrincipal());
    assertTrue(subject.hasRole("role3"));
    assertFalse(subject.hasRole("role1"));
    assertFalse(subject.hasRole("role2"));
    // assert we still have the previous (user2) principals in the stack:
    previous = subject.getPreviousPrincipals();
    assertFalse(previous == null || previous.isEmpty());
    assertTrue(previous.getPrimaryPrincipal().equals("user2"));
    // drop down to user2:
    subject.releaseRunAs();
    // assert still run as:
    assertTrue(subject.isRunAs());
    assertEquals("user2", subject.getPrincipal());
    assertTrue(subject.hasRole("role2"));
    assertFalse(subject.hasRole("role1"));
    assertFalse(subject.hasRole("role3"));
    // assert we still have the previous (user1) principals:
    previous = subject.getPreviousPrincipals();
    assertFalse(previous == null || previous.isEmpty());
    assertTrue(previous.getPrimaryPrincipal().equals("user1"));
    // drop down to original user1:
    subject.releaseRunAs();
    // assert we're no longer runAs:
    assertFalse(subject.isRunAs());
    assertEquals("user1", subject.getPrincipal());
    assertTrue(subject.hasRole("role1"));
    assertFalse(subject.hasRole("role2"));
    assertFalse(subject.hasRole("role3"));
    // no previous principals in orig state
    assertNull(subject.getPreviousPrincipals());
    subject.logout();
    LifecycleUtils.destroy(sm);
}
Also used : IniSecurityManagerFactory(org.apache.shiro.config.IniSecurityManagerFactory) DefaultSecurityManager(org.apache.shiro.mgt.DefaultSecurityManager) SecurityManager(org.apache.shiro.mgt.SecurityManager) Ini(org.apache.shiro.config.Ini) DelegatingSubject(org.apache.shiro.subject.support.DelegatingSubject) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken) Test(org.junit.Test)

Aggregations

SecurityManager (org.apache.shiro.mgt.SecurityManager)36 DefaultSecurityManager (org.apache.shiro.mgt.DefaultSecurityManager)13 IniSecurityManagerFactory (org.apache.shiro.config.IniSecurityManagerFactory)11 Test (org.junit.Test)11 Injector (com.google.inject.Injector)8 Provides (com.google.inject.Provides)6 Subject (org.apache.shiro.subject.Subject)6 Session (org.apache.shiro.session.Session)5 WebSecurityManager (org.apache.shiro.web.mgt.WebSecurityManager)5 Realm (org.apache.shiro.realm.Realm)4 IniRealm (org.apache.shiro.realm.text.IniRealm)4 DefaultWebSecurityManager (org.apache.shiro.web.mgt.DefaultWebSecurityManager)4 UnavailableSecurityManagerException (org.apache.shiro.UnavailableSecurityManagerException)3 Ini (org.apache.shiro.config.Ini)3 DelegatingSubject (org.apache.shiro.subject.support.DelegatingSubject)3 AfterClass (org.junit.AfterClass)3 Callable (java.util.concurrent.Callable)2 ServletContext (javax.servlet.ServletContext)2 ConfigurationException (org.apache.shiro.config.ConfigurationException)2 ShiroModuleTest (org.apache.shiro.guice.ShiroModuleTest)2