2010-10-02 4 views
4

Salut J'utilise maintenant la sécurité de printemps. Ça fonctionne bien. Mais si la connexion a échoué, aucun message d'erreur ne s'affiche. Je me demande comment puis-je afficher un message d'erreur?Comment afficher un message d'erreur dans ma page JSP en utilisant la sécurité de printemps 2.0

J'ai configuré le ResourceBundleMessageSource dans mon applicationContext.xml

<!-- Spring security error message config --> 
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <property name="basenames"> 
      <list> 
        <value>messages</value> 
      </list> 
     </property> 
    </bean> 

Et ma sécurité config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:David="http://www.springframework.org/schema/security" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
          http://www.springframework.org/schema/security 
          http://www.springframework.org/schema/security/spring-security-2.0.xsd"> 

    <David:http auto-config="true" access-denied-page="/accessDenied.html"> 

     <!-- Don`t set any role restriction on login.jsp --> 
     <David:intercept-url pattern="/login.jsp" 
      access="IS_AUTHENTICATED_ANONYMOUSLY" /> 

     <!-- Restrict access to All other pages --> 
     <David:intercept-url pattern="/admin.jsp" 
      access="ROLE_ADMIN" /> 

     <!-- Set the login page and what to do if login fails --> 
     <David:form-login login-page="/login.jsp" 
      default-target-url="/"/> 
     <David:logout logout-success-url="/" /> 
    </David:http> 

    <!-- Specify login examnination strategy --> 
    <David:authentication-provider> 
     <David:password-encoder hash="md5" /> 
     <David:jdbc-user-service 
      data-source-ref="dataSource" 
      users-by-username-query="select username, password, status as enabled from user where username=?" 
      authorities-by-username-query="select u.username,r.name as authority 
              from user u 
              join user_role ur 
               on u.id=ur.user_id 
              join role r 
               on r.id=ur.role_id 
              where u.username=?" /> 
    </David:authentication-provider> 
</beans> 

Ma page jsp:

<%@ page contentType="text/html;charset=utf-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<%@ page 
    import="org.springframework.security.ui.AbstractProcessingFilter"%> 
<%@ page 
    import="org.springframework.security.ui.webapp.AuthenticationProcessingFilter"%> 
<%@ page import="org.springframework.security.AuthenticationException"%> 

<form id="myform" class="cmxform" method="post" 
          action="${pageContext.request.contextPath}/j_spring_security_check"> 
          <fieldset> 
           <legend> 
            Please input correct username and password to login 
           </legend> 
           <p> 
            <label for="user"> 


             Username: 
            </label> 
            <input id="user" name="j_username" /> 
           </p> 
           <p> 
            <label for="pass"> 


             Password: 
            </label> 
            <input type="password" name="j_password" id="password" /> 
           </p> 
           <p> 
            <input type="submit" class="submit" value="Login" /> 
           </p> 
          </fieldset> 
         </form> 

Toutes les suggestions? Toute aide serait appréciée.

Répondre

3

Où est le jsp pour afficher réellement l'erreur? Quelque chose comme

<c:if test="${not empty param.error}"> 
    <!-- Display error message --> 
</c:if> 

Si vous souhaitez personnaliser ce message, vous devez également regarder this

+0

Nous vous remercions de votre réponse. Cela aide, mais qu'en est-il si vous utilisez Struts2? Quel est le format d'utilisation de struts2? –

+0

Le savez-vous? –

11
<c:if test="${not empty param.login_error}"> 
    <div class="error"> 
     Your login attempt was not successful, try again.<br /> 
     Reason: #{sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message} 
    </div> 
</c:if> 
2

Peut-être que vous pouvez essayer ... mettre

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

pour la balise c sur jsp page, puis mettre quelque chose comme ci-dessous pour l'erreur msg afficher

<c:when test="${param.error == 1}"> 
<h3 style="font-size:20; color:#FF1C19;">Wrong id or password!</h3> 
</c:when> 

"param.error == 1" peut obtenir la valeur de retour de Spring Security (sécurité config.xml) comme

<beans:bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler"> 
    <beans:property name="exceptionMappings"> 
     <beans:props>  
     <beans:prop key="org.springframework.security.core.userdetails.UsernameNotFoundException">/login.action?error=1</beans:prop> 
     </beans:props> 
    </beans:property> 
</beans:bean> 
3

Cela fonctionne

<c:if test="${param.error != null}"> 
    Error 
</c:if> 
1

Cela a fonctionné pour moi:

<c:if test="${not empty sessionScope.SPRING_SECURITY_LAST_EXCEPTION}"> 
<div class="error"> 
    Your login attempt was not successful, try again.<br /> 
    Reason: ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message} 
</div> 
</c:if> 
0

J'utilise ressort de sécurité v 4.0.3.RELEASE

Usin g le not empty n'a pas fonctionné pour moi. Cependant, la vérification de null a fonctionné pour moi.

<c:if test="${ param.error ne null}"> 
    Display error message 
</c:if> 
Questions connexes