Spring-ldap Filter

Spring-ldap Filter 

类:
org.springframework.ldap.filter.AndFilter :且
org.springframework.ldap.filter.OrFilter :或者
org.springframework.ldap.filter.NotFilter :非
org.springframework.ldap.filter.PresentFilter :LDAP目中有存储属性的
org.springframework.ldap.filter.NotPresentFilter :LDAP目中有无存储属性的
org.springframework.ldap.filter.EqualsFilter :等于
org.springframework.ldap.filter.LikeFilter :等于
org.springframework.ldap.filter.WhitespaceWildcardsFilter : 模糊
org.springframework.ldap.filter.GreaterThanOrEqualsFilter :大于等于
org.springframework.ldap.filter.LessThanOrEqualsFilter : 小于等于

结构:

案例:

import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.ldap.filter.GreaterThanOrEqualsFilter;
import org.springframework.ldap.filter.LessThanOrEqualsFilter;
import org.springframework.ldap.filter.LikeFilter;
import org.springframework.ldap.filter.NotFilter;
import org.springframework.ldap.filter.NotPresentFilter;
import org.springframework.ldap.filter.OrFilter;
import org.springframework.ldap.filter.PresentFilter;
import org.springframework.ldap.filter.WhitespaceWildcardsFilter;

public class LdapFilterTest {

    /**
    * @param args
    */
    public static void main(String[] args) {
        // 且
        AndFilter filter = new AndFilter();
        // 等于
        filter.and(new EqualsFilter("objectclass", "person"));
        // 模糊
        filter.and(new WhitespaceWildcardsFilter("sn", "张三"));
        System.out.println(filter.encode());
  
        AndFilter filter2 = new AndFilter();
        // 等于
        filter2.and(new LikeFilter("cn","12121"));
        filter2.and(filter);
        System.out.println(filter2.encode());
  
        AndFilter filter3 = new AndFilter();
        // 小于等于
        filter3.and(new LessThanOrEqualsFilter("age","40"));
        filter3.and(filter2);
        System.out.println(filter3.encode());
  
        AndFilter filter4 = new AndFilter();
        // 大于等于
        filter4.and(new GreaterThanOrEqualsFilter("age","20"));
        filter4.and(filter3);
        System.out.println(filter4.encode());
  
        // 或者
        OrFilter filter5 = new OrFilter();
        filter5.or(new LikeFilter("cn","12120") );
        filter5.or(new WhitespaceWildcardsFilter("sn", "张三"));
        System.out.println(filter5.encode());
  
        // 非
        NotFilter filter6 = new NotFilter(new LikeFilter("cn","12120"));
        System.out.println(filter6.encode());
  
        // LDAP目中有无存储属性的
        NotPresentFilter filter7 = new NotPresentFilter("desc");
        System.out.println(filter7.encode());
  
        // LDAP目中有存储属性的
        PresentFilter filter8 = new PresentFilter("email");
        System.out.println(filter8.encode());

    }

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud 中可以使用 Spring Security 来实现 LDAP 认证,具体步骤如下: 1. 配置 pom.xml,引入 Spring Security 和 Spring LDAP 相关依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-ldap</artifactId> <version>${spring-security.version}</version> </dependency> <dependency> <groupId>org.springframework.ldap</groupId> <artifactId>spring-ldap-core</artifactId> <version>${spring-ldap.version}</version> </dependency> ``` 2. 配置 application.yml,设置 LDAP 连接信息: ```yaml spring: ldap: urls: ldap://localhost:389 base: dc=my-domain,dc=com username: cn=Manager,dc=my-domain,dc=com password: password ``` 3. 配置 WebSecurityConfigurerAdapter,实现 LDAP 认证: ```java @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private Environment env; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin() .and() .logout(); http.csrf().disable(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.ldapAuthentication() .userSearchBase(env.getProperty("spring.ldap.user-search-base")) .userSearchFilter(env.getProperty("spring.ldap.user-search-filter")) .groupSearchBase(env.getProperty("spring.ldap.group-search-base")) .groupSearchFilter(env.getProperty("spring.ldap.group-search-filter")) .contextSource() .url(env.getProperty("spring.ldap.urls")) .managerDn(env.getProperty("spring.ldap.username")) .managerPassword(env.getProperty("spring.ldap.password")); } } ``` 这样就可以通过 LDAP 认证来控制访问权限了。在这个例子中,请求 "/admin/**" 的用户需要拥有 ADMIN 角色,请求 "/user/**" 的用户需要拥有 USER 角色,其他请求需要认证通过即可访问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值