gpt4 book ai didi

java - Spring Oauth2 增强权限

转载 作者:行者123 更新时间:2023-12-02 13:42:52 25 4
gpt4 key购买 nike

我正在从事一个项目,其中我有一个人在不同的租户中担任多个角色。所以我想自定义权限来获取该角色的角色和所有权,例如。

"authorities": [ 
{
"authority": "ROLE_store",
"ownership": "sec 18"
}
]

Spring 代码:

private List<GrantedAuthority> getGrantedAuthoritiesForStore(){
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
authorities.add(new SimpleGrantedAuthority("ROLE_"+"store"));
System.out.print("authorities :"+authorities);
return authorities;
}

最佳答案

这个解决方案解决了我的问题,它的结果几乎与我想要的相同。

public class CustomTokenEnhancer implements TokenEnhancer {

@Autowired
EmployeeService employeeService;

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

User user = (User) authentication.getPrincipal();
String username = user.getUsername();

List<Map<String, Object>> credential = employeeService.fetchEmployeesRoleCredentials(username);

final Map<String, Object> additionalInfo = new HashMap<>();

additionalInfo.put("credentials", credential);
// additionalInfo.put("authorities", user.getAuthorities());

((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);

return accessToken;

}

}

关于java - Spring Oauth2 增强权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42633548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com