OpenVP*整合ldap认证

前提:

ldap服务器已经安装:OpenLDAP安装部署

通过部署知道LDAP目录树,其中基准dc=xph,dc=com是该树的根节点:

该根节点有一个管理域cn=manager,dc=xph,dc=com 和一个组织单元dc=svn,dc=xph,dc=com

该组织单元下有两个子属性ou=People,dc=svn,dc=xph,dc=com 及ou=Group,dc=svn,dc=xph,dc=com

主机IP:192.168.75.129

一、生成证书

1、系统信息

[root@openvpn ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@openvpn ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:ba:87:19 brd ff:ff:ff:ff:ff:ff
    inet 192.168.75.140/24 brd 192.168.75.255 scope global noprefixroute dynamic ens33
       valid_lft 1705sec preferred_lft 1705sec
    inet6 fe80::3708:2a79:8c00:6442/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

2、添加epel yum源

[root@openvpn ~]# wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo

3、下载证书生成工具easy-rsa

[root@openvpn ~]# yum -y install easy-rsa

4、创建证书环境目录

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LDAP (Lightweight Directory Access Protocol) 是一种基于 TCP/IP 协议的轻量级目录访问协议,通常用于企业级应用程序中的用户身份验证和授权。Spring Boot 提供了对 LDAP 认证的支持,可以轻松地将其集成到应用程序中。 下面是一个基本的 LDAP 认证示例: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-ldap</artifactId> </dependency> ``` 2. 配置 application.yml 在 application.yml 文件中添加以下配置: ```yaml spring: ldap: urls: ldap://localhost:389 base: dc=example,dc=com user-search-base: ou=people user-search-filter: (uid={0}) group-search-base: ou=groups group-search-filter: (uniqueMember={0}) group-role-attribute: cn manager-dn: cn=admin,dc=example,dc=com manager-password: admin ``` 这里配置了 LDAP 服务器的 URL、基础 DN、用户搜索基础 DN、用户搜索过滤器、组搜索基础 DN、组搜索过滤器、组角色属性、管理员 DN 和管理员密码。 3. 创建 UserDetailsServiceImpl 类 创建一个实现 UserDetailsService 接口的类,用于从 LDAP 中加载用户详细信息。 ```java @Service public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private LdapTemplate ldapTemplate; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { List<UserDetails> users = ldapTemplate.search( LdapQueryBuilder.query().where("uid").is(username), (AttributesMapper<UserDetails>) attrs -> { String password = (String) attrs.get("userPassword").get(); List<GrantedAuthority> authorities = new ArrayList<>(); attrs.getAll().stream() .filter(attr -> attr.getID().startsWith("memberOf")) .flatMap(attr -> LdapUtils.convertValueToList(attr).stream()) .forEach(groupDn -> { String groupName = LdapUtils.getRdnValue(new LdapName(groupDn)).toString(); authorities.add(new SimpleGrantedAuthority("ROLE_" + groupName)); }); return new User(username, password, authorities); }); if (users.isEmpty()) { throw new UsernameNotFoundException("User not found"); } return users.get(0); } } ``` 这里使用 LdapTemplate 搜索 LDAP 目录,将查询结果转换为 UserDetails 对象,并返回该对象。 4. 创建 WebSecurityConfig 类 创建一个继承 WebSecurityConfigurerAdapter 的类,用于配置 Spring Security。 ```java @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessURL("/") .permitAll() .and() .logout() .logoutUrl("/logout") .permitAll(); } @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } } ``` 这里配置了 HTTP 访问策略和登录、注销页面的 URL,并将 UserDetailsService 注册到 AuthenticationManagerBuilder 中。 5. 创建登录和注销页面 创建一个登录页面和一个注销页面,用于在浏览器中进行身份验证和注销。例如: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form action="/login" method="post"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br><br> <input type="submit" value="Login"> </form> </body> </html> ``` ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Logout</title> </head> <body> <h1>Logout</h1> <form action="/logout" method="post"> <input type="submit" value="Logout"> </form> </body> </html> ``` 6. 运行应用程序 现在可以启动应用程序并在浏览器中访问它。输入正确的用户名和密码,应该可以成功登录。登录后,可以访问受保护的资源。注销后,应该不能访问受保护的资源。 以上是一个基本的 LDAP 认证示例,可以根据实际需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

友人a笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值