springsecurity中使access_token获取用户信息(tokenStore)
public class TokenUtil {
/**
* 根据token获取用户名
*
*<hr>
* @author hanjidong
* @date 2020年12月16日 上午10:37:12
* @since 0.0.1
* @param access_token
* @return
* String
*/
public static SyswareUserDetails getUserNameByToken(String access_token) {
try {
//获取tokensotre对象
InMemoryTokenStore tokenStore = (InMemoryTokenStore) SystemContext.getBean("tokenStore");
OAuth2AccessToken readAccessToken = tokenStore.readAccessToken(access_token);
if(readAccessToken == null){
throw new IllegalArgumentException("token无效");
}
if(readAccessToken.isExpired()){
throw new RuntimeException("token已过期");
}
OAuth2Authentication oAuth2Authentication = null;
Field authenticationStore = tokenStore.getClass().getDeclaredField("authenticationStore");
authenticationStore.setAccessible(true);
Object authenticationStoreObj = authenticationStore.get(tokenStore);
if (authenticationStoreObj instanceof ConcurrentHashMap) {
ConcurrentHashMap<String, OAuth2Authentication> nticationStoreObjMap = (ConcurrentHashMap<String, OAuth2Authentication>) authenticationStoreObj;
oAuth2Authentication = nticationStoreObjMap.get(access_token);
}
Object principal = oAuth2Authentication.getPrincipal();
return (SyswareUserDetails) principal;
// //获取存储用户名与token的map
// // 暴力反射获取属性
// Field declaredField = tokenStore.getClass().getDeclaredField("userNameToAccessTokenStore");
// // 设置反射时取消Java的访问检查,暴力访问
// declaredField.setAccessible(true);
// Object nameObject = declaredField.get(tokenStore);
// if (nameObject instanceof ConcurrentHashMap) {
// ConcurrentHashMap<String, Collection<OAuth2AccessToken>> usernameTokenMap = (ConcurrentHashMap<String, Collection<OAuth2AccessToken>>) nameObject;
// Set<Entry<String, Collection<OAuth2AccessToken>>> entrySet = usernameTokenMap.entrySet();
// for (Entry<String, Collection<OAuth2AccessToken>> entry : entrySet) {
// Collection<OAuth2AccessToken> value = entry.getValue();
// for (OAuth2AccessToken oAuth2AccessToken : value) {
// if (oAuth2AccessToken.getValue().equals(access_token)) {
// return entry.getKey().split(":")[1];
// }
// }
// }
// }
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
@Component
public class SystemContext implements ApplicationContextAware{
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@SuppressWarnings("static-access")
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
/**
* 获取applicationContext
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 通过name获取 Bean.
* @param name
* @return
*/
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
/**
* 通过name,以及Clazz返回指定的Bean
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}