PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String hashedPassword = passwordEncoder.encode(yourpassword);
String password = "p4ssword"; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encodedPassword = passwordEncoder.encode(password);
@Test public void passwordShouldMatch() { PasswordEncoder passwordEncoder = new BCryptPasswordEncoder() String plainPassword = "secret"; String hashedPassword = passwordEncoder.encode(plainPassword); assertThat(hashedPassword, matchesPassword(plainPassword)); }
@Autowired private UserDetailsManager users; @Autowired private PasswordEncoder enc; @RequestMapping(...) public String handle(Model model) { users.createUser(new User(username, enc.encode(password), authorities); return "view"; }
String password = "testPassword"; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encodedPassword = passwordEncoder.encode(password);
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; public class PrintBCryptString { public static void main(String[] args) { PasswordEncoder encoder = new BCryptPasswordEncoder(12); System.out.println(encoder.matches("type here some string", encoder.encode("type here some string"))); System.out.println(encoder.encode("type here some string")); } }
public String encode(CharSequence rawPassword) { return BCRYPT.encode(rawPassword);
<bean id="encoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/> <security:authentication-manager> <security:authentication-provider> <security:password-encoder ref="encoder"/> <security:jdbc-user-service data-source-ref="dataSource" users-by-username-query="select username,password, enabled from users where username=?" authorities-by-username-query="select u.username, ur.authority from users u, user_roles ur where u.username = ur.username and u.username =?"/> </security:authentication-provider> </security:authentication-manager> @Controller @Stateless public class UsersEJB { @PersistenceContext(unitName = "somePU") private EntityManager em; @Transactional public void create(Users users) { PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String hashedPassword = passwordEncoder.encode(users.getPassword()); users.setPassword(hashedPassword); em.persist(users); } }
/** * Creates ZooKeeper client configuration. */ public void createZooKeeeperClientConfig(String connectionUrl, CreateEnsembleOptions options) throws IOException { Dictionary<String, Object> properties = new Hashtable<String, Object>(); if (options.isAutoImportEnabled()) { loadPropertiesFrom(properties, options.getImportPath() + "/fabric/profiles/default.profile/io.fabric8.zookeeper.properties"); } properties.put("zookeeper.url", connectionUrl); properties .put("zookeeper.timeout", System.getProperties().containsKey("zookeeper.timeout") ? System.getProperties().getProperty("zookeeper.timeout") : "30000"); properties.put("fabric.zookeeper.pid", Constants.ZOOKEEPER_CLIENT_PID); properties.put("zookeeper.password", PasswordEncoder.encode(options.getZookeeperPassword())); Configuration config = configAdmin.get().getConfiguration(Constants.ZOOKEEPER_CLIENT_PID, null); config.update(properties); }
/** * Creates ZooKeeper client configuration. */ public boolean createZooKeeeperClientConfig(BundleContext context, String connectionUrl, CreateEnsembleOptions options) throws IOException, InterruptedException { Dictionary<String, Object> properties = new Hashtable<String, Object>(); if (options.isAutoImportEnabled()) { loadPropertiesFrom(properties, options.getImportPath() + "/fabric/profiles/default.profile/io.fabric8.zookeeper.properties"); } properties.put("zookeeper.url", connectionUrl); properties .put("zookeeper.timeout", System.getProperties().containsKey("zookeeper.timeout") ? System.getProperties().getProperty("zookeeper.timeout") : "30000"); properties.put("fabric.zookeeper.pid", Constants.ZOOKEEPER_CLIENT_PID); properties.put("zookeeper.password", PasswordEncoder.encode(options.getZookeeperPassword())); Configuration config = configAdmin.get().getConfiguration(Constants.ZOOKEEPER_CLIENT_PID, null); return OsgiUtils.updateCmConfigurationAndWait(context, config, properties, 30, TimeUnit.SECONDS); }
@Bean public JdbcUserDetailsManager userDetailsManager(DataSource dataSource) { JdbcUserDetailsManager mgr = new JdbcUserDetailsManager(); mgr.setDataSource(dataSource); // (1) return mgr; } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth, JdbcUserDetailsManager userDetailsManager, DataSource dataSource, PasswordEncoder enc) throws Exception { //set user detail service manually auth.userDetailsService(userDetailsManager); JdbcUserDetailsManagerConfigurer<AuthenticationManagerBuilder> conf = new JdbcUserDetailsManagerConfigurer<>(userDetailsManager); //apply the configurer auth.apply(conf); conf.dataSource(dataSource) // (2) .withDefaultSchema().passwordEncoder(enc) .withUser("user").password(enc.encode("password")).roles("USER").and() .withUser("admin").password(enc.encode("password")).roles("USER", "ADMIN"); }
@Service public class UserService { @Autowired private UserRepository userRepository; @Autowired private PasswordEncoder passwordEncoder; public User create(User user) { String hashedPassword = passwordEncoder.encode(user.getPassword()); user.setPassword(hashedPassword); return userRepository.save(user); } ... }
@Override protected Object doExecute() throws Exception { if (Strings.isNotBlank(newPassword)) { setData(getCurator(), ZkPath.AUTHENTICATION_CRYPT_PASSWORD.getPath(), PasswordEncoder.encode(newPassword)); } return null; } }
@Component @RepositoryEventHandler public class AccountRepositoryEventHandler { @Resource private PasswordEncoder passwordEncoder; @HandleBeforeSave public void onResetPassword(Account account) { if (account.isPasswordReset()) { account.setPassword(encodePassword(account.getPassword())); } } private String encodePassword(String plainPassword) { return passwordEncoder.encode(plainPassword); } }
@Test public void testLocalFabricCluster() throws Exception { CommandSupport.executeCommand("fabric:create --force --clean -n"); FabricService fabricService = ServiceLocator.getRequiredService(FabricService.class); Container[] containers = fabricService.getContainers(); Assert.assertNotNull("Containers not null", containers); // Test that a provided default password exists ConfigurationAdmin configurationAdmin = ServiceLocator.getRequiredService(ConfigurationAdmin.class); org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration(io.fabric8.api.Constants.ZOOKEEPER_CLIENT_PID); Dictionary<String, Object> dictionary = configuration.getProperties(); Assert.assertEquals("Expected provided zookeeper password", PasswordEncoder.encode(ADMIN_PASSWORD), dictionary.get("zookeeper.password")); } }
@Test public void testLocalFabricCluster() throws Exception { CommandSupport.executeCommand("fabric:create --clean -n"); FabricService fabricService = ServiceLocator.getRequiredService(FabricService.class); Container[] containers = fabricService.getContainers(); Assert.assertNotNull("Containers not null", containers); //Test that a provided by command line password exists ConfigurationAdmin configurationAdmin = ServiceLocator.getRequiredService(ConfigurationAdmin.class); org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration(io.fabric8.api.Constants.ZOOKEEPER_CLIENT_PID); Dictionary<String, Object> dictionary = configuration.getProperties(); Assert.assertEquals("Expected provided zookeeper password", PasswordEncoder.encode(ADMIN_PASSWORD), dictionary.get("zookeeper.password")); } }
@Component @Profile("dev") public class ApplicationStartup implements ApplicationListener<ContextRefreshedEvent> { @Autowired private UserRepository userRepository; @Autowired PasswordEncoder passwordEncoder; @Override public void onApplicationEvent(final ContextRefreshedEvent event) { User user = new User(); user.setFirstName("Test"); user.setLastName("User"); user.setEmail("email@google.com"); user.setUserName("admin"); user.setPassword(passwordEncoder.encode("PASSWORD")); userRepository.save(user); } }
@Test public void testLocalFabricCluster() throws Exception { Builder<?> builder = CreateEnsembleOptions.builder().agentEnabled(false).clean(true).zookeeperPassword(SYSTEM_PASSWORD).waitForProvision(false); CreateEnsembleOptions options = builder.build(); ZooKeeperClusterBootstrap bootstrap = ServiceLocator.getRequiredService(ZooKeeperClusterBootstrap.class); bootstrap.create(options); FabricService fabricService = ServiceLocator.getRequiredService(FabricService.class); Container[] containers = fabricService.getContainers(); Assert.assertNotNull("Containers not null", containers); //Test that a provided by command line password exists ConfigurationAdmin configurationAdmin = ServiceLocator.getRequiredService(ConfigurationAdmin.class); org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration(io.fabric8.api.Constants.ZOOKEEPER_CLIENT_PID); Dictionary<String, Object> dictionary = configuration.getProperties(); Assert.assertEquals("Expected provided zookeeper password", PasswordEncoder.encode(SYSTEM_PASSWORD), dictionary.get("zookeeper.password")); } }
@Test public void testLocalFabricCluster() throws Exception { Builder<?> builder = CreateEnsembleOptions.builder().agentEnabled(false).clean(true).zookeeperPassword(ADMIN_PASSWORD).waitForProvision(false); CreateEnsembleOptions options = builder.build(); ZooKeeperClusterBootstrap bootstrap = ServiceLocator.getRequiredService(ZooKeeperClusterBootstrap.class); bootstrap.create(options); FabricService fabricService = ServiceLocator.getRequiredService(FabricService.class); Container[] containers = fabricService.getContainers(); Assert.assertNotNull("Containers not null", containers); //Test that a provided default password exists ConfigurationAdmin configurationAdmin = ServiceLocator.getRequiredService(ConfigurationAdmin.class); org.osgi.service.cm.Configuration configuration = configurationAdmin.getConfiguration(io.fabric8.api.Constants.ZOOKEEPER_CLIENT_PID); Dictionary<String, Object> dictionary = configuration.getProperties(); Assert.assertEquals("Expected provided zookeeper password", PasswordEncoder.encode(ADMIN_PASSWORD), dictionary.get("zookeeper.password")); } }
void configureInternal(Map<String, ?> conf) throws Exception { configuration = configurer.configure(conf, this); if (Strings.isNullOrBlank(runtimeId)) { throw new IllegalArgumentException("Runtime id must not be null or empty."); } if (Strings.isNullOrBlank(localResolver)) { localResolver = globalResolver; } String decodedZookeeperPassword = null; Properties userProps = new Properties(); try { userProps.load(new File(confDir , "users.properties")); } catch (IOException e) { LOGGER.warn("Failed to load users from etc/users.properties. No users will be imported.", e); } if (Strings.isNotBlank(zookeeperPassword)) { decodedZookeeperPassword = PasswordEncoder.decode(zookeeperPassword); } else if (userProps.containsKey(DEFAULT_ADMIN_USER)) { String passwordAndRole = userProps.getProperty(DEFAULT_ADMIN_USER).trim(); decodedZookeeperPassword = passwordAndRole.substring(0, passwordAndRole.indexOf(ROLE_DELIMITER)); } else { decodedZookeeperPassword = PasswordEncoder.encode(CreateEnsembleOptions.generatePassword()); } if (userProps.isEmpty()) { userProps.put(DEFAULT_ADMIN_USER, decodedZookeeperPassword+ ROLE_DELIMITER + DEFAULT_ADMIN_ROLE); } options = CreateEnsembleOptions.builder().bindAddress(bindAddress).agentEnabled(agentAutoStart).ensembleStart(ensembleAutoStart).zookeeperPassword(decodedZookeeperPassword) .zooKeeperServerPort(zookeeperServerPort).zooKeeperServerConnectionPort(zookeeperServerConnectionPort).autoImportEnabled(profilesAutoImport) .importPath(profilesAutoImportPath).resolver(localResolver).globalResolver(globalResolver).users(userProps).profiles(profiles).version(version).build(); }