springboot下集成waffle实现windows AD域身份认证和单点登录

需求:实现获取当前用户的windows AD域的账户的所在域和用户名,即domain和username,从而实现身份认证,完成单点登录;

通过方案比对,目前第三方库waffle是一个比较好的方案,但是注意运行后台服务的系统也必须是windows系统,用户这边的满足使用要求,于是采用了该方案。

waffle集成

pom.xml中添加以下依赖

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.5.0</version>
</dependency>
<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna-platform</artifactId>
    <version>5.5.0</version>
</dependency>
<dependency>
    <groupId>com.github.waffle</groupId>
    <artifactId>waffle-spring-boot-starter</artifactId>
    <version>2.2.1</version>
</dependency>

在SecurityConfig中添加以下内容

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private NegotiateSecurityFilter filter;
private NegotiateSecurityFilterEntryPoint entryPoint;

/**
 * Autowire constructor injects bean auto-configured by Starter.
 *
 * @param filter
 *            the filter
 * @param entryPoint
 *            the entry point
 */
public SecurityConfig(NegotiateSecurityFilter filter, NegotiateSecurityFilterEntryPoint entryPoint) {
    this.filter = filter;
    this.entryPoint = entryPoint;
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.authorizeRequests().anyRequest().authenticated().and()
    .addFilterBefore(filter, BasicAuthenticationFilter.class).exceptionHandling()
    .authenticationEntryPoint(entryPoint);
}
}

在Controller方法中通过getRemoteUser便可以获取到当前用户的所在域和用户名

String remote = request.getRemoteUser();
String domain = remote.split("\\\\")[0];
String username = remote.split("\\\\")[1];
log.info("domain:"+domain);
log.info("username:"+username);

出现的问题:

前端通过异步ajax的方式来获取后台方法的返回值时一直出现问题,后来查阅发现waffle使用的NTLM认证不能通过异步方式来获取,于是在前端通过iframe引入验证链接,才用同步的方式调用。

<iframe style="display: none;" :src="iframeSrc" ref="iframe"></iframe>

加载页面完成后,再调用后台方法便可以拿到当前用户的所在域和用户名,进而执行后续的单点登录。

有问题欢迎评论私信。

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Waffle 是一个用于在 Java 平台上实现 Windows 集成认证的库。Waffle 提供了一个简单的 API,可以在 Windows 系统环境下进行身份验证。Waffle 支持多种 Windows 身份验证协议,包括 NTLM 和 Kerberos。 如果你正在使用 Waffle,可以使用 Waffle API 中提供的 WindowsAuthProviderImpl 类来获取当前用户的密码。这个类提供了一个 getAuthenticatedCredentials() 方法,可以返回一个包含用户名和密码的 Credential 对象。 下面是一个使用 Waffle 获取当前用户密码的示例代码: ```java import waffle.windows.auth.*; public class WaffleDemo { public static void main(String[] args) throws Exception { WindowsAuthProviderImpl provider = new WindowsAuthProviderImpl(); Credential credential = provider.getAuthenticatedCredentials(); String username = credential.getPrincipalName(); String password = credential.getPassword(); System.out.println("Username: " + username); System.out.println("Password: " + password); } } ``` 在上面的代码中,我们首先创建了一个 WindowsAuthProviderImpl 对象,然后调用它的 getAuthenticatedCredentials() 方法来获取当前用户的凭据。最后,我们分别从凭据对象中获取用户名和密码,并将它们打印到控制台上。 需要注意的是,获取用户密码是一种不安全的行为,因为密码通常应该是加密的并且不应该以明文形式存储或传输。因此,强烈建议避免在应用程序中获取用户密码,而是使用其他身份验证方法来验证用户身份。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值