Reputation: 2468
I am working in a Spring MVC Thymeleaf project where LDAP security with Database and Role-based granted authorities is a must-have requirement from the end-user.
What I need
example: LDAP user: [email protected] Role: Admin Granted Authorities for "Admin" role: permission_x,permission_y etc
Which will be used in web page as "hasAuthority("permission_x")"
What I found is here:
Spring Security with LDAP and Database roles, which is a bit outdated
https://spring.io/guides/gs/authenticating-ldap/ where only LDAP authentication is shown.
Now my questions are:
How LDAP authentication and jdbc based authorization will work together? Can anybody help?
Thanks in advance
Upvotes: 5
Views: 5821
Reputation: 2468
Since I have found a solution, I am sharing my own answer here. Hope it will help others:
My Answers are:
How I solved my problem:
Step 1:
The trick is using a UserDetailsContextMapper with UserDetails provided by regular UserDetailsService.
example:
@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;
}
};
}
After successful LDAP authentication, it will try to load valid registered user to the context with all granted authorities.
Step 2
hasAuthority didn't work for me. I have used something like below:
<div sec:authorize="hasRole('SOME_PRIVILEGE')">
<div class="alert alert-success" role="alert">
This is secret DIV
</div>
</div>
For a quick check, you can use below:
<span sec:authentication="principal.authorities"></span>
I hope it will help somebody someday.
Happy Coding!
Edit: For LDAP over SSL and Spring Boot
Upvotes: 9