Java使用LDAP修改解锁域账号,跳过证书认证

需求:员工在线更改域账号密码;解锁域账号;

环境:SpringBoot+Java8+Ldap。

必要条件:一个能改密码和解锁账号的超级管理员账号;

认证LDAP服务,跳过证书

连接LDAP,代码如下
    private static DirContext ctx = null;
    private static final String adminName = "cn=aaa,ou=AAAA,DC=cccc,DC=dddd,DC=com";
    private static final String adminPassword = "43sdrs";
    private static final String ldapUrl = "ldaps://aaa.ddddd.com:636";
    public static void ldapConnect() throws NamingException {
        Hashtable<Object, Object> env = new Hashtable<Object, Object>();
        //解决No subject alternative DNS name xxxxx的错误
        Security.setProperty("jdk.tls.disabledAlgorithms", "");
        System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");

        env.put(Context.SECURITY_PROTOCOL, "ssl");
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, adminName);
        env.put(Context.SECURITY_CREDENTIALS, adminPassword);
        env.put(Context.PROVIDER_URL, ldapUrl);
        //绕过证书认证,别忘记输入正确的类路径
        env.put("java.naming.ldap.factory.socket", "com.xxx.bbb.DummySSLSocketFactory");
        ctx = new InitialDirContext(env);
    }
跳过证书

需要用到三个类DummySSLSocketFactory,DummyTrustManager和TrustAllTrustManager

  • DummySSLSocketFactory
public class DummySSLSocketFactory extends SSLSocketFactory {
    private SSLSocketFactory factory;

    public DummySSLSocketFactory() {
        try {
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(null, new TrustManager[]{new DummyTrustManager()}, new java.security.SecureRandom());
            factory = (SSLSocketFactory) sslcontext.getSocketFactory();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public static SocketFactory getDefault() {
        return new DummySSLSocketFactory();
    }
    public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
        return factory.createSocket(socket, s, i, flag);
    }
    public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {
        return factory.createSocket(inaddr, i, inaddr1, j);
    }
    public Socket createSocket(InetAddress inaddr, int i) throws IOException {
        return factory.createSocket(inaddr, i);
    }
    public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
        return factory.createSocket(s, i, inaddr, j);
    }
    public Socket createSocket(String s, int i) throws IOException {
        return factory.createSocket(s, i);
    }
    public String[] getDefaultCipherSuites() {
        return factory.getSupportedCipherSuites();
    }
    public String[] getSupportedCipherSuites() {
        return factory.getSupportedCipherSuites();
    }
}
  • DummyTrustManager
public class DummyTrustManager implements X509TrustManager {
    public void checkClientTrusted( X509Certificate[] cert, String authType) {
        return;
    }
    
    public void checkServerTrusted( X509Certificate[] cert, String authType) {
        return;
    }
    
    public X509Certificate[] getAcceptedIssuers() {
        return new X509Certificate[0];
    }
}
  • TrustAllTrustManager
public class TrustAllTrustManager implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager {

    @Override
    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
        return null;
    }

    @Override
    public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
            throws java.security.cert.CertificateException {
        return;
    }

    @Override
    public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
            throws java.security.cert.CertificateException {
        return;
    }

}

修改域账号密码

    public static void updatePwd(String account, String newPwd) throws Exception {
        String userDN = getUserDomain(account);
        if (StringUtils.isBlank(userDN)) {
            return;
        }
        String newQuotedPassword = String.format("\"%s\"", newPwd);
        byte[] newUnicodePassword;
        newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");
        ModificationItem[] mods = new ModificationItem[1];
        mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
                new BasicAttribute("unicodePwd", newUnicodePassword));
        ctx.modifyAttributes(userDN, mods);
    }

解锁域账号

    public static void unlock(String account) throws NamingException {
        String userDN = getUserDomain(account);
        log.info("userDN:{}", userDN);
        BasicAttributes basicAttributes = new BasicAttributes();
        //512,表示Normal即正常账号
        basicAttributes.put("userAccountControl", "512");
        //lockOutTime表示已经锁住的事件,需要置为0可以立即解锁
        basicAttributes.put("lockOutTime", "0");
        ctx.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, basicAttributes);
    }

获取用户的域账号信息

private static String getUserDomain(String account) throws NamingException {
        String userDN = null;
        SearchControls contro = new SearchControls();
        contro.setSearchScope(2);

        NamingEnumeration<SearchResult> en = ctx.search("DC=aaa,DC=bbbbb,DC=com", "cn=" + account, contro);
        if (en == null || !en.hasMoreElements()) {
            return null;
        }
        while (en.hasMoreElements()) {
            Object obj = en.nextElement();
            if (obj instanceof SearchResult) {
                SearchResult si = (SearchResult) obj;
                Attributes attrs = si.getAttributes();
                userDN = (String) attrs.get("distinguishedName").get();
                break;
            }
        }
        return userDN;
    }

关闭LDAP连接

    public static void closeConnect() {
        try {
            if (ctx != null) {
                ctx.close();
            }
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }

示例

    /**
      解锁域账号
    **/
    public static void main(String[] args) {
        try {
            LdapSSLUtil.ldapConnect();
            LdapSSLUtil.enableUser(adName);
        } catch (Exception e) {
            log.error("发送错误:{}", e.getMessage(), e);
        } finally {
            LdapSSLUtil.closeConnect();
        }
    }

参考链接

https://www.cnblogs.com/huanghongbo/p/12409209.html

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Spring Boot集成LDAP实现AD用户认证、用户检索和密码重置,需要进行以下步骤: 1. 添加Spring LDAP和Spring Security依赖 首先,在pom.xml文件中添加Spring LDAP和Spring Security依赖: ```xml <dependency> <groupId>org.springframework.ldap</groupId> <artifactId>spring-ldap-core</artifactId> <version>2.3.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-ldap</artifactId> <version>5.4.6</version> </dependency> ``` 2. 配置LDAP连接信息 在application.properties文件中配置LDAP连接信息,如下所示: ```properties # LDAP server URL spring.ldap.urls=ldap://ad.example.com:389/ # LDAP base DN spring.ldap.base=dc=example,dc=com # LDAP user DN spring.ldap.username=cn=admin,dc=example,dc=com # LDAP user password spring.ldap.password=secret ``` 3. 配置Spring Security认证 在SecurityConfig类中配置Spring Security认证: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/login").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .failureUrl("/login?error=true") .permitAll() .and() .logout() .logoutSuccessUrl("/login") .permitAll(); } @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth .ldapAuthentication() .userDnPatterns("uid={0},ou=people") .groupSearchBase("ou=groups") .contextSource() .url("ldap://ad.example.com:389/dc=example,dc=com") .and() .passwordCompare() .passwordEncoder(new BCryptPasswordEncoder()) .passwordAttribute("userPassword"); } } ``` 这里,我们使用LDAP进行用户认证,用户DN格式为`uid={0},ou=people`,组DN为`ou=groups`。同时,我们使用BCrypt密码编码器进行密码加密。 4. 实现用户检索和密码重置 我们可以使用LdapTemplate类来检索用户和重置密码。例如,检索所有用户的代码如下: ```java @Autowired private LdapTemplate ldapTemplate; public List<String> findAllUsers() { return ldapTemplate.search( LdapQueryBuilder.query().where("objectclass").is("person"), (AttributesMapper<String>) attrs -> (String) attrs.get("cn").get() ); } ``` 重置密码的代码如下: ```java @Autowired private LdapTemplate ldapTemplate; public void resetPassword(String username, String newPassword) { Name dn = LdapNameBuilder .newInstance() .add("ou", "people") .add("uid", username) .build(); ModificationItem[] mods = new ModificationItem[] { new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userPassword", newPassword)) }; ldapTemplate.modifyAttributes(dn, mods); } ``` 这里,我们使用LdapNameBuilder类构建用户DN,然后使用ModifyAttributes方法重置用户密码。 以上就是使用Spring Boot集成LDAP实现AD用户认证、用户检索和密码重置的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值