本文共 3966 字,大约阅读时间需要 13 分钟。
详细性能瓶颈分析,请参考上篇文章
本文是针对传统使用UUID token 的情况进行扩展,提高系统的吞吐率,解决性能瓶颈的问题@Overridepublic OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException { MultiValueMapformData = new LinkedMultiValueMap (); formData.add(tokenName, accessToken); HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", getAuthorizationHeader(clientId, clientSecret)); // 调用认证服务器的check-token 接口检查token Map map = postForMap(checkTokenEndpointUrl, formData, headers); return tokenConverter.extractAuthentication(map);}
public OAuth2Authentication extractAuthentication(Mapmap) { Map parameters = new HashMap (); Set scope = extractScope(map); // 主要是 用户的信息的抽取 Authentication user = userTokenConverter.extractAuthentication(map); // 一些oauth2 信息的填充 OAuth2Request request = new OAuth2Request(parameters, clientId, authorities, true, scope, resourceIds, null, null, null); return new OAuth2Authentication(request, user); }
public Authentication extractAuthentication(Mapmap) { 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; } return new UsernamePasswordAuthenticationToken(principal, "N/A", authorities); } return null;}
/** * @author lengleng * @date 2019-03-07 ** 根据checktoken 的结果转化用户信息 */public class PigxUserAuthenticationConverter implements UserAuthenticationConverter { private static final String N_A = "N/A"; // map 是check-token 返回的全部信息 @Override public Authentication extractAuthentication(Map
map) { if (map.containsKey(USERNAME)) { Collection authorities = getAuthorities(map); String username = (String) map.get(USERNAME); Integer id = (Integer) map.get(SecurityConstants.DETAILS_USER_ID); Integer deptId = (Integer) map.get(SecurityConstants.DETAILS_DEPT_ID); Integer tenantId = (Integer) map.get(SecurityConstants.DETAILS_TENANT_ID); PigxUser user = new PigxUser(id, deptId, tenantId, username, N_A, true , true, true, true, authorities); return new UsernamePasswordAuthenticationToken(user, N_A, authorities); } return null; }}
public class PigxResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter { @Override public void configure(ResourceServerSecurityConfigurer resources) { DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter(); UserAuthenticationConverter userTokenConverter = new PigxUserAuthenticationConverter(); accessTokenConverter.setUserTokenConverter(userTokenConverter); remoteTokenServices.setRestTemplate(lbRestTemplate); remoteTokenServices.setAccessTokenConverter(accessTokenConverter); resources. .tokenServices(remoteTokenServices); }}
转载地址:http://occfm.baihongyu.com/