I am new to Spring and my requirement is that I do not want to authenticate the user with username and password. The user is authenticate is some other application and my app get the request with folloing details:
- User name
- Roles
I just want use Spring Security to secure the pages according to the roles in the request. I've given a thought about writing UserDetailService, but that only add request-data, Spring still ask for authentication information. Then I thought about writing something like the following:
public class UserLogin {
/*
@Resource(name = "userDetailsService")
private UserDetailsService userDetailsService;
*/
@Resource(name = "authenticationManager")
private AuthenticationManager authenticationManager;
public boolean login(UserEntity user) {
//UserDetails ud = userDetailsService.loadUserByUsername(username);
Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (String role : user.getAuthorities()) {
authorities.add(new GrantedAuthorityImpl(role));
}
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), authorities);
try {
Authentication auth = authenticationManager.authenticate(token);
SecurityContext securityContext = new SecurityContextImpl();
// Places in ThredLocal for future retrieval
SecurityContextHolder.setContext(securityContext);
SecurityContextHolder.getContext().setAuthentication(auth);
} catch (AuthenticationException e) {
return false;
}
return true;
}
}
Am I going in the right direction. If so, how to configure the whole thing .. in spring-xml .