quick formatting work

This commit is contained in:
eugenp 2014-06-16 17:19:26 +03:00
parent 89d3f72333
commit a910ebae44
3 changed files with 63 additions and 89 deletions

View File

@ -4,7 +4,6 @@ import java.security.Principal;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ -38,32 +37,30 @@ public class MyController {
return "home";
}
private String getUserName(Principal principal){
if(principal == null){
private String getUserName(Principal principal) {
if (principal == null) {
return "anonymous";
}else{
} else {
final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
Collection<? extends GrantedAuthority> authorities = currentUser.getAuthorities();
for(GrantedAuthority grantedAuthority : authorities) {
for (GrantedAuthority grantedAuthority : authorities) {
System.out.println(grantedAuthority.getAuthority());
}
return principal.getName();
}
}
private Collection<String> getUserRoles(Principal principal){
if(principal == null){
private Collection<String> getUserRoles(Principal principal) {
if (principal == null) {
return Arrays.asList("none");
}else{
} else {
Set<String> roles = new HashSet<String>();
final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
Collection<? extends GrantedAuthority> authorities = currentUser.getAuthorities();
for(GrantedAuthority grantedAuthority : authorities) {
for (GrantedAuthority grantedAuthority : authorities) {
roles.add(grantedAuthority.getAuthority());
}
return roles;
@ -71,4 +68,3 @@ public class MyController {
}
}

View File

@ -16,36 +16,14 @@ import org.springframework.stereotype.Controller;
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.ldapAuthentication()
.userSearchBase("ou=people")
.userSearchFilter("(uid={0})")
.groupSearchBase("ou=groups")
.groupSearchFilter("member={0}")
.contextSource()
.root("dc=baeldung,dc=com")
.ldif("classpath:users.ldif");
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication().userSearchBase("ou=people").userSearchFilter("(uid={0})").groupSearchBase("ou=groups").groupSearchFilter("member={0}").contextSource().root("dc=baeldung,dc=com").ldif("classpath:users.ldif");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.logoutSuccessUrl("/");
http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated();
http.formLogin().loginPage("/login").permitAll().and().logout().logoutSuccessUrl("/");
}
}