How to make a regular expression to validate JSF password

Asked

Viewed 1,076 times

0

I need a regular expression to validate password as below:

The password must be in the default: Ipe@1234 (letters, special characters and numbers)

I’m using this below, only it does not validate, error:

<p:password id="passwordInput" class="password" size="35"
maxlength="20" value="#{funcionarioBean.novaSenha}"
required="true" validatorMessage="Senha válida somente no padrão ipe@1234.">
<f:validateRegex pattern="((?=.\\d)(?=.[a-z])(?=.[A-Z])(?=.[@#$%]).{6,20})"/>
</p:password>

Always appears the message "Password valid only in standard Ipe@1234", even using the password Ipe@1234.


This is the xhtml I’m using to do the validation:

<ui:define name="conteudo">
        <p:growl id="frmLoginGeral" />
        <div id="redefinir">
            <h:form>
                <input type="hidden" id="idFunc" name="idFunc"
                    value="#{param['id']}" />
                <div class="senha">
                    <div class="row2">
                        <p:outputLabel class="nova_senha" value="Crie uma nova senha" />
                        <br /> <br />
                    </div>
                    <div class="row_center">
                        <div class="row3">
                            <p:outputLabel class="label_senha" value="Crie sua nova senha: " />
                            <br />
                            <p:password id="passwordInput" class="password" size="35"
                                maxlength="20" value="#{funcionarioBean.novaSenha}"
                                required="true"
                                validatorMessage="Senha válida somente no padrão ipe@1234.">
                                <f:validateRegex
                                    pattern="((?=.*\d)(?=.*[a-zA-Z])(?=.*[@#$%])[a-zA-Z0-9@$$%]{8,20})"/>
                            </p:password>
                            * <br /> <br />
                            <p:outputLabel class="label_senha"
                                value="Confirme sua nova senha: " />
                            <br />
                            <p:password id="passwordInput2" class="password" size="35"
                                maxlength="20" value="#{funcionarioBean.novaSenhaConfirma}" />
                            * <br /> <br />
                        </div>
                        <h:commandButton class="botao" value="Confirmar"
                            actionListener="#{funcionarioBean.gerarSenha}"
                            update=":frmLoginGeral" />
                    </div>
                </div>
            </h:form>
        </div>
    </ui:define>

And that’s the error message:

 WARNING: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
    javax.el.ELException: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
    at org.apache.el.parser.AstValue.invoke(AstValue.java:260)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:149)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:813)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider]
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:201)
    at org.hibernate.internal.AbstractSessionImpl.getJdbcConnectionAccess(AbstractSessionImpl.java:341)
    at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.<init>(JdbcCoordinatorImpl.java:114)
    at org.hibernate.engine.transaction.internal.TransactionCoordinatorImpl.<init>(TransactionCoordinatorImpl.java:89)
    at org.hibernate.internal.SessionImpl.<init>(SessionImpl.java:258)
    at org.hibernate.internal.SessionFactoryImpl$SessionBuilderImpl.openSession(SessionFactoryImpl.java:1589)
    at org.hibernate.internal.SessionFactoryImpl.openSession(SessionFactoryImpl.java:999)

1 answer

1

The first problem is that the . (point) in the sub-expressions requires that there is always a character before what one is trying to verify. It should be .* to say that there may or may not be any number of characters before, as in (?=.*\\d).

The second problem is that you are requiring capital letters and lowercase and your password does not match. To require any font, you would have to exchange (?=.*[a-z])(?=.*[A-Z]) for (?=.*[a-zA-Z]). Alternatively you would have to test, for example, with Ipe@1234.

The third problem is that the . at the end allows any character in the password, not limited to the types of characters already verified. This means that the password needs to have the letters, numbers and special characters you required, but does not limit the password to these. To solve this, you need to exchange for [a-zA-Z0-9@$$%]{6,20}.

The end result would be something like:

((?=.*\d)(?=.*[a-zA-Z])(?=.*[@#$%])[a-zA-Z0-9@$$%]{6,20})

Testing with the following code:

String regex = "((?=.*\\d)(?=.*[a-zA-Z])(?=.*[@#$%])[a-zA-Z0-9@$$%]{6,20})";
System.out.println("ipe@1234".matches(regex));
System.out.println("ipe@1é234".matches(regex));
System.out.println("i@1".matches(regex));
System.out.println("ipe1234".matches(regex));
System.out.println("@1234".matches(regex));
System.out.println("ipe@".matches(regex));

The result obtained is true (true) only for the first println, as expected.

One detail I’m not 100% sure about is whether you need to escape on the bar in \\d. The expression is not in a Java String but in a document xhtml, then I guess I wouldn’t need to duplicate the bar. Test with one or two and see how it works.

  • Well, I did the test with d e aparace error message "Password valid only in standard Ipe@1234", when put d, gave error 500... follows below:

  • This is the xhtml I’m using to do the edited validation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.