package com.kidgrow.authclient.converter; import com.kidgrow.common.model.SysUser; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.oauth2.provider.token.UserAuthenticationConverter; import org.springframework.util.StringUtils; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; /** * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
* * @Description: * 优化自org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter * jwt返回的principal改为返回SysUser,增加扩展字段
* @Project:
* @CreateDate: Created in 2020/2/13 10:46
* @Author: liuke */ public class CustomUserAuthenticationConverter implements UserAuthenticationConverter { private Collection defaultAuthorities; private UserDetailsService userDetailsService; /** * Optional {@link UserDetailsService} to use when extracting an {@link Authentication} from the incoming map. * * @param userDetailsService the userDetailsService to set */ public void setUserDetailsService(UserDetailsService userDetailsService) { this.userDetailsService = userDetailsService; } /** * Default value for authorities if an Authentication is being created and the input has no data for authorities. * Note that unless this property is set, the default Authentication created by {@link #extractAuthentication(Map)} * will be unauthenticated. * * @param defaultAuthorities the defaultAuthorities to set. Default null. */ public void setDefaultAuthorities(String[] defaultAuthorities) { this.defaultAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils .arrayToCommaDelimitedString(defaultAuthorities)); } @Override public Map convertUserAuthentication(Authentication authentication) { Map response = new LinkedHashMap(); response.put(USERNAME, authentication.getName()); if (authentication.getAuthorities() != null && !authentication.getAuthorities().isEmpty()) { response.put(AUTHORITIES, AuthorityUtils.authorityListToSet(authentication.getAuthorities())); } return response; } @Override public Authentication extractAuthentication(Map map) { if (map.containsKey(USERNAME)) { Object principal = map.get(USERNAME); Collection authorities = getAuthorities(map); if (userDetailsService != null) { UserDetails user = userDetailsService.loadUserByUsername((String) map.get(USERNAME)); authorities = user.getAuthorities(); principal = user; } else { Integer id = (Integer)map.get("id"); SysUser user = new SysUser(); user.setUsername((String)principal); user.setId(Long.valueOf(id)); principal = user; } return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities); } return null; } private Collection getAuthorities(Map map) { if (!map.containsKey(AUTHORITIES)) { return defaultAuthorities; } Object authorities = map.get(AUTHORITIES); if (authorities instanceof String) { return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities); } if (authorities instanceof Collection) { return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils .collectionToCommaDelimitedString((Collection) authorities)); } throw new IllegalArgumentException("Authorities must be either a String or a Collection"); } }