Spring Security - pobranie roli

0

Cześć,

Mam pytanie dotyczące Spring Security. Chciałbym mieć kontroler, który zwraca listę ról aktualnie zalogowanego użytkownika.

auth.inMemoryAuthentication()
                .withUser("user")
                .password("password")
                .roles("USER", "TEST")
                .authorities("DOMENA1", "DOMENA2");

Taki użytkownik przykładowo. Teraz co ważne, napisałem coś takiego jak niżej ale dostaje listę authorities a nie ról :/

public Collection<? extends GrantedAuthority> getUserRoles(Authentication authentication) {
        Collection<? extends GrantedAuthority> authorities = null;
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            authorities = authentication.getAuthorities();
        }
        return authorities;
    }

Czy ktoś wie jak dostać listę RÓL?

0

Rola to Authority z przedrostkiem "ROLE_" przy wykorzystywaniu domyślnego RoleVotera. Z tego co wiem (chociaż nigdy nie wykorzystywałem w praktyce), role mogą być związane z bardziej zaawansowaną logiką przy podejmowaniu decyzji o udzieleniu dostępu. W twoim przypadku prawdopodobnie powinienieś zwrócić listę Authorities, ponieważ to one bardziej jednoznacznie opisują stan użytkownika.

Spring Security has a voter-based architecture which means that an access decision is made by a series of AccessDecisionVoters. The voters act on the "configuration attributes" which are specified for a secured resource (such as a method invocation). With this approach, not all attributes may be relevant to all voters and a voter needs to know when it should ignore an attribute (abstain) and when it should vote to grant or deny access based on the attribute value. The most common voter is the RoleVoter which by default votes whenever it finds an attribute with the "ROLE_" prefix. It makes a simple comparison of the attribute (such as "ROLE_USER") with the names of the authorities which the current user has been assigned. If it finds a match (they have an authority called "ROLE_USER"), it votes to grant access, otherwise it votes to deny access.

The prefix can be changed by setting the rolePrefix property of RoleVoter. If you only need to use roles in your application and have no need for other custom voters, then you can set the prefix to a blank string, in which case the RoleVoter will treat all attributes as roles.

http://docs.spring.io/spring-security/site/docs/4.1.2.RELEASE/reference/htmlsingle/#appendix-faq-role-prefix

1 użytkowników online, w tym zalogowanych: 0, gości: 1