How to do a regular expression to validate JSF password

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, but it does not validate the 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>

The message "Valid password only in ipe @ 1234" is always displayed, even using the ipe @ 1234 password.

This is the xhtml that I am using to validate:

<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 this is 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)
    
asked by anonymous 12.04.2016 / 15:25

1 answer

1

The first problem is that the . (dot) in the subexpressions requires that there always be a character before it is trying to verify. It should be .* to say that there may or may not be any number of characters before, such as (?=.*\d) .

The second problem is that you're demanding and lowercase letters and your password does not match. To require any type of letter, you would have to change (?=.*[a-z])(?=.*[A-Z]) to (?=.*[a-zA-Z]) . Alternatively you would have to test, for example, with Ipe@1234 .

The third problem is that . at the end allows any character in the password, not limited to the types of characters already checked. This means that the password must have the letters, numbers, and special characters that you required, but do not limit the password to them. To solve this, you need to change% w / w%.

The end result would look 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 is true ( [a-zA-Z0-9@$$%]{6,20} ) only for the first true , as expected.

One detail that I'm not 100% sure is if you need to escape on the bar in println . The expression is not in a Java String but in a \d document, so I do not think I would need to duplicate the slash. Take the test with one or two and see how it works.

    
13.04.2016 / 06:26