java使用域账号登陆,ldap协议

我们在进行内部管理系统时,往往需要根据电脑域的账号来进行登陆控制。java在使用域账号登陆的时是使用到javax.naming.*来进行的,使用ldap协议。

具体代码如下:

public static boolean check(String userName, String password) {
    String host = "ad.xxx.com"; // AD服务器IP
    String port = "389"; // 端口

    String url = new String("ldap://" + host + ":" + port);
    String user = String.format("%s@xxx.com", userName);

    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.SECURITY_AUTHENTICATION, "simple");//一种模式,不用管,就这么写就可以了
    env.put(Context.SECURITY_PRINCIPAL, user);
    env.put(Context.SECURITY_CREDENTIALS, password);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    DirContext ctx = null;
    try {
        ctx = new InitialDirContext(env);
        return Boolean.TRUE;
    } catch (Exception err) {
        return Boolean.FALSE;
    } finally {
        try {
            if (ctx != null) {
                ctx.close();
            }
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Windows账号登录,你需要使用Spring Security框架。以下是实现步骤: 1. 添加Spring Security依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ``` 2. 配置Spring Security 在Spring Boot应用程序中,可以通过编写一个继承了WebSecurityConfigurerAdapter的配置类来配置Spring Security。 ```java @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private ActiveDirectoryLdapAuthenticationProvider adProvider; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { auth.authenticationProvider(adProvider); } } ``` 在这个配置类中,我们使用ActiveDirectoryLdapAuthenticationProvider来实现Windows账号的认证。 3. 配置LDAP连接 在application.properties文件中添加以下配置,用于连接LDAP服务器: ``` spring.ldap.urls=ldap://your.ldap.server spring.ldap.base=dc=your,dc=domain spring.ldap.username=your-ldap-username spring.ldap.password=your-ldap-password ``` 4. 实现登录页面 最后,你需要在/templates目录下创建一个login.html页面来实现登录页面,例如: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <form method="post" action="/login"> <label for="username">Username:</label> <input type="text" id="username" name="username" /><br /> <label for="password">Password:</label> <input type="password" id="password" name="password" /><br /> <input type="submit" value="Submit" /> </form> </body> </html> ``` 这样,你就可以使用Windows账号登录你的Spring Boot应用程序了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值