創建一個Bean來驗證我的登錄表單在過去的日子裏,我一直在嘗試使用spring安全性創建一個自定義登錄頁面,但是我沒有找到一個工作示例也沒有自己想出如何使用spring來驗證表單,並相信我,我嘗試了eveything,我可能在google上找到的每個相關示例。如何使用Spring Security 3.1.3和JSF
表單加載正常,everythins正常,我需要的是讓Spring Security在單擊「Login」按鈕時對數據庫驗證憑據。
讓我把它分解成幾部分來解釋。
所以,我有一個登錄表單:
<h:form>
<p:panelGrid columns="2">
<p:outputLabel for="j_username" value="Usuário:"/>
<p:inputText id="j_username"
title="Preencha com o seu usuário (login)."
required="true"
requiredMessage="O campo usuário é obrigatório."
value="#{loginBean.usuario}"/>
<p:outputLabel for="j_password" value="Senha:"/>
<p:password id="j_password"
title="Preencha com a sua senha."
required="true"
requiredMessage="O campo senha é obrigatório."
value="#{loginBean.senha}"/>
<p:inputText type="hidden"/>
<p:panelGrid columns="2" styleClass="customPanelgridTable">
<p:outputLabel for="_spring_security_remember_me" value="Lembrar senha? "/>
<p:selectBooleanCheckbox id="_spring_security_remember_me"
value="#{loginBean.lembrar_me}"/>
</p:panelGrid>
<f:facet name="footer">
<p:commandButton value="Entrar"
actionListener="#{loginBean.doLogin}"/>
</f:facet>
</p:panelGrid>
</h:form>
我需要方法 「doLogin」 驗證使用Spring Security的憑據。
我LoginBean:
@Named
@SessionScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
private String usuario, senha;
private boolean lembrar_me = false;
public String getUsuario() {
return usuario;
}
public void setUsuario(String usuario) {
this.usuario = usuario;
}
public String getSenha() {
return senha;
}
public void setSenha(String senha) {
this.senha = senha;
}
public boolean isLembrar_me() {
return lembrar_me;
}
public void setLembrar_me(boolean lembrar_me) {
this.lembrar_me = lembrar_me;
}
public void doLogin() {
//Spring validation...
}
}
我怎麼能這樣做?
的applicationContext.xml
<http security="none" pattern="/javax.faces.resource/**" />
<http security="none" pattern="/static/**"/>
<http auto-config="true" use-expressions="true"
access-denied-page="/public/login.xhtml">
<intercept-url pattern="/public/**" access="permitAll"/>
<intercept-url pattern="/secure/**" access="hasRole('ROLE_USER')"/>
<intercept-url pattern="/login.xhtml" access="permitAll"/>
<intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
<form-login login-page="/public/login.xhtml"
authentication-failure-url="/public/login.xhtml?erro=true"
default-target-url="/secure/secure.xhtml"/>
</http>
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<beans:property name="url" value="jdbc:mysql://localhost:3306/gde" />
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="username" value="root" />
<beans:property name="password" value="" />
</beans:bean>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="teste" password="teste" authorities="ROLE_USER"/>
</user-service>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="SELECT USUARIO as username, ISATIVO as enabled FROM usuario WHERE USUARIO=?"
authorities-by-username-query="SELECT USUARIO as username, AUTORIZACOES as authority FROM usuario_tipo_usuario WHERE USUARIO=?"
/>
</authentication-provider>
</authentication-manager>
任何幫助非常感謝,我堅持這個好幾天!
完全重複的[如何驗證一個登錄內部的bean使用彈簧證券](http://stackoverflow.com/questions/13481299/how-to-validate-a-登錄 - 一個bean-using-spring-security/13497190#13497190)和可能的[spring-custom-jsf-login-page-always-bad-credentials]的重複(http://stackoverflow.com/questions/ 11742283/spring-custom-jsf-login-page-always-bad-credentials/11746589#11746589) – Ravi