相关疑难解决方法(0)

在Spring 3.1中通过IP地址进行身份验证:最明智的方法吗?

我使用Spring Security 3.1实现了LDAP身份验证.我的security.xml文件发布在下面.

我需要更改我的身份验证过程,以便如果用户从"白名单"(保存在数据库表中)中的IP地址进入站点,则该用户应自动使用Spring 3.1进行身份验证,然后重定向远离登录屏幕(不是我的想法,我被告知这样).

如果用户不是来自白名单IP地址之一,则应强制他/她在登录页面上进行LDAP身份验证.

我是Spring和Spring Security的新手,所以我参阅了Spring 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 …
Run Code Online (Sandbox Code Playgroud)

java authentication ip spring-security

12
推荐指数
1
解决办法
1万
查看次数

Spring Security - 白名单IP范围

我查看过的很多资源和stackoverflow问题都提供了使用.xml文件的答案:

我想知道的是,如果可以在不使用XML配置的情况下使用Spring Security将IP地址范围列入白名单?

以下是我的控制器中的一个简单方法:

@RequestMapping(value = "/makeit", method = RequestMethod.GET)
@ResponseBody
//@PreAuthorize("hasIpAddress('192.168.0.0/16')")
public String requestData() {

    return "youve made it";
}
Run Code Online (Sandbox Code Playgroud)

我已经为安全配置创建了一个单独的类,但它没有太多,我只是为EnableGlobalMethodSecurity注释创建它- 这样我就可以使用@PreAuthorize注释(来自这里的答案:@PreAuthorize注释不能正常工作).

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        http
            .authorizeRequests()
                .anyRequest().access("hasIpAddress('0.0.0.0/0')");

        /*http
            .authorizeRequests()
                .anyRequest().hasIpAddress("0.0.0.0/0");*/

        /*http
            .authorizeRequests()
                .antMatchers("/**").hasIpAddress("0.0.0.0/0");*/

        /*http
            .authorizeRequests()
                .antMatchers("/**").access("hasIpAddress('0.0.0.0/0')");*/

        /*http
            .authorizeRequests()
                .anyRequest().access("hasIpAddress('0.0.0.0/0')");*/

    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试时,它回复了(通过POSTMAN):

{
  "timestamp": 1486743507520,
  "status": 401,
  "error": "Unauthorized", …
Run Code Online (Sandbox Code Playgroud)

java security spring-mvc spring-security spring-boot

7
推荐指数
1
解决办法
1万
查看次数