2012-10-04 19 views
10

को संभालने वाले अपवादों को प्रमाणित करता है मेरे पास स्प्रिंग सुरक्षा 3.0.x का उपयोग करके एक ऐप है। वहाँ मैं एक कस्टम AuthenticationProvider है:स्प्रिंग सुरक्षा

public class AppAuthenticationProvider implements AuthenticationProvider { 
    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
     ... 
     if (!check1()) throw new UsernameNotFoundException(); 
     if (!check2()) throw new DisabledException(); 
     ... 
    } 

अब मैं सिर्फ में प्रमाणीकरण-विफलता-यूआरएल है के लिए मैं cutom प्रतिक्रिया कोड प्रत्येक अपवाद पर DisabledException आदि के लिए UsernameNotFoundException, 403 के लिए उदाहरण के लिए 404, संदेश भेजना चाहते हैं मेरे वसंत सुरक्षा विन्यास इसलिए मैं प्रमाणीकरण() में प्रत्येक अपवाद पर इसे पुनर्निर्देशित करता हूं।

उत्तर

19

प्रमाणीकरण विफल हैंडलर:

public class CustomAuthenticationFailureHandler extends SimpleUrlAuthenticationFailureHandler { 

@Override 
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { 
    super.onAuthenticationFailure(request, response, exception); 
    if(exception.getClass().isAssignableFrom(UsernameNotFoundException.class)) { 
    showMessage("BAD_CREDENTIAL"); 
    } else if (exception.getClass().isAssignableFrom(DisabledException.class)) { 
    showMessage("USER_DISABLED"); 
    } 
} 

विन्यास:

<bean id="customAuthenticationFailureHandler" 
     class="com.apackage.CustomAuthenticationFailureHandler"> 
    <property name="defaultFailureUrl" value="/index.jsp"/> 
</bean> 
<security:http auto-config="true"> 
    <security:form-login default-target-url="/welcome.jsp" authentication-failure-handler-ref="customAuthenticationFailureHandler" /> 
</security:http> 
+0

मुझे SimpleUrlAuthenticationFailureHandler क्लास में एक शो मैसेज() विधि नहीं मिली (मैं स्प्रिंग सुरक्षा संस्करण 3.1.x का उपयोग कर रहा हूं)। क्या आप जानते हैं कि आप किस संस्करण का उपयोग कर रहे थे? –

+0

क्षमा करें, शो मैसेज विधि मेरे कार्यान्वयन के लिए विशिष्ट है और वसंत से नहीं आती है। मूल कोड दिखाने के लिए बहुत अधिक था। – baraber

+0

@baraber नियंत्रक विधि में अपवाद प्रकार को कैसे पास करें? – gstackoverflow

14

यह आमतौर पर एक बुरा विचार है कि प्रमाणीकरण विफल क्यों हुआ क्योंकि यह उपयोगी जानकारी के साथ हमलावर प्रदान कर सकता है। उदाहरण के लिए, यह उन्हें वैध खाता नामों की जांच करने की अनुमति दे सकता है।

यदि आपको चीजों को कस्टमाइज़ करने की आवश्यकता है, तो authentication-failure-url का उपयोग करने के बजाय, आप कस्टम AuthenticationFailureHandler बीन इंजेक्ट करने के लिए authentication-failure-handler-ref का उपयोग कर सकते हैं जहां आप अपवाद के आधार पर विभिन्न व्यवहार को कार्यान्वित कर सकते हैं।

+0

कैसे विधि नियंत्रक को AuthenticationFailureHandler से जानकारी पारित करने के लिए शामिल हैं? – gstackoverflow

5

jsp पृष्ठ में अपने अनुकूलित प्रमाणन के लिए नीचे दिए गए टैग का प्रयोग करें।

<c:if test="${sessionScope[\"SPRING_SECURITY_LAST_EXCEPTION\"].message eq 'Bad credentials'}"> 
Username/Password entered is incorrect. 
</c:if> 
<c:if test="${sessionScope[\"SPRING_SECURITY_LAST_EXCEPTION\"].message eq 'User is disabled'}"> 
Your account is disabled, please contact administrator. 
</c:if> 
<c:if test="${fn:containsIgnoreCase(sessionScope[\"SPRING_SECURITY_LAST_EXCEPTION\"].message,'A communications error has been detected')}"> 
Database connection is down, try after sometime. 
</c:if> 

भी ठीक से काम कर

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%> 

के लिए नीचे दिया गया टैग पुस्तकालय ...

संबंधित मुद्दे