简体   繁体   English

登录应用程序前的IP地址验证

[英]IP Address Authentication before login into application

Below is my use case/requirement. 以下是我的使用案例/要求。

  1. Provide a feature for IP Address authentication, let say i have 3 areas. 提供IP地址认证功能,假设我有3个区域。 (Black, Grey, White). (黑色,灰色,白色)。 Black area holds blocked IP Address, grey area hold temporarily disabled IP Address and white area contains valid IP Address. 黑色区域保持阻止的IP地址,灰色区域保持暂时禁用的IP地址,白色区域包含有效的IP地址。

  2. My login page should only be seen, when request comes from IP Address available in White area. 只有在白色区域提供的IP地址请求时才能看到我的登录页面。 For others, it should show some default page with some error message. 对于其他人,它应该显示一些带有一些错误消息的默认页面。

Do I need to do it through filters or any other option available? 我是否需要通过过滤器或任何其他可用选项来完成?

Since this done Pre login, so I cannot use AuthenticationProvider. 由于这样做了Pre登录,所以我无法使用AuthenticationProvider。

This can be done using Web Security Expressions , using hasIpAddress(...). 这可以使用hasIpAddress(...)使用Web Security Expressions来完成。

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

You can add more features to above basic IP check by implementing your own IP address check service, as below: 您可以通过实施自己的IP地址检查服务为上述基本IP检查添加更多功能,如下所示:

<bean id="validateIdService" class="your.pkg.ValidateIdService">
</bean>

<security:http auto-config="false" access-denied-page="/accessDenied.jsp" 
use-expressions="true">
    <security:intercept-url pattern="/login.jsp"
        access="@validateIdService.isValid()" />
</security:http>

And the you can have your service to check the IP address for you, as below: 您可以让您的服务为您检查IP地址,如下所示:

public class ValidateIdService {
    public boolean isValid() {
        HttpServletRequest req = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())
               .getRequest(); 
        String ipAddr = req.getRemoteAddr();
        // validate IP address
        if(valid)
           return true;
        else return false;
    }
}

Hope this helps you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM