2012-10-08 12 views
15

का उपयोग कर आईपी फ़िल्टर मुझे आश्चर्य है कि स्प्रिंग सिक्योरिटी का उपयोग करके अपने आईप द्वारा उपयोगकर्ताओं को अपने वेब ऐप तक पहुंच कैसे फ़िल्टर करें। क्या मुझे AbstractAuthenticationProcessingFilter या ऐसा कुछ करना चाहिए और अपने तरीके से इसकी विधियों को ओवरराइड करना चाहिए? यदि हां, तो क्या आप web.xml में फ़िल्टर विवरण के इस विस्तार और उदाहरण का उदाहरण दे सकते हैं? अग्रिम धन्यवाद।स्प्रिंग सिक्योरिटी

पीएस मेरे ऐप में मेरे पास स्प्रिंग सिक्योरिटी सपोर्ट भी है (डिफ़ॉल्ट org.springframework.web.filter.DelegatingFilterProxy का उपयोग करके), लेकिन मैं चाहता हूं कि यह न केवल उपयोगकर्ता प्रमाण-पत्रों की जांच करे, बल्कि उनके आईपी भी।

+0

के संभावित डुप्लिकेट http://stackoverflow.com/questions/10147161/authenticating-by-ip-address-in-spring- 3-1-smartest-way-to-do-that – Anshu

उत्तर

13

स्प्रिंग सिक्योरिटी के Web Security Expressions का उपयोग करने के लिए आप ऐसा कर सकते हैं। उदाहरण के लिए:

<http use-expressions="true"> 
    <intercept-url pattern="/admin*" 
     access="hasRole('admin') and hasIpAddress('192.168.1.0/24')"/> 
    ... 
</http> 
+0

बहुत अच्छा काम किया। बस सुनिश्चित करें कि आपके पास उपयोग-अभिव्यक्ति = "सत्य" है क्योंकि वेब सुरक्षा अभिव्यक्तियों के लिए यह आवश्यक है। यद्यपि समाधान में यह है कि यह स्पष्ट रूप से यह नहीं कहता है कि यह आवश्यक है (जो मेरी मदद कर सकता था;) – Andre

+2

यह ऐसा समाधान नहीं है जो वास्तविक दुनिया परिदृश्यों में अच्छी तरह से काम करेगा, हालांकि, यह एक एक्सएमएल में हार्ड-कोड किया गया है फ़ाइल और एक ऐप-सर्वर पुनरारंभ करने की आवश्यकता है। – Aquarelle

7

चेक आईपी पता द्वारा प्रमाणीकृत करने

// Authentication Provider To Authenticate By IP Address With Allowed IPs 
// Stored in a db table 


package acme.com.controller.security; 

//import acme.com.controller.security.CustomUserInfoHolder; 

import org.springframework.security.authentication.AuthenticationProvider; 
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 
import org.springframework.security.core.Authentication; 
import org.springframework.security.core.AuthenticationException; 
import org.springframework.security.web.authentication.WebAuthenticationDetails; 
import org.springframework.security.core.context.SecurityContextHolder; 
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; 
import org.springframework.security.core.authority.mapping.NullAuthoritiesMapper; 
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 
import org.springframework.security.core.userdetails.UserDetails; 

import org.apache.log4j.Logger; 


public class CustomIPAddressAuthenticationProvider implements AuthenticationProvider 
{ 

    private static final Logger logger = Logger.getLogger(CustomIPAddressAuthenticationProvider.class); 
    private GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper(); 


    @Override 
    public Authentication authenticate(Authentication authentication) 
    throws AuthenticationException { 


     WebAuthenticationDetails wad = null; 
     String userIPAddress   = null; 
     boolean isAuthenticatedByIP = false; 

     // Get the IP address of the user tyring to use the site 
     wad = (WebAuthenticationDetails) authentication.getDetails(); 
     userIPAddress = wad.getRemoteAddress(); 


     logger.debug("userIPAddress == " + userIPAddress); 

     // Compare the user's IP Address with the IP address in the database 
     // stored in the USERS_AUTHENTICATED_BY_IP table & joined to the 
     // USERS tabe to make sure the IP Address has a current user 
     //isAuthenticatedByIP = someDataObject.hasIPAddress(userIPAddress); 
     isAuthenticatedByIP = true; 


     // Authenticated, the user's IP address matches one in the database 
     if (isAuthenticatedByIP) 
     { 

      logger.debug("isAuthenticatedByIP is true, IP Addresses match"); 
      UserDetails user = null; 


      UsernamePasswordAuthenticationToken result = null; 

      result = new UsernamePasswordAuthenticationToken("John Principal", 
                   "PlaceholderPWE"); 

      result.setDetails(authentication.getDetails()); 

      return result; 
     } 


     // Authentication didn't happen, return null to signal that the 
     // AuthenticationManager should move on to the next Authentication provider 
     return null; 
    } 


    @Override 
    public boolean supports(Class<? extends Object> authentication) 
    { 
     // copied it from AbstractUserDetailsAuthenticationProvider 
     return(UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); 
    } 

} 

सुरक्षा-web.xml

<s:http pattern="/login*" security="none"/> 
    <s:http pattern="/search*" security="none"/> 
    <s:http pattern="/css/**" security="none"/> 
    <s:http pattern="/js/**" security="none"/> 
    <s:http pattern="/images/**" security="none"/> 




    <s:http auto-config="true" use-expressions="true"> 
     <s:intercept-url pattern="/**" access="isAuthenticated()" /> 

     <s:form-login login-page="/login" 
      authentication-failure-url="/loginfailed" /> 
     <s:logout logout-success-url="/logout" /> 
    </s:http> 



    <s:ldap-server url = "ldap://ldap-itc.smen.acme.com:636/o=acme.com"/> 


    <bean id="customIPAddressAuthenticationProvider" class="com.acme.controller.security.CustomIPAddressAuthenticationProvider" /> 


    <s:authentication-manager> 
     <!-- Proposed: Custom Authentication Provider: Try To Authenticate BY IP Address First, IF NOT, Authenticate WiTh THE LDAP Authentication Provider --> 
     <s:authentication-provider ref="customIPAddressAuthenticationProvider" /> 
     <s:ldap-authentication-provider user-dn-pattern="uid={0},ou=People"/> 
    </s:authentication-manager> 


</beans> 

दृष्टिकोण मौजूदा प्रश्न Authenticating By IP Address In Spring 3.1: Smartest Way To Do That? से लिया जाता है के लिए इस कस्टम AuthenticationProvider कार्यान्वयन, इस के साथ शुरू करने के लिए मदद कर सकता है।

+0

आपके उत्तर के लिए धन्यवाद। ऐसा लगता है कि मेरे अनुरोध के लिए उपयुक्त है, लेकिन मैंने बस इसकी सादगी के कारण वेब सुरक्षा अभिव्यक्तियों का उपयोग करने का निर्णय लिया। –

0

अंशु के जवाब आईपी द्वारा एक उपयोगकर्ता के सत्यापन का एक अच्छा विचार है, लेकिन यह cas प्रमाणीकरण के साथ काम नहीं किया जा सकता है। मेरे पास एक और संकल्प है, फ़िल्टर का उपयोग करके इस स्थिति के लिए अधिक उपयुक्त है।

public class IPAuthenticationFilter extends AbstractAuthenticationProcessingFilter { 
    private AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService; 
    private static Set<String> ipWhitelist; 

    @Autowired 
    private AppProperty appProperty; 

    @PostConstruct 
    public void init() { 
     ipWhitelist = new HashSet<>(Arrays.asList(appProperty.getIpWhitelist())); 
     setAuthenticationSuccessHandler(new AuthenticationSuccessHandler() { 
      @Override 
      public void onAuthenticationSuccess(
        HttpServletRequest httpServletRequest, 
        HttpServletResponse httpServletResponse, 
        Authentication authentication) throws IOException, ServletException { 
       // do nothing 
      } 
     }); 
    } 

    public IPAuthenticationFilter() { 
              super("/"); 
                          } 

    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) 
      throws AuthenticationException, IOException { 
     String userName = request.getHeader(appProperty.getHeaderCurUser()); 
     Assertion assertion = new AssertionImpl(userName); 
     CasAssertionAuthenticationToken token = new CasAssertionAuthenticationToken(assertion, ""); 
     UserDetails userDetails = authenticationUserDetailsService.loadUserDetails(token); 

     CasAuthenticationToken result = new CasAuthenticationToken(
       "an-id-for-ip-auth", 
       userDetails, 
       request.getRemoteAddr(), 
       userDetails.getAuthorities(), 
       userDetails, 
       assertion 
     ); 
     return result; 
    } 

    protected boolean requiresAuthentication(HttpServletRequest request, HttpServletResponse response) { 
     String userName = request.getHeader(appProperty.getHeaderCurUser()); 
     return ipWhitelist.contains(request.getRemoteAddr()) && !StringUtils.isEmpty(userName); 
    } 

    protected void successfulAuthentication(
      HttpServletRequest request, 
      HttpServletResponse response, 
      FilterChain chain, 
      Authentication authResult) throws IOException, ServletException { 
     super.successfulAuthentication(request, response, chain, authResult); 
     chain.doFilter(request, response); 
    } 

    public AuthenticationUserDetailsService<CasAssertionAuthenticationToken> getAuthenticationUserDetailsService() { 
     return authenticationUserDetailsService; 
    } 

    public void setAuthenticationUserDetailsService(
      AuthenticationUserDetailsService<CasAssertionAuthenticationToken> authenticationUserDetailsService) { 
     this.authenticationUserDetailsService = authenticationUserDetailsService; 
    } 
} 

आप इस तरह कैस से पहले इस फिल्टर जोड़ सकते हैं:

http.addFilterBefore(ipAuthenticationFilter(), CasAuthenticationFilter.class)