验证通过将IP地址在Spring 3.1:最聪明的方式做到这一点?(Authenticating B

2019-06-23 14:40发布

我已经使用Spring Security的3.1实现LDAP认证。 我对于security.xml文件被贴在下面。

我需要改变我的身份验证过程,例如,如果一用户从一个IP地址的“白名单”(保存在一个数据库表)的网站,那么该用户将自动被使用Spring 3.1身份验证,然后从重定向离开登录屏幕(不是我的主意,我被告知这样)。

如果用户从白名单IP地址之一是没有,那么他/她应该被强迫去通过登录页面上的LDAP认证。

我是新来春春的安全,所以我去了春季3.1参考文档和读取所有第一节还有,我看,如果您有任何特殊的认证需要你应该阅读建议第二节架构和实施 。 我这样做,很慢,做了笔记。

然而,由于我是新来的这一切,我不知道我完全明白我需要做的,什么是要去这样做是最聪明的方式。


更新3:我得到的骨架代码工作的,这里是我结束了文件


我的自定义的AuthenticationProvider实现通过IP地址认证

// 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));
    }

}

我* -security.xml文件

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:s="http://www.springframework.org/schema/security"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <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>

Answer 1:

你的做法似乎相当完善,你是正确的思维是,Spring会尝试每个AuthenticationProvider的,直到它得到一个成功的结果,所以你的情况,你会在LDAP提供商之前定义的基于IP的供应商。

根据设置的不同,你可能无法得到你的authentication.getDetails()调用WebAuthenticationDetails对象。 如果是这样的话,你应该添加Spring的RequestContextListener或RequestContextFilter两个到你的web.xml。 然后,您就可以通过使用RequestContextHolder类,并呼吁RequestContextHolder.getRequestAttributes获得源IP地址()。

您应该只需要实现的AuthenticationProvider,没有必要实施的UserDetailsS​​ervice的UserDetails或认证类。 如果你不能够通过自己的IP地址来认证用户,你应该返回null。 在这种情况下,Spring会尝试LDAP提供。 如果由于某种原因,你不希望传递到LDAP,你应该抛出的AuthenticationException将停止该过程,并最终导致403错误的用户的尝试。

我希望这有帮助 :)



文章来源: Authenticating By IP Address In Spring 3.1: Smartest Way To Do That?