liujie
2026-09-14 82a8bc62d5b9d5d607f6952329952b17eda2f0a0
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.ArrayList;
import java.util.Collection;
import java.util.List;
 
public class AppUserPrincipal {
    private final Long userId;
    private final String username;
    private final String role;
    private final ClientType clientType;
    private final List<String> menuCodes;
 
    public AppUserPrincipal(Long userId, String username, String role, ClientType clientType) {
        this(userId, username, role, clientType, Collections.<String>emptyList());
    }
 
    public AppUserPrincipal(Long userId, String username, String role, ClientType clientType,
                            Collection<String> menuCodes) {
        this.userId = userId;
        this.username = username;
        this.role = role;
        this.clientType = clientType;
        this.menuCodes = new ArrayList<String>(menuCodes);
    }
    public Long getUserId() { return userId; }
    public String getUsername() { return username; }
    public String getRole() { return role; }
    public ClientType getClientType() { return clientType; }
    public List<String> getMenuCodes() { return Collections.unmodifiableList(menuCodes); }
    public List<GrantedAuthority> getAuthorities() {
        if (clientType == ClientType.ADMIN) {
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
            if (!"ADMIN".equals(role)) {
                authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
            }
            for (String menuCode : menuCodes) {
                authorities.add(new SimpleGrantedAuthority("PERM_" + menuCode));
            }
            return authorities;
        }
        return Collections.<GrantedAuthority>singletonList(new SimpleGrantedAuthority("ROLE_" + role));
    }
}