Example usage for org.apache.shiro SecurityUtils setSecurityManager

List of usage examples for org.apache.shiro SecurityUtils setSecurityManager

Introduction

In this page you can find the example usage for org.apache.shiro SecurityUtils setSecurityManager.

Prototype

public static void setSecurityManager(SecurityManager securityManager) 

Source Link

Document

Sets a VM (static) singleton SecurityManager, specifically for transparent use in the #getSubject() getSubject() implementation.

Usage

From source file:name.brucephillips.rolesecurity.QuickstartSpring.java

License:Apache License

public static void main(String... args) {

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("shiro.spring.xml");

    SecurityManager securityManager = (SecurityManager) applicationContext.getBean("securityManager");
    ;/*  w w w.  ja  va2s .c  o  m*/

    SecurityUtils.setSecurityManager(securityManager);

    Subject currentUser = SecurityUtils.getSubject();

    Session session = currentUser.getSession();
    session.setAttribute("someKey", "aValue");
    String value = (String) session.getAttribute("someKey");
    if (value.equals("aValue")) {
        log.info("Retrieved the correct value! [" + value + "]");
    }

    if (!currentUser.isAuthenticated()) {
        UsernamePasswordToken token = new UsernamePasswordToken("sue@hotmail.com", "sue");
        token.setRememberMe(true);
        try {
            currentUser.login(token);
        } catch (UnknownAccountException uae) {
            log.info("There is no user with username of " + token.getPrincipal());
        } catch (IncorrectCredentialsException ice) {
            log.info("Password for account " + token.getPrincipal() + " was incorrect!");
        } catch (LockedAccountException lae) {
            log.info("The account for username " + token.getPrincipal() + " is locked.  "
                    + "Please contact your administrator to unlock it.");
        } catch (AuthenticationException ae) {
            ae.printStackTrace();
        }
    }

    log.info("User [" + currentUser.getPrincipal() + "] logged in successfully.");

    if (currentUser.hasRole("user")) {
        log.info("====> ROLE OK");
    } else {
        log.info("====> ROLE KO");
    }

    if (currentUser.isPermitted("winnebago:drive:eagle7")) {
        log.info("===> PERM OK");
    } else {
        log.info("==> PERM KO");
    }

    currentUser.logout();

}

From source file:net.felsing.client_cert.RaContextListener.java

License:Open Source License

private void initShiro(ServletContextEvent servletContextEvent) {
    String shiroIniFile = PropertyLoader.getProperties().getProperty("shiroini");
    servletContextEvent.getServletContext().setInitParameter("shiroConfigLocations", "file:" + shiroIniFile);

    Factory<SecurityManager> factory = new IniSecurityManagerFactory(shiroIniFile);
    SecurityManager securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);

    logger.info("Shiro loaded, using " + shiroIniFile);
}

From source file:no.priv.bang.ukelonn.testutils.TestUtils.java

License:Apache License

/***
 * Fake injected OSGi services.//from   w  w w .  j  av  a2 s . co  m
 * @return the serviceprovider implmenting the UkelonnService
 */
public static UkelonnServiceProvider setupFakeOsgiServices() {
    ukelonnServiceSingleton = new UkelonnServiceProvider();
    ukelonnServiceSingleton.activate();
    UkelonnDatabaseProvider ukelonnDatabaseProvider = new UkelonnDatabaseProvider();
    DataSourceFactory derbyDataSourceFactory = new DerbyDataSourceFactory();
    ukelonnDatabaseProvider.setDataSourceFactory(derbyDataSourceFactory);
    LogService logservice = new MockLogService();
    ukelonnDatabaseProvider.setLogService(logservice);
    ukelonnDatabaseProvider.activate();

    // Set up shiro
    MemorySession session = new MemorySession();
    session.activate();
    UkelonnRealm realm = new UkelonnRealm();
    realm.setDatabase(ukelonnDatabaseProvider.get());
    realm.activate();
    shirofilter = new UkelonnShiroFilter();
    shirofilter.setRealm(realm);
    shirofilter.setSession(session);
    shirofilter.activate();
    SecurityUtils.setSecurityManager(shirofilter.getSecurityManager());

    ukelonnServiceSingleton.setUkelonnDatabase(ukelonnDatabaseProvider.get());
    ukelonnServiceSingleton.setLogservice(logservice);
    return ukelonnServiceSingleton;
}

From source file:org.abstractj.config.ShiroConfig.java

License:Apache License

@PostConstruct
public void init() {
    SecurityManager securityManager = new DefaultSecurityManager(securityRealm);
    SecurityUtils.setSecurityManager(securityManager);
}

From source file:org.apache.batchee.shiro.ShiroTest.java

License:Apache License

@BeforeClass
public static void initShiro() {
    SecurityUtils.setSecurityManager(new IniSecurityManagerFactory("classpath:shiro.ini").getInstance());
}

From source file:org.apache.camel.component.shiro.security.ShiroSecurityPolicy.java

License:Apache License

public ShiroSecurityPolicy(String iniResourcePath) {
    this();/*from   w ww .  j  a  v a2  s. c  o m*/
    Factory<SecurityManager> factory = new IniSecurityManagerFactory(iniResourcePath);
    securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
}

From source file:org.apache.camel.component.shiro.security.ShiroSecurityPolicy.java

License:Apache License

public ShiroSecurityPolicy(Ini ini) {
    this();/*from   ww w. ja  v  a  2 s. c o m*/
    Factory<SecurityManager> factory = new IniSecurityManagerFactory(ini);
    securityManager = factory.getInstance();
    SecurityUtils.setSecurityManager(securityManager);
}

From source file:org.apache.coheigea.cxf.shiro.authentication.ShiroUTValidator.java

License:Apache License

public ShiroUTValidator(String iniResourcePath) {
    Factory<SecurityManager> factory = new IniSecurityManagerFactory(iniResourcePath);
    SecurityUtils.setSecurityManager(factory.getInstance());
}

From source file:org.apache.geode.internal.security.IntegratedSecurityService.java

License:Apache License

/**
 * initialize Shiro's Security Manager and Security Utilities
 *//*from  w w w  . java  2  s . c om*/
public void initSecurity(Properties securityProps) {
    if (securityProps == null) {
        return;
    }

    String shiroConfig = securityProps.getProperty(SECURITY_SHIRO_INIT);
    String securityManagerConfig = securityProps.getProperty(SECURITY_MANAGER);
    String clientAuthenticatorConfig = securityProps.getProperty(SECURITY_CLIENT_AUTHENTICATOR);
    String peerAuthenticatorConfig = securityProps.getProperty(SECURITY_PEER_AUTHENTICATOR);

    if (!StringUtils.isBlank(shiroConfig)) {
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:" + shiroConfig);

        // we will need to make sure that shiro uses a case sensitive permission resolver
        Section main = factory.getIni().addSection("main");
        main.put("geodePermissionResolver", "org.apache.geode.internal.security.shiro.GeodePermissionResolver");
        if (!main.containsKey("iniRealm.permissionResolver")) {
            main.put("iniRealm.permissionResolver", "$geodePermissionResolver");
        }

        org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        isIntegratedSecurity = true;
        isClientAuthenticator = false;
        isPeerAuthenticator = false;
    }
    // only set up shiro realm if user has implemented SecurityManager
    else if (!StringUtils.isBlank(securityManagerConfig)) {
        SecurityManager securityManager = SecurityService.getObjectOfTypeFromClassName(securityManagerConfig,
                SecurityManager.class);
        securityManager.init(securityProps);
        this.setSecurityManager(securityManager);
    } else {
        isIntegratedSecurity = null;
        isClientAuthenticator = !StringUtils.isBlank(clientAuthenticatorConfig);
        isPeerAuthenticator = !StringUtils.isBlank(peerAuthenticatorConfig);
    }

    // this initializes the post processor
    String customPostProcessor = securityProps.getProperty(SECURITY_POST_PROCESSOR);
    if (!StringUtils.isBlank(customPostProcessor)) {
        postProcessor = SecurityService.getObjectOfTypeFromClassName(customPostProcessor, PostProcessor.class);
        postProcessor.init(securityProps);
    } else {
        postProcessor = null;
    }
}

From source file:org.apache.geode.internal.security.IntegratedSecurityService.java

License:Apache License

public void close() {
    if (securityManager != null) {
        securityManager.close();/*w  w  w.  java  2 s  . com*/
        securityManager = null;
    }

    if (postProcessor != null) {
        postProcessor.close();
        postProcessor = null;
    }
    ThreadContext.remove();
    SecurityUtils.setSecurityManager(null);
    isIntegratedSecurity = null;
    isClientAuthenticator = false;
    isPeerAuthenticator = false;
}