Reputation: 738
I am using spring security for authentication. I created a custom user who extends from User
,then I used it in my customServiceDetails (implementation of UserDetailsService
) , I want to be able to get my custom user, so I tried to do this :
public class MyUser extends User implements Serializable {
private static final long serialVersionUID = 1L;
private String uuid;
public MyUser(String username, String password, boolean enabled,
boolean accountNonExpired, boolean credentialsNonExpired,
boolean accountNonLocked,
Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, authorities);
}
public MyUser(String username, String password, String uuid,
boolean enabled, boolean accountNonExpired,
boolean credentialsNonExpired, boolean accountNonLocked,
Collection<? extends GrantedAuthority> authorities) {
super(username, password, enabled, accountNonExpired,
credentialsNonExpired, accountNonLocked, authorities);
this.uuid = uuid;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
}
I tried to get the current user by doing this but it's not working :
(MyUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
I wonder if there is any suggestion ?
Upvotes: 1
Views: 904
Reputation: 738
I am using Zk framework so the solution was like that :
MyUser user =(MyUser)((UsernamePasswordAuthenticationToken) Executions.getCurrent().getUserPrincipal()).getPrincipal();
i get the current custom user from UsernamePasswordAuthenticationToken
Upvotes: 0
Reputation: 7199
Try this
16.3.3 Defining @RequestMapping handler methods
http://docs.spring.io/spring/docs/4.0.x/spring-framework-reference/html/mvc.html
java.security.Principal
containing the currently authenticated user.
@Secured({"ROLE_REGULAR_USER","ROLE_ADMIN"})
@RequestMapping(value = "/context")
public ModelAndView get(Pricipal principal) {
String username = principal.getName();
// Continue processing...
return null;
}
Upvotes: 1