How to get AdditionaInformation List From OAuth2

青春壹個敷衍的年華 提交于 2019-12-11 08:12:50

问题


I set an additional information by using OAuth2AccessToken enhance. I can see the additional information in the token but how can I get that list in my services class?

public final class CustomTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(
            OAuth2AccessToken accessToken,
            OAuth2Authentication authentication) {
        Map<String, Object> additionalInfo = new HashMap<>();

        List<String> companies = new ArrayList<>();
        companies.add("Company 1");
        companies.add("Company 2");
        companies.add("Company 3");

        additionalInfo.put("companies", companies);

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

I tried to get authentication from security context and cascade it to Oauth2Authentication but that object doesn't have additional information list.

SecurityContext securityContext = SecurityContextHolder.getContext();
OAuth2Authentication oauth = (OAuth2Authentication)securityContext.getAuthentication();

回答1:


This is how I fetched additional info named department:

@PreAuthorize("hasAuthority('ROLE_ACCOUNTS') and #oauth2.hasScope('READ')")
@GetMapping()
public List<Account> getAll(OAuth2Authentication principal) {

    OAuth2AuthenticationDetails auth2AuthenticationDetails = (OAuth2AuthenticationDetails) principal.getDetails();
    Map<String, Object> details = tokenStore.readAccessToken(auth2AuthenticationDetails.getTokenValue()).getAdditionalInformation();
    String department= (String) details.get("department");
    return accountService.getAllAccounts(department);
}


来源:https://stackoverflow.com/questions/49895552/how-to-get-additionainformation-list-from-oauth2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!