mnh*_*ilu 3 spring ldap spring-mvc spring-security spring-boot
我在 Spring MVC Thymeleaf 项目中工作,其中具有数据库和基于角色的授予权限的 LDAP 安全性是最终用户的必备要求。
我需要的
Run Code Online (Sandbox Code Playgroud)example: LDAP user: nahid@test.com Role: Admin Granted Authorities for "Admin" role: permission_x,permission_y etc它将在网页中用作“hasAuthority(“permission_x”)”
我发现的是在这里:
https://spring.io/guides/gs/authenticating-ldap/其中仅显示 LDAP 身份验证。
现在我的问题是:
LDAP 身份验证和基于 jdbc 的授权将如何协同工作?有人可以帮忙吗?
提前致谢
由于我找到了解决方案,我在这里分享我自己的答案。希望它会帮助其他人:
我的回答是:
我如何解决我的问题:
第1步:
诀窍是使用的UserDetailsContextMapper用的UserDetails定期提供的UserDetailsService。
例子:
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDetailsContextMapper(userDetailsContextMapper())
.userDnPatterns("uid={0},ou=people")
.groupSearchBase("ou=groups")
.contextSource()
.url("ldap://localhost:8389/dc=springframework,dc=org")
.and()
.passwordCompare()
.passwordEncoder(new BCryptPasswordEncoder())
.passwordAttribute("userPassword");
}
@Bean
public UserDetailsContextMapper userDetailsContextMapper() {
return new LdapUserDetailsMapper() {
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
UserDetails details= userDetailsService.loadUserByUsername(username+"@test.com");
return details;
}
};
}
Run Code Online (Sandbox Code Playgroud)
成功通过 LDAP 身份验证后,它将尝试将有效的注册用户加载到具有所有授予权限的上下文中。
第2步
hasAuthority对我不起作用。我使用过类似下面的东西:
<div sec:authorize="hasRole('SOME_PRIVILEGE')">
<div class="alert alert-success" role="alert">
This is secret DIV
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
为了快速检查,您可以使用以下内容:
<span sec:authentication="principal.authorities"></span>
Run Code Online (Sandbox Code Playgroud)
我希望有一天它会帮助某人。
快乐编码!
编辑: 对于 LDAP over SSL 和 Spring Boot
| 归档时间: |
|
| 查看次数: |
1171 次 |
| 最近记录: |