Java tutorial
package com.mycompany.mavenproject4; import com.stormpath.sdk.account.Account; import com.stormpath.sdk.api.ApiKey; import com.stormpath.sdk.api.ApiKeys; import com.stormpath.sdk.application.ApplicationList; import com.stormpath.sdk.application.Applications; import com.stormpath.sdk.authc.AuthenticationRequest; import com.stormpath.sdk.authc.AuthenticationResult; import com.stormpath.sdk.authc.UsernamePasswordRequest; import com.stormpath.sdk.client.Client; import com.stormpath.sdk.client.ClientBuilder; import com.stormpath.sdk.client.Clients; import com.stormpath.sdk.resource.ResourceException; import com.stormpath.sdk.tenant.Tenant; import java.util.Properties; import org.apache.commons.codec.digest.DigestUtils; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Ferg */ public class NewJFrame extends javax.swing.JFrame { String mySalt = "052werDHG#$^%"; /** * Creates new form NewJFrame */ Client client; public NewJFrame() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { Properties properties = new Properties(); properties.setProperty("apiKey.id", "3OIJT00DLSL8BQ3UCLR5X6C6S"); properties.setProperty("apiKey.secret", "CGMCI0ul9ZR3hDiDIvcocccav3KOKcpHZ2doeqUm1i8"); ClientBuilder builder = Clients.builder(); ApiKey apiKey = ApiKeys.builder().setProperties(properties).build(); client = Clients.builder().setApiKey(apiKey).build(); jTabbedPane1 = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); jPanel3 = new javax.swing.JPanel(); jLabel6 = new javax.swing.JLabel(); fname = new javax.swing.JTextField(); jPanel4 = new javax.swing.JPanel(); jLabel7 = new javax.swing.JLabel(); lname = new javax.swing.JTextField(); jPanel5 = new javax.swing.JPanel(); jLabel8 = new javax.swing.JLabel(); uname = new javax.swing.JTextField(); jPanel6 = new javax.swing.JPanel(); jLabel9 = new javax.swing.JLabel(); pword = new javax.swing.JPasswordField(); jPanel7 = new javax.swing.JPanel(); jLabel10 = new javax.swing.JLabel(); email = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); createresult = new javax.swing.JLabel(); jPanel11 = new javax.swing.JPanel(); jLabel14 = new javax.swing.JLabel(); myhash1 = new javax.swing.JTextField(); jPanel2 = new javax.swing.JPanel(); jPanel8 = new javax.swing.JPanel(); jLabel11 = new javax.swing.JLabel(); email2 = new javax.swing.JTextField(); jPanel9 = new javax.swing.JPanel(); jLabel12 = new javax.swing.JLabel(); pword2 = new javax.swing.JPasswordField(); jButton2 = new javax.swing.JButton(); loginresult = new javax.swing.JLabel(); jPanel10 = new javax.swing.JPanel(); jLabel13 = new javax.swing.JLabel(); myhash2 = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new java.awt.GridLayout(1, 1)); jPanel1.setLayout(new java.awt.GridLayout(12, 3, 5, 5)); jLabel6.setText("First Name"); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout .setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap().addComponent(jLabel6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(fname, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE) .addContainerGap())); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(fname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel6)) .addGap(0, 0, 0))); fname.getAccessibleContext().setAccessibleName("fname"); jPanel1.add(jPanel3); jLabel7.setText("Last Name"); javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4); jPanel4.setLayout(jPanel4Layout); jPanel4Layout .setHorizontalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addContainerGap().addComponent(jLabel7) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(lname, javax.swing.GroupLayout.DEFAULT_SIZE, 225, Short.MAX_VALUE) .addContainerGap())); jPanel4Layout.setVerticalGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel4Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel7)) .addGap(0, 0, 0))); lname.getAccessibleContext().setAccessibleName("lname"); jPanel1.add(jPanel4); jLabel8.setText("User Name"); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout .setHorizontalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap().addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(uname, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE) .addContainerGap())); jPanel5Layout.setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(uname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel8)) .addGap(0, 0, 0))); uname.getAccessibleContext().setAccessibleName("uname"); jPanel1.add(jPanel5); jLabel9.setText("Password"); javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); jPanel6.setLayout(jPanel6Layout); jPanel6Layout .setHorizontalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addContainerGap().addComponent(jLabel9) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(pword, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE) .addContainerGap())); jPanel6Layout.setVerticalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel9).addComponent(pword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(3, 3, 3))); jPanel1.add(jPanel6); jLabel10.setText("Email"); javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7); jPanel7.setLayout(jPanel7Layout); jPanel7Layout .setHorizontalGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel7Layout.createSequentialGroup().addContainerGap().addComponent(jLabel10) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(email, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE) .addContainerGap())); jPanel7Layout.setVerticalGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel7Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel10)) .addGap(0, 0, 0))); email.getAccessibleContext().setAccessibleName("email"); jPanel1.add(jPanel7); jButton1.setText("Submit"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jPanel1.add(jButton1); jPanel1.add(createresult); jLabel14.setText("SHA256 Hash"); javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11); jPanel11.setLayout(jPanel11Layout); jPanel11Layout .setHorizontalGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel11Layout.createSequentialGroup().addContainerGap().addComponent(jLabel14) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(myhash1, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE) .addContainerGap())); jPanel11Layout.setVerticalGroup(jPanel11Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel11Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(myhash1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel14)) .addGap(0, 0, 0))); jPanel1.add(jPanel11); jTabbedPane1.addTab("Create Account", jPanel1); jPanel2.setLayout(new java.awt.GridLayout(12, 2)); jLabel11.setText("Email"); javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8); jPanel8.setLayout(jPanel8Layout); jPanel8Layout .setHorizontalGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel8Layout.createSequentialGroup().addContainerGap() .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(email2, javax.swing.GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE) .addContainerGap())); jPanel8Layout.setVerticalGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel8Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(email2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel11)) .addGap(0, 0, 0))); jPanel2.add(jPanel8); jLabel12.setText("Password"); javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9); jPanel9.setLayout(jPanel9Layout); jPanel9Layout .setHorizontalGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel9Layout.createSequentialGroup().addContainerGap().addComponent(jLabel12) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(pword2, javax.swing.GroupLayout.DEFAULT_SIZE, 229, Short.MAX_VALUE) .addContainerGap())); jPanel9Layout.setVerticalGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel9Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel12).addComponent(pword2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(3, 3, 3))); jPanel2.add(jPanel9); jButton2.setText("Log In"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jPanel2.add(jButton2); jPanel2.add(loginresult); jLabel13.setText("SHA256 Hash"); javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10); jPanel10.setLayout(jPanel10Layout); jPanel10Layout .setHorizontalGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel10Layout.createSequentialGroup().addContainerGap().addComponent(jLabel13) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(myhash2, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE) .addContainerGap())); jPanel10Layout.setVerticalGroup(jPanel10Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel10Layout.createSequentialGroup().addGap(5, 5, 5) .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(myhash2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel13)) .addGap(0, 0, 0))); jPanel2.add(jPanel10); jTabbedPane1.addTab("Log in", jPanel2); getContentPane().add(jTabbedPane1); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed myhash1.setText("myHash: " + DigestUtils.sha256Hex((email.getText() + mySalt + pword.getText()))); Tenant tenant = client.getCurrentTenant(); ApplicationList applications = tenant .getApplications(Applications.where(Applications.name().eqIgnoreCase("sQuire"))); com.stormpath.sdk.application.Application app = applications.iterator().next(); Account account = client.instantiate(Account.class); account.setGivenName(fname.getText()).setSurname(lname.getText()).setUsername(uname.getText()) .setEmail(email.getText()).setPassword(pword.getText()); // .getCustomData().put("favoriteColor", "white"); try { app.createAccount(account); createresult.setText("Account '" + uname.getText() + "' Created"); } catch (Exception e) { createresult.setText(e.getMessage()); } try { //MessageDigest digest = MessageDigest.getInstance("SHA-256"); //byte[] hash = digest.digest((email2.getText() + pword2.getText()).getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed myhash2.setText("myHash: " + DigestUtils.sha256Hex((email2.getText() + mySalt + pword2.getText()))); Tenant tenant = client.getCurrentTenant(); ApplicationList applications = tenant .getApplications(Applications.where(Applications.name().eqIgnoreCase("sQuire"))); com.stormpath.sdk.application.Application app = applications.iterator().next(); AuthenticationRequest request = new UsernamePasswordRequest(email2.getText(), pword2.getText()); //Now let's authenticate the account with the application: try { AuthenticationResult result = app.authenticateAccount(request); Account account = result.getAccount(); loginresult.setText("Account Logged in"); } catch (ResourceException ex) { loginresult.setText(ex.getMessage()); } }//GEN-LAST:event_jButton2ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel createresult; private javax.swing.JTextField email; private javax.swing.JTextField email2; private javax.swing.JTextField fname; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel11; private javax.swing.JLabel jLabel12; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel10; private javax.swing.JPanel jPanel11; private javax.swing.JPanel jPanel2; private javax.swing.JPanel jPanel3; private javax.swing.JPanel jPanel4; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JPanel jPanel7; private javax.swing.JPanel jPanel8; private javax.swing.JPanel jPanel9; private javax.swing.JTabbedPane jTabbedPane1; private javax.swing.JTextField lname; private javax.swing.JLabel loginresult; private javax.swing.JTextField myhash1; private javax.swing.JTextField myhash2; private javax.swing.JPasswordField pword; private javax.swing.JPasswordField pword2; private javax.swing.JTextField uname; // End of variables declaration//GEN-END:variables }