quick formatting work
This commit is contained in:
parent
89d3f72333
commit
a910ebae44
@ -15,13 +15,13 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
|||||||
@ComponentScan("org.baeldung")
|
@ComponentScan("org.baeldung")
|
||||||
public class SampleLDAPApplication extends WebMvcConfigurerAdapter {
|
public class SampleLDAPApplication extends WebMvcConfigurerAdapter {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SampleLDAPApplication.class, args);
|
SpringApplication.run(SampleLDAPApplication.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addViewControllers(ViewControllerRegistry registry) {
|
public void addViewControllers(ViewControllerRegistry registry) {
|
||||||
registry.addViewController("/login").setViewName("login");
|
registry.addViewController("/login").setViewName("login");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -4,7 +4,6 @@ import java.security.Principal;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@ -20,55 +19,52 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
@Controller
|
@Controller
|
||||||
public class MyController {
|
public class MyController {
|
||||||
|
|
||||||
@RequestMapping("/")
|
@RequestMapping("/")
|
||||||
public String init(Map<String, Object> model, Principal principal) {
|
public String init(Map<String, Object> model, Principal principal) {
|
||||||
model.put("title", "PUBLIC AREA");
|
model.put("title", "PUBLIC AREA");
|
||||||
model.put("message", "Any user can view this page");
|
model.put("message", "Any user can view this page");
|
||||||
model.put("username", getUserName(principal));
|
model.put("username", getUserName(principal));
|
||||||
model.put("userroles", getUserRoles(principal));
|
model.put("userroles", getUserRoles(principal));
|
||||||
return "home";
|
return "home";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping("/secure")
|
@RequestMapping("/secure")
|
||||||
public String secure(Map<String, Object> model, Principal principal) {
|
public String secure(Map<String, Object> model, Principal principal) {
|
||||||
model.put("title", "SECURE AREA");
|
model.put("title", "SECURE AREA");
|
||||||
model.put("message", "Only Authorised Users Can See This Page");
|
model.put("message", "Only Authorised Users Can See This Page");
|
||||||
model.put("username", getUserName(principal));
|
model.put("username", getUserName(principal));
|
||||||
model.put("userroles", getUserRoles(principal));
|
model.put("userroles", getUserRoles(principal));
|
||||||
return "home";
|
return "home";
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getUserName(Principal principal){
|
private String getUserName(Principal principal) {
|
||||||
|
if (principal == null) {
|
||||||
|
return "anonymous";
|
||||||
|
} else {
|
||||||
|
|
||||||
if(principal == null){
|
final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
|
||||||
return "anonymous";
|
Collection<? extends GrantedAuthority> authorities = currentUser.getAuthorities();
|
||||||
}else{
|
for (GrantedAuthority grantedAuthority : authorities) {
|
||||||
|
System.out.println(grantedAuthority.getAuthority());
|
||||||
|
}
|
||||||
|
return principal.getName();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
|
private Collection<String> getUserRoles(Principal principal) {
|
||||||
Collection<? extends GrantedAuthority> authorities = currentUser.getAuthorities();
|
if (principal == null) {
|
||||||
for(GrantedAuthority grantedAuthority : authorities) {
|
return Arrays.asList("none");
|
||||||
System.out.println(grantedAuthority.getAuthority());
|
} else {
|
||||||
}
|
|
||||||
return principal.getName();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Collection<String> getUserRoles(Principal principal){
|
Set<String> roles = new HashSet<String>();
|
||||||
|
|
||||||
if(principal == null){
|
final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
|
||||||
return Arrays.asList("none");
|
Collection<? extends GrantedAuthority> authorities = currentUser.getAuthorities();
|
||||||
}else{
|
for (GrantedAuthority grantedAuthority : authorities) {
|
||||||
|
roles.add(grantedAuthority.getAuthority());
|
||||||
Set<String> roles = new HashSet<String>();
|
}
|
||||||
|
return roles;
|
||||||
final UserDetails currentUser = (UserDetails) ((Authentication) principal).getPrincipal();
|
}
|
||||||
Collection<? extends GrantedAuthority> authorities = currentUser.getAuthorities();
|
}
|
||||||
for(GrantedAuthority grantedAuthority : authorities) {
|
|
||||||
roles.add(grantedAuthority.getAuthority());
|
|
||||||
}
|
|
||||||
return roles;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,37 +15,15 @@ import org.springframework.stereotype.Controller;
|
|||||||
@Controller
|
@Controller
|
||||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(AuthenticationManagerBuilder auth)
|
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
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");
|
||||||
|
}
|
||||||
|
|
||||||
auth.ldapAuthentication()
|
@Override
|
||||||
.userSearchBase("ou=people")
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
.userSearchFilter("(uid={0})")
|
http.authorizeRequests().antMatchers("/", "/home").permitAll().anyRequest().authenticated();
|
||||||
.groupSearchBase("ou=groups")
|
http.formLogin().loginPage("/login").permitAll().and().logout().logoutSuccessUrl("/");
|
||||||
.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("/");
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user