其他分享
首页 > 其他分享> > 扩展 jwt 解决 oauth2 性能瓶颈

扩展 jwt 解决 oauth2 性能瓶颈

作者:互联网

oauth2 性能瓶颈

资源服务器的请求都会被拦截 到认证服务器校验合法性 (如下图)

check-token 过程中涉及的源码

扩展jwt 生成携带用户详细信息

  1.    @Bean

  2.    public TokenEnhancer tokenEnhancer() {

  3.        return (accessToken, authentication) -> {

  4.            if (SecurityConstants.CLIENT_CREDENTIALS

  5.                    .equals(authentication.getOAuth2Request().getGrantType())) {

  6.                return accessToken;

  7.            }


  8.            final Map<String, Object> additionalInfo = new HashMap<>(8);

  9.            PigxUser pigxUser = (PigxUser) authentication.getUserAuthentication().getPrincipal();

  10.            additionalInfo.put("user_id", pigxUser.getId());

  11.            additionalInfo.put("username", pigxUser.getUsername());

  12.            additionalInfo.put("dept_id", pigxUser.getDeptId());

  13.            additionalInfo.put("tenant_id", pigxUser.getTenantId());

  14.            additionalInfo.put("license", SecurityConstants.PIGX_LICENSE);

  15.            ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);

  16.            return accessToken;

  17.        };

  18.    }

重写默认的资源服务器处理行为

  1. @Slf4j

  2. public class PigxCustomTokenServices implements ResourceServerTokenServices {

  3.    @Setter

  4.    private TokenStore tokenStore;


  5.    @Setter

  6.    private DefaultAccessTokenConverter defaultAccessTokenConverter;


  7.    @Setter

  8.    private JwtAccessTokenConverter jwtAccessTokenConverter;


  9.    @Override

  10.    public OAuth2Authentication loadAuthentication(String accessToken) throws AuthenticationException, InvalidTokenException {

  11.        OAuth2Authentication oAuth2Authentication = tokenStore.readAuthentication(accessToken);

  12.        UserAuthenticationConverter userTokenConverter = new PigxUserAuthenticationConverter();

  13.        defaultAccessTokenConverter.setUserTokenConverter(userTokenConverter);

  14.        Map<String, ?> map = jwtAccessTokenConverter.convertAccessToken(readAccessToken(accessToken), oAuth2Authentication);

  15.        return defaultAccessTokenConverter.extractAuthentication(map);

  16.    }



  17.    @Override

  18.    public OAuth2AccessToken readAccessToken(String accessToken) {

  19.        return tokenStore.readAccessToken(accessToken);

  20.    }

  21. }

  1. /**

  2. * @author lengleng

  3. * @date 2019-03-17

  4. * <p>

  5. * jwt 转化用户信息

  6. */

  7. public class PigxUserAuthenticationConverter implements UserAuthenticationConverter {

  8.    private static final String USER_ID = "user_id";

  9.    private static final String DEPT_ID = "dept_id";

  10.    private static final String TENANT_ID = "tenant_id";

  11.    private static final String N_A = "N/A";


  12.    @Override

  13.    public Authentication extractAuthentication(Map<String, ?> map) {

  14.        if (map.containsKey(USERNAME)) {

  15.            Collection<? extends GrantedAuthority> authorities = getAuthorities(map);


  16.            String username = (String) map.get(USERNAME);

  17.            Integer id = (Integer) map.get(USER_ID);

  18.            Integer deptId = (Integer) map.get(DEPT_ID);

  19.            Integer tenantId = (Integer) map.get(TENANT_ID);

  20.            PigxUser user = new PigxUser(id, deptId, tenantId, username, N_A, true

  21.                    , true, true, true, authorities);

  22.            return new UsernamePasswordAuthenticationToken(user, N_A, authorities);

  23.        }

  24.        return null;

  25.    }


  26.    private Collection<? extends GrantedAuthority> getAuthorities(Map<String, ?> map) {

  27.        Object authorities = map.get(AUTHORITIES);

  28.        if (authorities instanceof String) {

  29.            return AuthorityUtils.commaSeparatedStringToAuthorityList((String) authorities);

  30.        }

  31.        if (authorities instanceof Collection) {

  32.            return AuthorityUtils.commaSeparatedStringToAuthorityList(StringUtils

  33.                    .collectionToCommaDelimitedString((Collection<?>) authorities));

  34.        }

  35.        throw new IllegalArgumentException("Authorities must be either a String or a Collection");

  36.    }

  37. }

  1. @Slf4j

  2. public class PigxResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {

  3.    @Override

  4.    public void configure(ResourceServerSecurityConfigurer resources) {

  5.        DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();

  6.        UserAuthenticationConverter userTokenConverter = new PigxUserAuthenticationConverter();

  7.        accessTokenConverter.setUserTokenConverter(userTokenConverter);


  8.        PigxCustomTokenServices tokenServices = new PigxCustomTokenServices();


  9.        // 这里的签名key 保持和认证中心一致

  10.        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();

  11.        converter.setSigningKey("123");

  12.        converter.setVerifier(new MacSigner("123"));

  13.        JwtTokenStore jwtTokenStore = new JwtTokenStore(converter);

  14.        tokenServices.setTokenStore(jwtTokenStore);

  15.        tokenServices.setJwtAccessTokenConverter(converter);

  16.        tokenServices.setDefaultAccessTokenConverter(accessTokenConverter);


  17.        resources

  18.                .authenticationEntryPoint(resourceAuthExceptionEntryPoint)

  19.                .tokenServices(tokenServices);

  20.    }

  21. }

使用JWT 扩展后带来的问题


标签:map,oauth2,String,accessToken,瓶颈,jwt,token,服务器,new
来源: https://blog.51cto.com/15016434/2646089