0

So I have class:

public class CustomUserDetailService implements UserDetailsService {

@Autowired
private EmployeeRepository employeeRepository;

@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    Employee employee = employeeRepository.readEmployee(s);
    if(employee != null) {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(employee.getRole().name()));
        //how to save employee login and user id????
        return new User(employee.getLogin(), employee.getPassword(),true, true, true, true, authorities)
    }else {
        throw new UsernameNotFoundException("Can't locate employee '" + s + "'");
    }
}
}

And class in which I use the stored data:

public class CurrentEmployeeParam {
public static Long getCurrentEmployeeId(){
    // how to get saved employee login and id???
    String employeeId = //something
    return Long.parseLong(employeeId);
}

So how to saved employee info and get employee info from any point in the program?

somebody
  • 1,077
  • 5
  • 14
  • 32
  • Here is the answer. http://stackoverflow.com/questions/18791645/how-to-use-session-attributes-in-spring-mvc – Junaid Nov 24 '14 at 08:11
  • @Junaid but I haven't `HttpServletRequest request` – somebody Nov 24 '14 at 08:12
  • Using Pojos you can do this like http://stackoverflow.com/questions/19699922/how-to-maintain-data-in-pojo-class-for-session-scope-spring-mvc-3-0-3 – Junaid Nov 24 '14 at 08:14

2 Answers2

0

if you are using Spring MVC you can use session scoped beans

prsutar
  • 429
  • 2
  • 4
  • 17
0

For example, can use:

public class AuthenticatedEmployee extends org.springframework.security.core.userdetails.User {

private Long employeeId;
private Long companyId;

public AuthenticatedEmployee(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, Collection<? extends GrantedAuthority> authorities)
        throws IllegalArgumentException {
    super(username, password, enabled, accountNonExpired, credentialsNonExpired, true, authorities);
}
// setters and getters
}

UserDetails class:

public class CustomUserDetailService implements UserDetailsService {

@Autowired
private EmployeeRepository employeeRepository;

@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    Employee employee = employeeRepository.readEmployee(s);
    if(employee != null) {
        List<GrantedAuthority> authorities =  new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(employee.getRole().name()));
        AuthenticatedEmployee user = new AuthenticatedEmployee(employee.getLogin(), employee.getPassword(), true, true, true, authorities);
        user.setEmployeeId(employee.getId());
        user.setCompanyId(employee.getCompany().getId());
        return user;
    }else {
        throw new UsernameNotFoundException("Can't locate employee '" + s + "'");
    }
}
}

And get employee info:

public class CurrentEmployeeParam {
public static Long getCurrentCompanyId() {
    return getAuthenticatedEmployee().getCompanyId();
}

public static Long getCurrentEmployeeId() {
    return getAuthenticatedEmployee().getEmployeeId();
}

private static AuthenticatedEmployee getAuthenticatedEmployee() {
    return (AuthenticatedEmployee) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
somebody
  • 1,077
  • 5
  • 14
  • 32