2012-07-05 9 views
6

में उपयोगकर्ता अधिकृत करने के बाद कस्टम फ़िल्टर जोड़ने के लिए कैसे मैं वसंत सुरक्षा 3 के लिए नौसिखिया हूँ। मैं उपयोगकर्ताओं के लॉगिन के लिए भूमिका का उपयोग कर रहा हूं।वसंत आवेदन

उपयोगकर्ता के आवेदन में अधिकृत होने के बाद मैं कुछ सत्र मूल्य जोड़ना चाहता हूं। शायद मुझे कुछ फ़िल्टर चाहिए ताकि यह मेरी विधि पर रीडायरेक्ट हो जो कुछ सत्र मान जोड़ता है। मैंने अपनी सुरक्षा.एक्सएमएल फ़ाइल को कॉन्फ़िगर किया है लेकिन मुझे यकीन नहीं है कि मैं सही चीजें कर रहा हूं या नहीं। उस दिशा में कोई भी उदाहरण मदद करेगा। मुझे किस फ़िल्टर क्लास का उपयोग करना चाहिए? मुझे security.xml फ़ाइल को कैसे कॉन्फ़िगर करना चाहिए?

<custom-filter ref="authenticationFilter" after="FORM_LOGIN_FILTER "/> 

<beans:bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"> 
    <beans:property name="filterProcessesUrl" value="/j_spring_security_check" /> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
    <beans:property name="authenticationSuccessHandler" ref="successHandler" /> 
</beans:bean> 

<beans:bean id="successHandler" class="org.dfci.sparks.datarequest.security.CustomAuthorizationFilter"/> 

मेरी फ़िल्टर क्लास विधि मुझे कुछ सत्र मान जोड़ने की आवश्यकता है।

public class CustomAuthorizationFilter implements AuthenticationSuccessHandler { 

    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, 
      HttpServletResponse response, Authentication authentication) 
      throws IOException, ServletException { 
     Set<String> roles = AuthorityUtils.authorityListToSet(authentication 
       .getAuthorities()); 
     if (roles.contains("ROLE_USER")) { 
      request.getSession().setAttribute("myVale", "myvalue"); 
     } 
    } 
} 

संपादित कोड

मैं अपने security.xml फ़ाइल और वर्ग फ़ाइल

<custom-filter ref="authenticationFilter" after="FORM_LOGIN_FILTER "/> 
public class CustomAuthorizationFilter extends GenericFilterBean { 

    /* 
    * ServletRequestAttributes attr = (ServletRequestAttributes) 
    * RequestContextHolder.currentRequestAttributes(); HttpSession 
    * session=attr.getRequest().getSession(true); 
    */ 
    @Autowired 
    private UserService userService; 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, 
      FilterChain chain) throws IOException, ServletException { 

     try { 
      chain.doFilter(request, response); 

        HttpServletRequest req = (HttpServletRequest) request; 
        HttpSession session = req.getSession(true); 
        Authentication authentication = SecurityContextHolder 
          .getContext().getAuthentication(); 
        Set<String> roles = AuthorityUtils 
          .authorityListToSet(authentication.getAuthorities()); 
        User user = null;     
         if (true) { 
          session.setAttribute("Flag", "Y"); 
         } 
      } 

     } catch (IOException ex) { 
      throw ex; 
     } 
    } 

} 

कौन सा हर यूआरएल का आह्वान संशोधित किया है। क्या उपयोगकर्ता फ़िल्टर प्रमाणीकृत होने पर केवल फ़िल्टर विधि को कॉल करने का कोई विकल्प है?

+0

उपर्युक्त कोड में समस्या क्या है? क्या आप लॉगिन सफलता यूआरएल के अंदर सत्र myValue विशेषता नहीं प्राप्त कर रहे हैं? –

+0

@ सुनीलचवन: यह उपयोगकर्ता प्रमाणित होने से पहले इस फ़िल्टर को कॉल करता है। – Raje

+0

आप यह कैसे कह सकते हैं? (एपीआई) देखें [http://static.springsource.org/spring-security/site/docs/3.0.x/apidocs/org/springframework/security/web/authentication/AuthenticationSuccessHandler.html] विवरण। यह स्पष्ट प्रमाणीकरण के बाद स्पष्ट रूप से कहता है कि यह प्रमाणीकरण सफलता विधि –

उत्तर

8

अंत में मैं अपनी समस्या का समाधान करने में सक्षम था। फ़िल्टर का उपयोग करने के बजाय मैंने हैंडलर जोड़ा है जो केवल सफल लॉगिन के लिए आमंत्रित करता है।

के बाद लाइन security.xml में जो सत्र विशेषता जोड़ने

<form-login login-page="/" authentication-failure-url="/?login_error=1" default-target-url="/" always-use-default-target="false" 
     authentication-success-handler-ref="authenticationSuccessHandler"/> 
     <logout /> 

<beans:bean id="authenticationSuccessHandler" class="security.CustomSuccessHandler"/> 

इसके अलावा, मैं जोड़ लिया है एक कस्टम हैंडलर जोड़ा जाता है।

package security; 

import java.io.IOException; 
import java.security.GeneralSecurityException; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; 

public class CustomSuccessHandler extends 
      SavedRequestAwareAuthenticationSuccessHandler { 
    @Override 
    public void onAuthenticationSuccess(final HttpServletRequest request, 
      final HttpServletResponse response, final Authentication authentication) 
      throws IOException, ServletException { 
     super.onAuthenticationSuccess(request, response, authentication); 

     HttpSession session = request.getSession(true); 

     try { 
      if (CurrentUser.isUserInRole("USER")) { 
       session.setAttribute("Flag", "user"); 
      } 
     } catch (Exception e) { 
      logger.error("Error in getting User()", e); 
     } 
    } 

} 
+0

मैं एक सामान्य समारोह को सुनने से बुला रहा हूं, लेकिन यह शून्य सूचक अपवाद लौटाता है जो jdbctemplate चला रहा है। – Zigri2612

+0

exce। Threadpoolexecutor अज्ञात स्रोत – Zigri2612

0

आप स्टैंडआर्ट जावा फ़िल्टर (मेरा मतलब फ़िल्टर इंटरफ़ेस लागू करना) का उपयोग कर सकते हैं। वेब.एक्सएमएल में प्रमाणीकरण फ़िल्टर के बाद बस इसे रखें (इसका मतलब है कि यह बाद में फ़िल्टर श्रृंखला में जाएगा और सुरक्षा फ़िल्टर श्रृंखला के बाद बुलाया जाएगा)। web.xml की

public class CustomFilter implements Filter{ 

    @Override 
    public void destroy() { 
     // Do nothing 
    } 

    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, 
      FilterChain chain) throws IOException, ServletException { 

      HttpServletRequest request = (HttpServletRequest) req; 

      Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 

      Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); 
      if (roles.contains("ROLE_USER")) { 
       request.getSession().setAttribute("myVale", "myvalue"); 
      } 

      chain.doFilter(req, res); 

    } 

    @Override 
    public void init(FilterConfig arg0) throws ServletException { 
     // Do nothing 
    } 

} 

टुकड़ा:

<!-- The Spring Security Filter Chain --> 
<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
</filter> 

<!-- Pay attention to the url-pattern --> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
    <!-- <dispatcher>FORWARD</dispatcher> 
<dispatcher>REQUEST</dispatcher> --> 
</filter-mapping> 

<!-- Your filter definition --> 
<filter> 
    <filter-name>customFilter</filter-name> 
    <filter-class>com.yourcompany.test.CustomFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>customFilter</filter-name> 
    <url-pattern>/VacationsManager.jsp</url-pattern> 
</filter-mapping> 
+0

मैं आपके मामले में बहुत कुछ उदाहरण संपादित करता हूं। उदाहरण देने के लिए – dimas

+0

धन्यवाद। जैसा कि आपने उल्लेख किया है, मैं उसी चरण का पालन करता हूं। मैंने अपना कोड संशोधित किया है लेकिन फ़िल्टर विधि प्रत्येक यूआरएल के लिए कॉल करती है। क्या उपयोगकर्ता केवल प्रमाणित होने पर कॉल करने का कोई विकल्प है? – Raje

+0

यदि मैं आपको सही समझता हूं, तो आपको फ़िल्टर के लिए सही यूआरएल-पैटर्न सेट करना चाहिए (उदाहरण के लिए आपका लॉगिन पेज /login.jsp)। ऐसे मामले में फ़िल्टर केवल आपके लॉगिन पृष्ठ के लिए अनुरोध को रोक देगा। अन्य यूआरएल फिल्टर निष्पादित नहीं होगा। – dimas