【问题标题】:White-listing an ip in spring project在春季项目中将IP列入白名单
【发布时间】:2015-10-26 19:04:03
【问题描述】:

我有一个简单的 java 应用程序,可以登录。它有一个扩展 WebSecurityConfigurerAdapter 的 SecurityConfig 类。我已经在那里实现了几种方法

void configure(WebSecurity web)
void configure(HttpSecurity http)

在 configure(WebSecurity web) 方法中,我忽略了某些 URL 的身份验证

public void configure(WebSecurity web) throws Exception {
    web
    .ignoring()
            .antMatchers(HttpMethod.POST, "/examplePattern");
}

我在这个项目中有一些服务端点。现在我需要将某个 ip(我有一个字符串数组)列入白名单,它应该只能访问特定的端点。这是因为所有对端点的调用都要经过用户登录认证。

基本上我需要的是,如果从特定 IP 调用某个端点,请求应该到达控制器,而无需进行身份验证

我是这个领域的新手,所以如果你有解决这个问题的方法,请告诉我。

提前致谢

【问题讨论】:

    标签: java spring spring-security whitelist


    【解决方案1】:

    您将需要基于 IP 地址的身份验证提供程序,如下所示:

    @Service 
    public class IPAddressBasedAuthenticationProvider implements AuthenticationProvider {
    
    
         @Autowired
         private HttpServletRequest request;
    
         @Override
         public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    
             String ipAddress = request.getRemoteAddr();
             // Check against your array.
             //return created authentication object (if user provided valid credentials)
        }
    }
    

    我希望这会有所帮助。

    【讨论】:

    • 我正在寻找一种方法,如果 ipaddress 匹配,我不应该进行身份验证,但该请求应该通过。
    • 从什么经历?
    • 请求应该在没有认证的情况下到达控制器。基本上我需要的是,如果从特定 IP 调用某个端点,则请求应该到达控制器,而无需进行身份验证。
    • 如果你只是在你的 JSP 中调用控制器映射,它就会到达。我仍然不明白你想要什么,也许其他用户可以提供帮助。
    • 对控制器的所有调用都将通过我们验证用户是否已登录的安全性。我想要的是,如果它来自试图到达特定端点的特定 IP,而不检查身份验证。
    猜你喜欢
    • 2018-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 2017-09-22
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多