Example usage for org.springframework.security.authentication ProviderManager getProviders

List of usage examples for org.springframework.security.authentication ProviderManager getProviders

Introduction

In this page you can find the example usage for org.springframework.security.authentication ProviderManager getProviders.

Prototype

public List<AuthenticationProvider> getProviders() 

Source Link

Usage

From source file:org.red5.demo.auth.Application.java

@Override
public boolean appStart(IScope app) {
    // authentication management
    ProviderManager authManager = (ProviderManager) applicationContext.getBean("authManager");
    log.info("Available auth providers: {}", authManager.getProviders().size());
    if (authManager.isEraseCredentialsAfterAuthentication()) {
        log.info("Provider set to erase creds, changing to NOT do this");
        authManager.setEraseCredentialsAfterAuthentication(false);
    }//  w  ww.  jav  a 2s .  co  m
    // add an authentication listener
    addListener((IApplication) applicationContext.getBean("authHandler"));
    // hit the super class
    return super.appStart(app);
}

From source file:org.openmrs.contrib.metadatarepository.webapp.listener.StartupListener.java

/**
 * {@inheritDoc}/*from  www  .  java 2s  .  com*/
 */
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {
    log.debug("Initializing context...");

    ServletContext context = event.getServletContext();

    // Orion starts Servlets before Listeners, so check if the config
    // object already exists
    Map<String, Object> config = (HashMap<String, Object>) context.getAttribute(Constants.CONFIG);

    if (config == null) {
        config = new HashMap<String, Object>();
    }

    if (context.getInitParameter(Constants.CSS_THEME) != null) {
        config.put(Constants.CSS_THEME, context.getInitParameter(Constants.CSS_THEME));
    }

    ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    /*String[] beans = ctx.getBeanDefinitionNames();
    for (String bean : beans) {
    log.debug(bean);
    }*/

    PasswordEncoder passwordEncoder = null;
    try {
        ProviderManager provider = (ProviderManager) ctx
                .getBean("org.springframework.security.authentication.ProviderManager#0");
        for (Object o : provider.getProviders()) {
            AuthenticationProvider p = (AuthenticationProvider) o;
            if (p instanceof RememberMeAuthenticationProvider) {
                config.put("rememberMeEnabled", Boolean.TRUE);
            } else if (ctx.getBean("passwordEncoder") != null) {
                passwordEncoder = (PasswordEncoder) ctx.getBean("passwordEncoder");
            }
        }
    } catch (NoSuchBeanDefinitionException n) {
        log.debug("authenticationManager bean not found, assuming test and ignoring...");
        // ignore, should only happen when testing
    }

    context.setAttribute(Constants.CONFIG, config);

    // output the retrieved values for the Init and Context Parameters
    if (log.isDebugEnabled()) {
        log.debug("Remember Me Enabled? " + config.get("rememberMeEnabled"));
        if (passwordEncoder != null) {
            log.debug("Password Encoder: " + passwordEncoder.getClass().getSimpleName());
        }
        log.debug("Populating drop-downs...");
    }

    setupContext(context);
}

From source file:alpha.portal.webapp.listener.StartupListener.java

/**
 * {@inheritDoc}/*  w  ww  .  ja  v a2s. co m*/
 */
@SuppressWarnings("unchecked")
public void contextInitialized(final ServletContextEvent event) {
    StartupListener.log.debug("Initializing context...");

    final ServletContext context = event.getServletContext();

    // Orion starts Servlets before Listeners, so check if the config
    // object already exists
    Map<String, Object> config = (HashMap<String, Object>) context.getAttribute(Constants.CONFIG);

    if (config == null) {
        config = new HashMap<String, Object>();
    }

    if (context.getInitParameter(Constants.CSS_THEME) != null) {
        config.put(Constants.CSS_THEME, context.getInitParameter(Constants.CSS_THEME));
    }

    final ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    /*
     * String[] beans = ctx.getBeanDefinitionNames(); for (String bean :
     * beans) { log.debug(bean); }
     */

    PasswordEncoder passwordEncoder = null;
    try {
        final ProviderManager provider = (ProviderManager) ctx
                .getBean("org.springframework.security.authentication.ProviderManager#0");
        for (final Object o : provider.getProviders()) {
            final AuthenticationProvider p = (AuthenticationProvider) o;
            if (p instanceof RememberMeAuthenticationProvider) {
                config.put("rememberMeEnabled", Boolean.TRUE);
            } else if (ctx.getBean("passwordEncoder") != null) {
                passwordEncoder = (PasswordEncoder) ctx.getBean("passwordEncoder");
            }
        }
    } catch (final NoSuchBeanDefinitionException n) {
        StartupListener.log.debug("authenticationManager bean not found, assuming test and ignoring...");
        // ignore, should only happen when testing
    }

    context.setAttribute(Constants.CONFIG, config);

    // output the retrieved values for the Init and Context Parameters
    if (StartupListener.log.isDebugEnabled()) {
        StartupListener.log.debug("Remember Me Enabled? " + config.get("rememberMeEnabled"));
        if (passwordEncoder != null) {
            StartupListener.log.debug("Password Encoder: " + passwordEncoder.getClass().getSimpleName());
        }
        StartupListener.log.debug("Populating drop-downs...");
    }

    StartupListener.setupContext(context);
}

From source file:com.cartmatic.estore.webapp.AppContextLoaderImpl.java

public void reloadConfig() {
    // Spring Security bean?????
    try {/*from   w ww  .j  av  a 2  s .co m*/
        boolean rememberMeEnabled = false;
        ProviderManager provider = (ProviderManager) springContext.getBean("authenticationManager");

        for (Iterator it = provider.getProviders().iterator(); it.hasNext();) {
            AuthenticationProvider p = (AuthenticationProvider) it.next();
            if (p instanceof RememberMeAuthenticationProvider) {
                rememberMeEnabled = true;
            }
        }
        ConfigUtil.getInstance().setRememberMeEnabled(rememberMeEnabled);
    } catch (NoSuchBeanDefinitionException n) {
        logger.warn("NoSuchBeanDefinitionException, ignore.");
    }

    ConfigUtil.getInstance().checkConfigAtStartup();
    servletContext.setAttribute(Constants.CONFIG, ConfigUtil.getInstance());
    logger.debug("Config reloaded [OK].");
}

From source file:org.musicrecital.webapp.listener.StartupListener.java

/**
 * {@inheritDoc}/*from   w  w w.j  av  a  2 s  . c  o m*/
 */
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {
    log.debug("Initializing context...");

    ServletContext context = event.getServletContext();

    // Orion starts Servlets before Listeners, so check if the config
    // object already exists
    Map<String, Object> config = (HashMap<String, Object>) context.getAttribute(Constants.CONFIG);

    if (config == null) {
        config = new HashMap<String, Object>();
    }

    ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);

    PasswordEncoder passwordEncoder = null;
    try {
        ProviderManager provider = (ProviderManager) ctx
                .getBean("org.springframework.security.authentication.ProviderManager#0");
        for (Object o : provider.getProviders()) {
            AuthenticationProvider p = (AuthenticationProvider) o;
            if (p instanceof RememberMeAuthenticationProvider) {
                config.put("rememberMeEnabled", Boolean.TRUE);
            } else if (ctx.getBean("passwordEncoder") != null) {
                passwordEncoder = (PasswordEncoder) ctx.getBean("passwordEncoder");
            }
        }
    } catch (NoSuchBeanDefinitionException n) {
        log.debug("authenticationManager bean not found, assuming test and ignoring...");
        // ignore, should only happen when testing
    }

    context.setAttribute(Constants.CONFIG, config);

    // output the retrieved values for the Init and Context Parameters
    if (log.isDebugEnabled()) {
        log.debug("Remember Me Enabled? " + config.get("rememberMeEnabled"));
        if (passwordEncoder != null) {
            log.debug("Password Encoder: " + passwordEncoder.getClass().getSimpleName());
        }
        log.debug("Populating drop-downs...");
    }

    setupContext(context);

    // Determine version number for CSS and JS Assets
    String appVersion = null;
    try {
        InputStream is = context.getResourceAsStream("/META-INF/MANIFEST.MF");
        if (is == null) {
            log.warn("META-INF/MANIFEST.MF not found.");
        } else {
            Manifest mf = new Manifest();
            mf.read(is);
            Attributes atts = mf.getMainAttributes();
            appVersion = atts.getValue("Implementation-Version");
        }
    } catch (IOException e) {
        log.error("I/O Exception reading manifest: " + e.getMessage());
    }

    // If there was a build number defined in the war, then use it for
    // the cache buster. Otherwise, assume we are in development mode
    // and use a random cache buster so developers don't have to clear
    // their browser cache.
    if (appVersion == null || appVersion.contains("SNAPSHOT")) {
        appVersion = "" + new Random().nextInt(100000);
    }

    log.info("Application version set to: " + appVersion);
    context.setAttribute(Constants.ASSETS_VERSION, appVersion);
}

From source file:org.red5.webapps.admin.Application.java

@Override
public boolean appStart(IScope app) {
    log.info("Admin application started");
    // authentication check
    ProviderManager authManager = (ProviderManager) applicationContext.getBean("authenticationManager");
    log.info("Available auth providers: {}", authManager.getProviders().size());
    if (authManager.isEraseCredentialsAfterAuthentication()) {
        log.info("Provider set to erase creds, changing to NOT do this");
        authManager.setEraseCredentialsAfterAuthentication(false);
    }//w  ww  . j  a  v  a 2s.  c  om
    // add an authentication listener
    if (enforceAuthentication) {
        addListener(new Red5AuthenticationHandler(applicationContext));
    }
    return true;
}

From source file:ubc.pavlab.aspiredb.server.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with admin privileges, and put the token in the context. This means your tests
 * will be authorized to do anything an administrator would be able to do.
 *///from   w ww . j av a  2  s.  c  o  m
protected void grantAdminAuthority(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    // Grant all roles to test user.
    TestingAuthenticationToken token = new TestingAuthenticationToken("administrator", "administrator",
            Arrays.asList(new GrantedAuthority[] {
                    new SimpleGrantedAuthority(AuthorityConstants.ADMIN_GROUP_AUTHORITY) }));

    token.setAuthenticated(true);

    putTokenInContext(token);
}

From source file:ubc.pavlab.aspiredb.server.BaseSpringContextTest.java

protected void grantAnonAuthority(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    // Grant all roles to test user.
    TestingAuthenticationToken token = new TestingAuthenticationToken("anon", "anon",
            Arrays.asList(new GrantedAuthority[] {
                    new SimpleGrantedAuthority(AuthorityConstants.IS_AUTHENTICATED_ANONYMOUSLY) }));

    token.setAuthenticated(true);//from ww  w . j  ava 2s  .co  m

    putTokenInContext(token);
}

From source file:ubc.pavlab.aspiredb.server.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with regular user privileges, and put the token in the context. This means your
 * tests will be authorized to do anything that user could do
 *//*from  w w  w.j  a  va 2s. c o m*/
protected void switchToUser(ApplicationContext ctx, String username) {

    UserDetails user = userManager.loadUserByUsername(username);

    List<GrantedAuthority> authrs = new ArrayList<GrantedAuthority>(user.getAuthorities());

    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    TestingAuthenticationToken token = new TestingAuthenticationToken(username, "testing", authrs);
    token.setAuthenticated(true);

    putTokenInContext(token);
}

From source file:ubic.gemma.core.util.test.BaseSpringContextTest.java

/**
 * Grant authority to a test user, with admin privileges, and put the token in the context. This means your tests
 * will be authorized to do anything an administrator would be able to do.
 *
 * @param ctx context//w  w w  .j av  a 2s  .c o  m
 */
protected void grantAdminAuthority(ApplicationContext ctx) {
    ProviderManager providerManager = (ProviderManager) ctx.getBean("authenticationManager");
    providerManager.getProviders().add(new TestingAuthenticationProvider());

    // Grant all roles to test user.
    TestingAuthenticationToken token = new TestingAuthenticationToken("administrator", "administrator",
            Arrays.asList(new GrantedAuthority[] {
                    new SimpleGrantedAuthority(AuthorityConstants.ADMIN_GROUP_AUTHORITY) }));

    token.setAuthenticated(true);

    AuthenticationTestingUtil.putTokenInContext(token);
}