在过去的几天里,我一直在疯狂地尝试使用 Spring Security 创建一个自定义登录页面,但是我没有找到一个可行的示例,也没有自己弄清楚如何使用 Spring 验证表单,相信我,我尝试了一切,我可能在谷歌上找到每个相关的例子。
表单加载正常,一切就绪,当我单击“登录”按钮时,我只需要让 Spring Security 对数据库的凭据进行身份验证。
让我把它分成几部分来解释。
所以,我有一个登录表单:
<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 验证凭据。
我的登录豆:
@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...
}
}
我怎样才能做到这一点?
应用程序上下文.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>
非常感谢任何帮助,我坚持了好几天!!!