Example usage for org.apache.shiro.authc.credential HashedCredentialsMatcher setHashAlgorithmName

List of usage examples for org.apache.shiro.authc.credential HashedCredentialsMatcher setHashAlgorithmName

Introduction

In this page you can find the example usage for org.apache.shiro.authc.credential HashedCredentialsMatcher setHashAlgorithmName.

Prototype

public void setHashAlgorithmName(String hashAlgorithmName) 

Source Link

Document

Sets the Hash org.apache.shiro.crypto.hash.Hash#getAlgorithmName() algorithmName to use when performing hashes for credentials matching.

Usage

From source file:au.org.theark.core.security.ArkLdapRealm.java

License:Open Source License

public ArkLdapRealm() {
    setName("arkLdapRealm"); // This name must match the name in the User class's getPrincipals() method
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
    hashedCredentialsMatcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);
    setCredentialsMatcher(hashedCredentialsMatcher);
}

From source file:cn.com.xl.core.shiro.ShiroDbRealm.java

License:Apache License

/**
 * ??/*from w ww  . java2s.  com*/
 */
@PostConstruct
public void setCredentialMatcher() {
    HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
    credentialsMatcher.setHashAlgorithmName(ShiroKit.hashAlgorithmName);
    credentialsMatcher.setHashIterations(ShiroKit.hashIterations);
    setCredentialsMatcher(credentialsMatcher);
}

From source file:com.aegeus.core.AuthenticationConfiguration.java

License:Apache License

@Bean
public JdbcRealm realm() {
    ConfigObject config = config();/*w  w  w  . j  a v  a 2  s  .c o m*/

    String uri = String.format("jdbc:%s://%s:%d/%s", config.getWorkflow().getMetaStore().getType(),
            config.getWorkflow().getMetaStore().getHost(), config.getWorkflow().getMetaStore().getPort(),
            config.getWorkflow().getMetaStore().getDb());

    // initialize meta store database connection
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(uri);
    ds.setUser(config.getWorkflow().getMetaStore().getUsername());
    ds.setPassword(config.getWorkflow().getMetaStore().getPassword());

    HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
    matcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);

    JdbcRealm realm = new JdbcRealm();
    realm.setDataSource(ds);
    realm.setPermissionsLookupEnabled(true);
    realm.setAuthenticationQuery("SELECT pass FROM users WHERE user = ?");
    realm.setPermissionsQuery(
            "SELECT p.permission FROM permissions p INNER JOIN users u ON p.user_id = u.id WHERE u.user = ?");
    realm.setUserRolesQuery(
            "SELECT r.role FROM roles r INNER JOIN users u ON u.id = r.user_id WHERE u.user = ?");
    realm.setCredentialsMatcher(matcher);
    realm.init();

    return realm;
}

From source file:com.cuisongliu.springboot.shiro.autoconfig.ShiroAutoConfig.java

License:Open Source License

@Bean
CredentialsMatcher credentialsMatcher() {
    HashedCredentialsMatcher md5CredentialsMatcher = new HashedCredentialsMatcher();
    md5CredentialsMatcher.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
    md5CredentialsMatcher.setHashIterations(springShiroProperties.getMd5Iterations());
    return md5CredentialsMatcher;
}

From source file:com.eheobo.samples.shiro.security.SampleRealm.java

License:Apache License

public SampleRealm() {
    setName("SampleRealm"); //This name must match the name in the User class's getPrincipals() method
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
    hashedCredentialsMatcher.setHashAlgorithmName(Sha256Hash.ALGORITHM_NAME);
    setCredentialsMatcher(hashedCredentialsMatcher);
}

From source file:demo.learn.shiro.pojo.UserTest.java

License:Apache License

/**
 * Tests basic salting./*w w  w . jav a  2 s.c  o m*/
 */
@Test
public void testBasicSalting() {
    try {
        String username = "root";
        String plainTextPassword = "root";
        RandomNumberGenerator rng = new SecureRandomNumberGenerator();

        UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);

        ByteSource salt = rng.nextBytes();

        String hashedPasswordBase64 = new Sha256Hash(plainTextPassword, salt, 1024).toBase64();

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashedPasswordBase64, salt,
                "learn.shiro");

        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashIterations(1024);
        matcher.setStoredCredentialsHexEncoded(false);
        matcher.setHashAlgorithmName("SHA-256");

        boolean result = matcher.doCredentialsMatch(token, info);
        Assert.assertEquals(true, result);
    } catch (Exception ex) {
        ex.printStackTrace();
        Assert.assertEquals(ex.getMessage(), false, true);
    }
}

From source file:demo.learn.shiro.realm.CustomRealmTest.java

License:Apache License

/**
 * Tests matching./* w  w w . java 2  s.co m*/
 */
public void testMatch() {
    try {
        String username = "root";
        String password = "root";

        UserDao userDao = UserDao.getInstance();
        userDao.delete(username);
        userDao.create(username, password);

        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("SHA-256");
        matcher.setHashIterations(S.HASH_ITER);
        matcher.setStoredCredentialsHexEncoded(false);

        CustomRealm realm = new CustomRealm();
        realm.setCredentialsMatcher(matcher);

        AuthenticationInfo info = realm.getAuthenticationInfo(token);
        Assert.assertNotNull(info);
    } catch (Exception ex) {
        Assert.assertFalse(ex.getMessage(), true);
    }
}

From source file:demo.learn.shiro.tool.PasswordMatcherTool.java

License:Apache License

/**
 * Main method.//from w  ww  .  ja  v  a2s  .c o  m
 * @param args Pass in plain text password, hashed password,
 * and salt. These arguments are generated from
 * {@link PasswordEncryptionTool}.
 * @throws ParseException
 */
@SuppressWarnings("static-access")
public static void main(String[] args) throws ParseException {
    String username = "";
    String plainTextPassword = "root";
    String hashedPasswordBase64 = "ZzIkhapTVzGkhWRQqdUn2zod5npt9RJMSni8My6X+r8=";
    String saltBase64 = "BobnkcsIXcZGksA30eOySA==";
    String realmName = "";

    Option p = OptionBuilder.withArgName("password").hasArg().withDescription("plain text password")
            .isRequired(false).create('p');
    Option h = OptionBuilder.withArgName("password").hasArg().withDescription("hashed password")
            .isRequired(false).create('h');
    Option s = OptionBuilder.withArgName("salt").hasArg().withDescription("salt (Base64 encoded)")
            .isRequired(false).create('s');

    Options options = new Options();
    options.addOption(p);
    options.addOption(h);
    options.addOption(s);

    try {
        CommandLineParser parser = new BasicParser();
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("p")) {
            plainTextPassword = cmd.getOptionValue("p");
        }
        if (cmd.hasOption("h")) {
            hashedPasswordBase64 = cmd.getOptionValue("h");
        }
        if (cmd.hasOption("s")) {
            saltBase64 = cmd.getOptionValue("s");
        }
    } catch (ParseException pe) {
        String cmdLineSyntax = "java -cp %CLASSPATH% demo.learn.shiro.tool.PasswordMatcherTool";
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(cmdLineSyntax, options, false);
        return;
    }

    SimpleByteSource salt = new SimpleByteSource(Base64.decode(saltBase64));
    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, hashedPasswordBase64, salt,
            realmName);
    UsernamePasswordToken token = new UsernamePasswordToken(username, plainTextPassword);

    HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
    matcher.setHashIterations(S.HASH_ITER);
    matcher.setStoredCredentialsHexEncoded(false);
    matcher.setHashAlgorithmName(S.ALGORITHM_NAME);

    boolean result = matcher.doCredentialsMatch(token, info);
    System.out.println("match? " + result);

}

From source file:JavaMvc.Config.SampleRealm.java

License:Apache License

public SampleRealm() {
    setName("SampleRealm"); //This name must match the name in the User class's getPrincipals() method
    HashedCredentialsMatcher matcher = new HashedCredentialsMatcher();
    matcher.setHashAlgorithmName("SHA-256");
    setCredentialsMatcher(matcher);/* w w  w  .j a v a  2 s.c o  m*/
}

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));
    }/* w  w  w .  j  a  v  a  2s .  c o  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);

}