package com.css.simulation.oauth.server.cofiguration.oauth;

import com.css.simulation.oauth.server.cofiguration.security.MyUserDetails;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Map;

@Component
public class MyUserAuthenticationConverter extends DefaultUserAuthenticationConverter {

    @Override
    public Map<String, ?> convertUserAuthentication(Authentication authentication) {
        Map<String, Object> response = new LinkedHashMap<>();
        MyUserDetails myUserDetails = (MyUserDetails) authentication.getPrincipal();
        //1 用户基本信息
        response.put("id", myUserDetails.getId());
        response.put("username", authentication.getName());
        response.put("roleCode", myUserDetails.getRoleCode());
        response.put("useType", myUserDetails.getUseType());
        response.put("createUserId", myUserDetails.getCreateUserId());
        //2 用户权限信息
        if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) {
            response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities()));
        }
        return response;
    }
}
