Example usage for org.springframework.security.provisioning JdbcUserDetailsManager createUser

List of usage examples for org.springframework.security.provisioning JdbcUserDetailsManager createUser

Introduction

In this page you can find the example usage for org.springframework.security.provisioning JdbcUserDetailsManager createUser.

Prototype

public void createUser(final UserDetails user) 

Source Link

Usage

From source file:org.fracturedatlas.athena.admin.AthenaAdmin.java

public static void main(String[] args) {

    Console c = System.console();
    if (c == null) {
        System.exit(1);//  ww w  .  j a  v  a  2s . c o  m
    }

    Properties props = new Properties();
    ClassPathResource cpr = new ClassPathResource("admin.properties");
    try {
        InputStream in = cpr.getInputStream();
        props.load(in);
        in.close();
    } catch (Exception e) {
        c.format("Could not read properties file admin.properties\n");
        System.exit(1);
    }

    ApplicationContext context = new ClassPathXmlApplicationContext("security.xml");
    JdbcUserDetailsManager userDao = (JdbcUserDetailsManager) context.getBean("userDao");
    Md5PasswordEncoder encoder = (Md5PasswordEncoder) context.getBean("passwordEncoder");

    //TODO: Props file
    String realmName = props.getProperty("athena.admin.realm");

    if (args.length == 0) {
        System.out.println("USAGE: admin [command]");
        System.out.println("Where [command] is one of: create-user");
        System.exit(1);
    }

    Boolean usernameGood = false;
    String login = null;
    while (!usernameGood) {
        login = c.readLine("Enter new username: ");
        if (StringUtils.isBlank(login)) {
            c.format("username cannot be blank, please try again\n");
        } else {
            usernameGood = true;
        }
    }
    Boolean match = false;
    char[] password = null;
    char[] confirmedPassword = null;
    while (!match) {
        password = c.readPassword("Enter password: ");
        confirmedPassword = c.readPassword("Enter password again: ");
        match = Arrays.equals(password, confirmedPassword);
        if (!match) {
            c.format("Passwords do not match please try again\n");
        }
    }

    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new GrantedAuthorityImpl("ROLE_CLIENT_APPLICATION"));
    String clearPassword = new String(password);
    String saltedClearPassword = login + ":" + realmName + ":" + clearPassword;
    String encryptedPassword = encoder.encodePassword(saltedClearPassword, null);
    User user = new User(login, encryptedPassword, true, true, true, true, authorities);
    try {
        userDao.createUser(user);
    } catch (org.springframework.dao.DuplicateKeyException dke) {
        System.out.println("Username [" + user.getUsername() + "] already exists.");
        System.exit(1);
    }

    System.out.println("Successfully created [" + user.getUsername() + "]");
}