0

This is the code to create a Principal object.

List<String> roles = new ArrayList();
roles.add(userRole);
this.principal = new GenericPrincipal(username,"***********",roles);

When I printed the principal object, I got [username(userRole,)]

Getting the name from principal

GenericPrincipal principal = (GenericPrincipal)request.getUserPrincipal();
String name = principal.getName();

Getting the roles from principal

GenericPrincipal principal = (GenericPrincipal)request.getUserPrincipal();
String[] roles = principal.getRoles();

When I printed the principal object, I got [username()]

I'm able to get the name from Principal but not the roles. The length of roles[] is 0.

I even tried getPassword() method. It returns null. Only the getName() method works correctly.

Why is the roles array empty and How do I get the role correctly?

Hulk Man
  • 153
  • 1
  • 15
  • There isn't enough information here to answer your question. What is the request object? Do you have the code for the getRoles() method? One thing you could try is changing roles to ArrayList roles = principal.getRoles(); – user8537453 Apr 24 '18 at 12:59
  • It is HttpServletRequest object and the getRoles() implementation is the default tomcat one. – Hulk Man Apr 24 '18 at 14:18
  • @user8537453 ArrayList didn't work too. – Hulk Man Apr 25 '18 at 12:24

0 Answers0