Example usage for org.apache.shiro.mgt RealmSecurityManager setRealm

List of usage examples for org.apache.shiro.mgt RealmSecurityManager setRealm

Introduction

In this page you can find the example usage for org.apache.shiro.mgt RealmSecurityManager setRealm.

Prototype

public void setRealm(Realm realm) 

Source Link

Document

Convenience method for applications using a single realm that merely wraps the realm in a list and then invokes the #setRealms method.

Usage

From source file:com.josue.shiro.cdi.custom.CustomEnvironmentLoaderListener.java

@Override
protected WebEnvironment createEnvironment(ServletContext pServletContext) {
    WebEnvironment environment = super.createEnvironment(pServletContext);
    RealmSecurityManager rsm = (RealmSecurityManager) environment.getSecurityManager();

    PasswordService passwordService = new DefaultPasswordService();
    PasswordMatcher passwordMatcher = new PasswordMatcher();
    passwordMatcher.setPasswordService(passwordService);

    jpaRealm.setCredentialsMatcher(passwordMatcher);
    rsm.setRealm(jpaRealm);
    ((DefaultWebEnvironment) environment).setSecurityManager(rsm);
    return environment;
}

From source file:net.scran24.user.server.services.InitListener.java

License:Apache License

public void contextInitialized(ServletContextEvent contextEvent) {
    ServletContext context = contextEvent.getServletContext();

    Map<String, String> configParams = new HashMap<String, String>();

    Enumeration<String> paramNames = context.getInitParameterNames();

    while (paramNames.hasMoreElements()) {
        String name = paramNames.nextElement();
        configParams.put(name, context.getInitParameter(name));
    }//from  ww w  .j a  v  a  2 s.  co  m

    AbstractModule configModule;

    try {
        Constructor<?> ctor = Class.forName(context.getInitParameter("config-module"))
                .getConstructor(Map.class);
        configModule = (AbstractModule) ctor.newInstance(configParams);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException
            | SecurityException | IllegalArgumentException | InvocationTargetException e) {
        throw new RuntimeException(e);
    }

    Injector injector = Guice.createInjector(configModule);

    context.setAttribute("intake24.injector", injector);

    WebEnvironment shiroEnvironment = (WebEnvironment) context
            .getAttribute(EnvironmentLoader.ENVIRONMENT_ATTRIBUTE_KEY);
    RealmSecurityManager securityManager = (RealmSecurityManager) shiroEnvironment.getSecurityManager();

    ScranAuthRealm securityRealm = new ScranAuthRealm(injector.getInstance(DataStore.class));

    HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
    credentialsMatcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);
    credentialsMatcher.setStoredCredentialsHexEncoded(false);
    credentialsMatcher.setHashIterations(1024);

    securityRealm.setCredentialsMatcher(credentialsMatcher);
    securityManager.setRealm(securityRealm);

}

From source file:org.panifex.security.shiro.env.ModularWebEnvironment.java

License:Open Source License

/**
 * Synchronizes the realm's map with the security manager.
 *//*from ww w  . j ava 2s. c o  m*/
protected final void synchronizeRealms() {
    RealmSecurityManager securityManager = (RealmSecurityManager) getSecurityManager();

    if (securityManager != null) {
        if (!realms.isEmpty()) {
            securityManager.setRealms(realms.values());
        } else {
            // bind simple realm because it should be at least one realm
            securityManager.setRealm(new SimpleAccountRealm());
        }
    } else {
        log.warn("Realm security manager is not available. Realms is not updated");
    }
}

From source file:uk.co.q3c.v7.base.shiro.ShiroIntegrationTestBase.java

License:Apache License

@Before
public void setup() {
    // trick the SubjectBuilder into using a WebSubject
    when(servletRequest.getSession(false)).thenReturn(httpSession);
    RealmSecurityManager rsm = (RealmSecurityManager) getSecurityManager();
    rsm.setRealm(getRealm());
    // 1. Build the Subject instance for the test to run:
    subject = new WebSubject.Builder(getSecurityManager(), servletRequest, servletResponse).buildSubject();
    // 2. Bind the subject to the current thread:
    setSubject(subject);// w  w  w.j a  va  2 s  . c o  m

}

From source file:uk.q3c.krail.core.shiro.ShiroIntegrationTestBase.java

License:Apache License

@Before
public void setupShiro() {
    // trick the SubjectBuilder into using a WebSubject
    when(servletRequest.getSession(false)).thenReturn(httpSession);
    RealmSecurityManager rsm = getSecurityManager();
    rsm.setRealm(getRealm());
    // 1. Build the Subject instance for the test to run:
    subject = new WebSubject.Builder(getSecurityManager(), servletRequest, servletResponse).buildSubject();
    // 2. Bind the subject to the current thread:
    setSubject(subject);/*from   ww w  .j  av a 2s .  c om*/
    when(subjectPro.get()).thenReturn(subject);

}