uebung4/aufgabe16
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package uebung;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import jakarta.enterprise.context.SessionScoped;
|
||||
import jakarta.faces.application.FacesMessage;
|
||||
import jakarta.faces.component.UIComponent;
|
||||
import jakarta.faces.context.FacesContext;
|
||||
import jakarta.faces.validator.ValidatorException;
|
||||
import jakarta.inject.Named;
|
||||
|
||||
@Named
|
||||
@SessionScoped
|
||||
public class PasswortManagedBean implements Serializable {
|
||||
private String passwort;
|
||||
|
||||
public PasswortManagedBean() {
|
||||
passwort = "";
|
||||
}
|
||||
|
||||
public String getPasswort() {
|
||||
return passwort;
|
||||
}
|
||||
|
||||
public void setPasswort(String passwort) {
|
||||
this.passwort = passwort;
|
||||
}
|
||||
|
||||
public void validate(FacesContext context, UIComponent ui_component,
|
||||
Object object) throws ValidatorException {
|
||||
|
||||
String eingabe = (String) object;
|
||||
|
||||
// boolean enthaeltGrossbuchstaben = false;
|
||||
// boolean enthaeltKleinbuchstaben = false;
|
||||
// boolean enthaeltZahl = false;
|
||||
// for (int i = 0; i < eingabe.length(); i++) {
|
||||
// if(Character.isUpperCase(eingabe.charAt(i))){enthaeltGrossbuchstaben = true;}
|
||||
// if(Character.isLowerCase(eingabe.charAt(i))){enthaeltKleinbuchstaben = true;}
|
||||
// if(Character.isDigit(eingabe.charAt(i))){enthaeltZahl = true;}
|
||||
// }
|
||||
// if(!(enthaeltGrossbuchstaben && enthaeltKleinbuchstaben && enthaeltZahl)){
|
||||
// throw new ValidatorException(new FacesMessage(
|
||||
// "Fehler beim Validieren des Passworts!"));
|
||||
// }
|
||||
|
||||
// oder alternativ mit einem regulaeren Ausdruck:
|
||||
if (!eingabe.matches(".*[A-Z].*")
|
||||
|| !eingabe.matches(".*[a-z].*")
|
||||
|| !eingabe.matches(".*[0-9].*")) {
|
||||
throw new ValidatorException(new FacesMessage(
|
||||
"Fehler beim Validieren des Passworts!"));
|
||||
}
|
||||
|
||||
// oder alternativ mit einem verbesserten RegEx:
|
||||
// ?= bewegt den Suchzeiger innerhalb des Quellstrings nicht weiter und
|
||||
// sucht ob direkt nach der aktuellen Position das angegebene Pattern folgt.
|
||||
// - ^ positioniert den Zeiger am Anfang
|
||||
// - (?=.*\\d) prüft, ob nach der aktuellen Position (Anfang) beliebige Zeichen
|
||||
// und dann eine Ziffer folgt. Somit wird geprüft, ob im String eine
|
||||
// Ziffer enthalten ist. Der Zeiger bleibt am Anfang stehen.
|
||||
// - Die weiteren (?=.*...) prüfen, ob nach der aktuellen Position (Anfang)
|
||||
// kleine Buchstaben (?=.*[a-z]) oder große Buchstaben (?=.*[A-Z]) folgen.
|
||||
// - Das letzte .* sagt aus, dass danach beliebige weitere Zeichen folgen können.
|
||||
if(!eingabe.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).*$")){
|
||||
throw new ValidatorException(new FacesMessage(
|
||||
"Fehler beim Validieren des Passworts!"));
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/main/webapp/uebung4/aufgabe16/passwortValidierer.xhtml
Normal file
27
src/main/webapp/uebung4/aufgabe16/passwortValidierer.xhtml
Normal file
@@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:ui="jakarta.faces.facelets"
|
||||
xmlns:f="jakarta.faces.core"
|
||||
xmlns:h="jakarta.faces.html">
|
||||
|
||||
<h:head>
|
||||
<title>Passwort Validierer</title>
|
||||
</h:head>
|
||||
<body>
|
||||
<f:view>
|
||||
<h:form>
|
||||
<!-- <h:messages /> -->
|
||||
<h:panelGrid columns="3">
|
||||
<h:outputLabel value="Passwort:" for="eingabe"/>
|
||||
<h:inputSecret id="eingabe"
|
||||
value="#{passwortManagedBean.passwort}"
|
||||
validator="#{passwortManagedBean.validate}"
|
||||
requiredMessage="Es muss ein Wert angegeben werden."
|
||||
required="true" />
|
||||
<h:message for="eingabe"/><br />
|
||||
</h:panelGrid>
|
||||
<h:commandButton value="Senden"/>
|
||||
</h:form>
|
||||
</f:view>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,28 @@
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:ui="jakarta.faces.facelets"
|
||||
xmlns:f="jakarta.faces.core"
|
||||
xmlns:h="jakarta.faces.html">
|
||||
|
||||
<h:head>
|
||||
<title>Passwort Validierer</title>
|
||||
</h:head>
|
||||
<body>
|
||||
<f:view>
|
||||
<h:form>
|
||||
<h:messages />
|
||||
<h:panelGrid columns="3">
|
||||
<h:outputLabel value="Passwort:" for="eingabe"/>
|
||||
<h:inputSecret id="eingabe"
|
||||
value="#{passwortManagedBean.passwort}"
|
||||
required="true"
|
||||
validatorMessage="Das Passwort entspricht nicht den Sicherheitsrichtlinien!"
|
||||
requiredMessage="Es muss ein Wert angegeben werden.">
|
||||
<f:validateRegex pattern="^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$" />
|
||||
</h:inputSecret>
|
||||
</h:panelGrid>
|
||||
<h:commandButton value="Senden"/>
|
||||
</h:form>
|
||||
</f:view>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user