需求:实现获取当前用户的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>
加载页面完成后,再调用后台方法便可以拿到当前用户的所在域和用户名,进而执行后续的单点登录。
有问题欢迎评论私信。