Enregistrement d’un utilisateur
Posted on: 30 juin 2020 /
Categories: Java
Cet article est la suite de l’article Client Java JWT
Dans cet article nous allons voir comment modifier notre précédente application Java et y ajouter l’enregistrement d’un utilisateur.
Nous allons premièrement ajouter un objet RegisterJDialog qui sera notre fenêtre d’ajout de notre utilisateur. Voici le contenu ne notre objet :
public class RegisterJDialog extends JDialog implements ActionListener, PropertyChangeListener, WindowListener{
private static final long serialVersionUID = 1L;
private JLabel nameLabel;
private JLabel emailLabel;
private JLabel passwordLabel;
private JTextField nameField;
private JTextField emailField;
private JPasswordField passwordField ;
private JButton boutonRegister;
private ConnectionRest connectionRest;
private PropertyChangeSupport ptChSupport = new PropertyChangeSupport(this);
public RegisterJDialog(JFrame mainFrame){
super(mainFrame);
initialize();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(boutonRegister)) {
try {
JSONObject jsonAuthentification = new JSONObject();
jsonAuthentification.put("name",nameField.getText());
jsonAuthentification.put("email",emailField.getText());
jsonAuthentification.put("password",new String(passwordField.getPassword()));
jsonAuthentification.put("licence","MNA-1A-5U-1");
connectionRest = new ConnectionRest("CREATE_USER");
connectionRest.setObj(jsonAuthentification);
connectionRest.addPropertyChangeListener(this);
connectionRest.start();
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if(evt.getSource().equals(connectionRest)){
if(((String)evt.getNewValue()).charAt(0)=='{'){
System.err.println("Erreur : "+evt.getNewValue());
}else {
ptChSupport.firePropertyChange("AuthorizationOk", null, evt.getNewValue());
this.setVisible(false);
}
}
}
private void initialize() {
try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
this.setSize(240, 190);
this.setLocationRelativeTo(null);
this.addWindowListener(this);
this.setTitle("Enregistrement");
nameLabel = new JLabel("Nom : ");
emailLabel = new JLabel("E-mail : ");
passwordLabel = new JLabel("Password : ");
nameField = new JTextField();
emailField = new JTextField();
passwordField = new JPasswordField();
boutonRegister = new JButton("S'enregistrer");
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0;
getContentPane().add(nameLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
getContentPane().add(nameField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0;
getContentPane().add(emailLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.weightx = 1;
getContentPane().add(emailField, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.NONE;
gbc.weightx = 0;
getContentPane().add(passwordLabel, gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
getContentPane().add(passwordField, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.weightx = 2;
gbc.gridwidth = 2;
getContentPane().add(boutonRegister, gbc);
boutonRegister.addActionListener(this);
} catch (Exception exc) {
exc.printStackTrace();
}
}
public void windowClosing(WindowEvent e) {
ptChSupport.firePropertyChange("Login", null,null);
this.setVisible(false);
}
public void addPropertyChangeListener(PropertyChangeListener l){ptChSupport.addPropertyChangeListener(l);}
public void removePropertyChangeListener(PropertyChangeListener l){ptChSupport.removePropertyChangeListener(l);}
public void windowDeactivated(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
Cette fenêtre sera accessible depuis notre fenêtre principale RestJFrame on y ajoutera dans les fonctions propertyChange() et initialize() les lignes suivantes :
public class RestJFrame extends JFrame implements ActionListener, PropertyChangeListener{
...
private RegisterJDialog registerJDialog = null;
...
public void propertyChange(PropertyChangeEvent evt) {
if(evt.getSource().equals(loginJDialog)&&evt.getPropertyName().equals("AuthorizationOk")){
productTableModel.setToken((String)evt.getNewValue());
refresh();
}else if(evt.getSource().equals(loginJDialog)&&evt.getPropertyName().equals("Register")){
registerJDialog.setVisible(true);
}else if(evt.getSource().equals(registerJDialog)&&evt.getPropertyName().equals("AuthorizationOk")){
productTableModel.setToken((String)evt.getNewValue());
refresh();
}else if(evt.getSource().equals(registerJDialog)&&evt.getPropertyName().equals("Login")){
loginJDialog.setVisible(true);
}
}
...
private void initialize() {
...
registerJDialog = new RegisterJDialog(this);
registerJDialog.addPropertyChangeListener(this);
...
}
...
}
public class LoginJDialog extends JDialog implements ActionListener, PropertyChangeListener{
...
private JButton boutonRegister;
...
public void actionPerformed(ActionEvent e) {
...
else if (e.getSource().equals(boutonRegister)) {
ptChSupport.firePropertyChange("Register", null, null);
this.setVisible(false);
}
}
...
private void initialize() {
...
boutonRegister = new JButton("S'enregistrer");
...
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weightx = 2;
gbc.gridwidth = 2;
getContentPane().add(boutonRegister, gbc);
...
}
...
}
Pour finir, nous devrons modifier la fonction Get de ConnectionRest pour y ajouter l’appel de la page register.php comme ce-ci :
public class ConnectionRest extends Thread{
...
public String get(String pMethode) throws IOException, JSONException {
...
if (!pMethode.equals("POST") && (jsonObj != null)&&!pMethode.equals("CREATE_USER")) {
...
}
...
if (pMethode.equals("CREATE_USER")) {
pMethode = "POST";
url = URL + "register.php";
}
try {
...
} finally {
...
}
...
}
...
}
Sources :
https://eric.munier.me/jwt/
