|
| 1 | +package test; |
| 2 | + |
| 3 | +import javafx.event.ActionEvent; |
| 4 | +import javafx.fxml.FXML; |
| 5 | +import javafx.scene.control.Label; |
| 6 | +import javafx.scene.control.PasswordField; |
| 7 | +import javafx.scene.control.TextField; |
| 8 | +import javafx.scene.paint.Color; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.security.access.AccessDeniedException; |
| 11 | +import org.springframework.security.authentication.AuthenticationManager; |
| 12 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 13 | +import org.springframework.security.core.Authentication; |
| 14 | +import org.springframework.security.core.AuthenticationException; |
| 15 | +import org.springframework.security.core.context.SecurityContextHolder; |
| 16 | +import org.springframework.stereotype.Component; |
| 17 | + |
| 18 | +@Component |
| 19 | +public class HelloController extends FXMLController { |
| 20 | + |
| 21 | + @Autowired |
| 22 | + public AuthenticationManager authManager; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + public OperationService operationService; |
| 26 | + |
| 27 | + @FXML |
| 28 | + public TextField username; |
| 29 | + @FXML |
| 30 | + public PasswordField password; |
| 31 | + @FXML |
| 32 | + public TextField txtDiv; |
| 33 | + @FXML |
| 34 | + public TextField txtMult; |
| 35 | + @FXML |
| 36 | + public TextField txtSuma; |
| 37 | + @FXML |
| 38 | + public Label lblInfo; |
| 39 | + |
| 40 | + @FXML |
| 41 | + public void onAuthClick(ActionEvent event) { |
| 42 | + try { |
| 43 | + final String usuario = username.getText().trim(); |
| 44 | + final String contrasena = password.getText().trim(); |
| 45 | + |
| 46 | + Authentication request = new UsernamePasswordAuthenticationToken(usuario, contrasena); |
| 47 | + Authentication result = authManager.authenticate(request); |
| 48 | + SecurityContextHolder.getContext().setAuthentication(result); |
| 49 | + |
| 50 | + String name = result.getName(); |
| 51 | + String roles = result.getAuthorities().toString(); |
| 52 | + |
| 53 | + lblInfo.setText("Bienvenido: " + name + " " + roles); |
| 54 | + lblInfo.setTextFill(Color.GREEN); |
| 55 | + |
| 56 | + username.clear(); |
| 57 | + password.clear(); |
| 58 | + |
| 59 | + } catch (AuthenticationException e) { |
| 60 | + lblInfo.setText("usuario (y/o) contrasena incorrecto."); |
| 61 | + lblInfo.setTextFill(Color.RED); |
| 62 | + |
| 63 | + System.out.println("Authentication failed: " + e.getMessage()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @FXML |
| 68 | + public void onSuma(ActionEvent actionEvent) { |
| 69 | + try { |
| 70 | + clear(); |
| 71 | + double r = operationService.sumar(2, 2); |
| 72 | + txtSuma.setText("Sumar: 2 + 2 = " + r); |
| 73 | + } catch (AccessDeniedException e) { |
| 74 | + txtSuma.setText(e.getMessage()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @FXML |
| 79 | + public void onMult(ActionEvent actionEvent) { |
| 80 | + try { |
| 81 | + clear(); |
| 82 | + double r = operationService.multiplicar(4, 6); |
| 83 | + txtMult.setText("Multiplicar: 4 * 6 = " + r); |
| 84 | + } catch (AccessDeniedException e) { |
| 85 | + txtMult.setText(e.getMessage()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @FXML |
| 90 | + public void onDiv(ActionEvent actionEvent) { |
| 91 | + clear(); |
| 92 | + double r = operationService.dividir(8, 2); |
| 93 | + txtDiv.setText("Dividir: 8 / 2 = " + r); |
| 94 | + } |
| 95 | + |
| 96 | + @FXML |
| 97 | + public void onLogout(ActionEvent actionEvent) { |
| 98 | + MainApp.logout(); |
| 99 | + |
| 100 | + username.clear(); |
| 101 | + password.clear(); |
| 102 | + lblInfo.setText(""); |
| 103 | + } |
| 104 | + |
| 105 | + private void clear() { |
| 106 | + txtSuma.clear(); |
| 107 | + txtMult.clear(); |
| 108 | + txtDiv.clear(); |
| 109 | + } |
| 110 | +} |
0 commit comments