Spring Security configuration

Modify the SpringSecurityConfiguration.java file, as follows:

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger LOG =
LoggerFactory.getLogger(SpringSecurityConfig.class);
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests() .antMatchers("/admins").hasRole("ADMINS")
.antMatchers("/users").hasRole("USERS")
.anyRequest().fullyAuthenticated()
.and()
.httpBasic(); // Use Basic authentication
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userDnPatterns("uid={0},ou=people")
.userSearchBase("ou=people")
.userSearchFilter("uid={0}")
.groupSearchBase("ou=groups")
.groupSearchFilter("uniqueMember={0}")
.contextSource(contextSource())
.passwordCompare()
.passwordAttribute("userPassword");
}
@Bean
public DefaultSpringSecurityContextSource contextSource() {
LOG.info("Inside configuring embedded LDAP server");
DefaultSpringSecurityContextSource contextSource = new
DefaultSpringSecurityContextSource(
Arrays.asList("ldap://localhost:8389/"), "dc=packtpub,dc=com");
contextSource.afterPropertiesSet();
return contextSource;
}
}

The first configure method is very similar to what we saw in the previous SAML example. We have just added certain matches and separated the roles. With these changes, it will still perform basic authentication.

The second configure method is where we have set up authentication using the LDAP server. The LDAP server stores user information in a directory-like format. This method details how to find the user by navigating through the directory structure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.138.138