package com.diagbot.config;

import com.diagbot.entity.SysUserUaa;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: token生成携带的信息
 * @author: gaodm
 * @time: 2018/9/3 15:16
 */
public class CustomTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        final Map<String, Object> additionalInfo = new HashMap<>();
        SysUserUaa user = (SysUserUaa) authentication.getUserAuthentication().getPrincipal();
        additionalInfo.put("user_id", user.getId());
        additionalInfo.put("hosp_id", user.getHospitalId());
        //		additionalInfo.put("authorities", user.getAuthorities());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
        return accessToken;
    }

}
