From a910ebae4419b9f235a1d73a5d2a1bb830804b16 Mon Sep 17 00:00:00 2001 From: eugenp Date: Mon, 16 Jun 2014 17:19:26 +0300 Subject: [PATCH] quick formatting work --- .../org/baeldung/SampleLDAPApplication.java | 14 +-- .../org/baeldung/controller/MyController.java | 98 +++++++++---------- .../org/baeldung/security/SecurityConfig.java | 40 ++------ 3 files changed, 63 insertions(+), 89 deletions(-) diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java index 8bbf97de8f..e380ea9602 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/SampleLDAPApplication.java @@ -15,13 +15,13 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter @ComponentScan("org.baeldung") public class SampleLDAPApplication extends WebMvcConfigurerAdapter { - public static void main(String[] args) { - SpringApplication.run(SampleLDAPApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(SampleLDAPApplication.class, args); + } - @Override - public void addViewControllers(ViewControllerRegistry registry) { - registry.addViewController("/login").setViewName("login"); - } + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/login").setViewName("login"); + } } \ No newline at end of file diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java index f65cf04939..e347bd8633 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/controller/MyController.java @@ -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; @@ -20,55 +19,52 @@ import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyController { - @RequestMapping("/") - public String init(Map model, Principal principal) { - model.put("title", "PUBLIC AREA"); - model.put("message", "Any user can view this page"); - model.put("username", getUserName(principal)); - model.put("userroles", getUserRoles(principal)); - return "home"; - } + @RequestMapping("/") + public String init(Map model, Principal principal) { + model.put("title", "PUBLIC AREA"); + model.put("message", "Any user can view this page"); + model.put("username", getUserName(principal)); + model.put("userroles", getUserRoles(principal)); + return "home"; + } + + @RequestMapping("/secure") + public String secure(Map model, Principal principal) { + model.put("title", "SECURE AREA"); + model.put("message", "Only Authorised Users Can See This Page"); + model.put("username", getUserName(principal)); + model.put("userroles", getUserRoles(principal)); + return "home"; + } + + private String getUserName(Principal principal) { + if (principal == null) { + return "anonymous"; + } else { + + final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal(); + Collection authorities = currentUser.getAuthorities(); + for (GrantedAuthority grantedAuthority : authorities) { + System.out.println(grantedAuthority.getAuthority()); + } + return principal.getName(); + } + } + + private Collection getUserRoles(Principal principal) { + if (principal == null) { + return Arrays.asList("none"); + } else { + + Set roles = new HashSet(); + + final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal(); + Collection authorities = currentUser.getAuthorities(); + for (GrantedAuthority grantedAuthority : authorities) { + roles.add(grantedAuthority.getAuthority()); + } + return roles; + } + } - @RequestMapping("/secure") - public String secure(Map model, Principal principal) { - model.put("title", "SECURE AREA"); - model.put("message", "Only Authorised Users Can See This Page"); - model.put("username", getUserName(principal)); - model.put("userroles", getUserRoles(principal)); - return "home"; - } - - private String getUserName(Principal principal){ - - if(principal == null){ - return "anonymous"; - }else{ - - final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal(); - Collection authorities = currentUser.getAuthorities(); - for(GrantedAuthority grantedAuthority : authorities) { - System.out.println(grantedAuthority.getAuthority()); - } - return principal.getName(); - } - } - - private Collection getUserRoles(Principal principal){ - - if(principal == null){ - return Arrays.asList("none"); - }else{ - - Set roles = new HashSet(); - - final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal(); - Collection authorities = currentUser.getAuthorities(); - for(GrantedAuthority grantedAuthority : authorities) { - roles.add(grantedAuthority.getAuthority()); - } - return roles; - } - } - } - diff --git a/spring-security-mvc-ldap/src/main/java/org/baeldung/security/SecurityConfig.java b/spring-security-mvc-ldap/src/main/java/org/baeldung/security/SecurityConfig.java index 97bbaf8302..c9bb5b74ae 100644 --- a/spring-security-mvc-ldap/src/main/java/org/baeldung/security/SecurityConfig.java +++ b/spring-security-mvc-ldap/src/main/java/org/baeldung/security/SecurityConfig.java @@ -15,37 +15,15 @@ import org.springframework.stereotype.Controller; @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"); - - } - - @Override - protected void configure(HttpSecurity http) throws Exception { + @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"); + } - http - .authorizeRequests() - .antMatchers("/", "/home").permitAll() - .anyRequest().authenticated(); - http - .formLogin() - .loginPage("/login") - .permitAll() - .and() - .logout() - .logoutSuccessUrl("/"); + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated(); + http.formLogin().loginPage("/login").permitAll().and().logout().logoutSuccessUrl("/"); + } - - } - }