liujie
2026-07-30 fc767bf18e262b724c78b5f860b6d908d4993dcb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.tourguide.security;
 
import com.tourguide.modules.user.domain.ClientType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
 
import java.util.Collections;
import java.util.Arrays;
import java.util.List;
 
public class AppUserPrincipal {
    private final Long userId;
    private final String username;
    private final String role;
    private final ClientType clientType;
 
    public AppUserPrincipal(Long userId, String username, String role, ClientType clientType) {
        this.userId = userId;
        this.username = username;
        this.role = role;
        this.clientType = clientType;
    }
    public Long getUserId() { return userId; }
    public String getUsername() { return username; }
    public String getRole() { return role; }
    public ClientType getClientType() { return clientType; }
    public List<GrantedAuthority> getAuthorities() {
        if (clientType == ClientType.ADMIN && !"ADMIN".equals(role)) {
            return Arrays.<GrantedAuthority>asList(
                    new SimpleGrantedAuthority("ROLE_ADMIN"),
                    new SimpleGrantedAuthority("ROLE_" + role));
        }
        return Collections.<GrantedAuthority>singletonList(new SimpleGrantedAuthority("ROLE_" + role));
    }
}