List of usage examples for org.springframework.security.web.authentication.switchuser SwitchUserFilter setSwitchFailureUrl
public void setSwitchFailureUrl(String switchFailureUrl)
From source file:de.whs.poodle.security.SpringSecurityConfig.java
@Bean public SwitchUserFilter switchUserFilter() { SwitchUserFilter filter = new SwitchUserFilter(); filter.setTargetUrl("/"); filter.setSwitchUserUrl("/switchUser"); filter.setExitUserUrl("/exitUser"); filter.setSwitchFailureUrl("/?switchUserFailed=1"); /*// ww w. j a v a 2s . c o m * Called when a user is switched and returns the UserDetails. */ filter.setUserDetailsService(username -> { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); /* If no username is specified, we interpret this as "student mode" * (see <form> in instructor/navItems.html). */ if (username.isEmpty()) { // get the logged in student Instructor instructor = instructorRepo.getByUsername(auth.getName()); log.debug("{} switched to student mode", instructor.getUsername()); // create the fake student and switch Student fakeStudent = studentRepo.createFakeStudent(instructor.getId()); ArrayList<GrantedAuthority> authorities = new ArrayList<>(); authorities.add(new SimpleGrantedAuthority("ROLE_STUDENT")); authorities.add(new SimpleGrantedAuthority("ROLE_FAKE_STUDENT")); return new User(fakeStudent.getUsername(), "password", authorities); } else { // switch to specified user (admins only) boolean isAdmin = auth.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ADMIN")); if (!isAdmin) throw new ForbiddenException(); log.debug("User {} switching to {}", auth.getName(), username); ArrayList<GrantedAuthority> authorities = new ArrayList<>(); /* * username is the user that we switched to. We have no information * on whether he is a student or an instructor. Since he must be * in the database, let's just check there. */ if (studentRepo.studentExists(username)) authorities.add(new SimpleGrantedAuthority("ROLE_STUDENT")); else if (instructorRepo.exists(username)) authorities.add(new SimpleGrantedAuthority("ROLE_INSTRUCTOR")); else throw new UsernameNotFoundException("user doesn't exist."); return new User(username, "password", authorities); } }); return filter; }