आप अपनी खुद की SuccessHandler लागू करने के लिए और UsernamePasswordAuthenticationFilter को इसकी सुई की जरूरत है:
public class MyAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
public MyAuthenticationSuccessHandler() {
super();
setRedirectStrategy(new NoRedirectStrategy());
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {
super.onAuthenticationSuccess(request, response, authentication);
response.write("my JSON response");
}
protected class NoRedirectStrategy implements RedirectStrategy {
@Override
public void sendRedirect(HttpServletRequest request,
HttpServletResponse response, String url) throws IOException {
// no redirect
}
}
}
नोट NoRedirectStrategy: डिफ़ॉल्ट UsernamePassword फिल्टर द्वारा होगा एक सफल लॉगऑन के बाद रीडायरेक्ट करें जिसे आप शायद इस मामले में नहीं चाहते हैं। यह भी ध्यान रखें कि उपरोक्त कार्यान्वयन में फ़िल्टर श्रृंखला प्रमाणीकरण सफलता पर समाप्त हो जाती है - आपको यह सुनिश्चित करना होगा कि आपका फ़िल्टर उचित प्रतिक्रिया प्रदान करता है, यह आपके लिए यह करने के लिए अंतर्निहित सर्वलेट पर भरोसा न करें।
वैकल्पिक रूप से आप रीडायरेक्ट को जगह पर छोड़ सकते हैं और क्लाइंट को दूसरे यूआरएल पर भेज सकते हैं जो बदले में जेएसओएन प्रतिक्रिया को प्रस्तुत करता है।
नमूना सुरक्षा संदर्भ:
<!-- Security -->
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
<sec:filter-chain-map path-type="ant">
<sec:filter-chain pattern="/rest/login" filters="wsFilter"/>
</sec>
</bean>
<bean id="wsFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="myAuthenticationManager"/>
<property name="authenticationSuccessHandler" ref="myAuthSuccessHandler"/>
<property name="passwordParameter" value="pass"></property>
<property name="usernameParameter" value="user"></property>
<property name="postOnly" value="false"></property>
</bean>
<bean id="myAuthSuccessHandler" class="com.my.MyAuthenticationSuccessHandler"/>
<sec:authentication-manager id="myAuthenticationManager" >
<sec:authentication-provider user-service-ref="UserDetailsImpl">
<sec:password-encoder hash="md5">
</sec:password-encoder>
</sec:authentication-provider>
</sec:authentication-manager>
वहाँ कोई कस्टम फ़िल्टर को लागू करने के बजाय सिर्फ एक सर्वलेट कि json आप की जरूरत का उत्पादन करने के लिए अग्रेषित करने के लिए वसंत सुरक्षा विन्यास का उपयोग के लिए एक कारण है? – ElderMael
@mael: कारण डिफ़ॉल्ट j_username/j_password लॉगिन पैरामीटर नामों को ओवरराइड करना है। तो मैंने इसे उपयोगकर्ता नाम/पासवर्ड में बदल दिया। – Warner
इसके लिए अपने स्वयं के फ़िल्टर को लागू करने की आवश्यकता नहीं है: [_ उपयोग करने के लिए डिफ़ॉल्ट पैरामीटर नाम स्थिर फ़ील्ड SPRING_SECURITY_FORM_USERNAME_KEY और SPRING_SECURITY_FORM_PASSWORD_KEY में निहित हैं। ** उपयोगकर्ता नाम पैरामीटर और पासवर्ड पैरामीटर गुण ** ._] (http://static.springsource.org/spring-security/site/docs/3.1.x/apidocs/org/springframework/ सेट करके पैरामीटर नाम भी बदला जा सकता है। सुरक्षा/वेब/प्रमाणीकरण/उपयोगकर्ता नाम पासवर्डवर्ड प्रमाणीकरण Filter.html) – Xaerxess