What is the correct way to use enum java

0

How can I use enum as roles to authenticate in a java web app?

public enum Role 
{
  ROLE_ADMIN,ROLE_USER;
}

I am using the spring boot framework and thus the roles are valid:

@GetMapping("/home")
public String defaultAfterLogin(HttpServletRequest request)
{  
    if(request.isUserInRole("ROLE_ADMIN") 
    {
        return "redirect:/admin";
    }
    return "redirect:/user";
}
    
asked by goku venz 26.09.2017 в 17:07
source

1 answer

0

The correct form is of the

Public enum Role {

ROLE_ADMIN("ROLE_ADMIN"), ROLE_USER("ROLE_USER");
private String valor;

private Role(String valor) {
    this.valor = valor;
}
public String getValor() {
    return valor;
}

public void setValor(String valor) {
    this.valor = valor;
}

}

Now to use the value of any position in it is used like this

   String valores= Role.ROLE_ADMIN.getValor();
    
answered by 27.09.2017 / 23:10
source