我将尝试简要解释我的应用程序架构的一部分,以便您了解我想要实现的目标。我认为这是一个非常常见的场景,但我不知道如何在 Springboot 中实现它。
我想要什么我想
使用 OAuth2 使用不同的提供商从我的 Angular FE 登录。在这种情况下,我使用的是在 Springboot (2.1.7.RELEASE) 中实现的授权服务器,并且我也在对 Google 帐户进行身份验证。一旦我从一个提供者那里获得了访问令牌,我想访问 BE 中的一些资源,但首先,必须验证访问令牌。
到目前为止
我所拥有的在我的 BE 中,我有以下内容AuthorizationServerConfig:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
private BCryptPasswordEncoder passwordEncoder;
private AuthenticationManager authenticationManager;
@Autowired
public AuthorizationServerConfig(BCryptPasswordEncoder passwordEncoder, AuthenticationManager authenticationManager) {
this.passwordEncoder = passwordEncoder;
this.authenticationManager = authenticationManager;
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("client_id")
.secret(passwordEncoder.encode("client_secret"))
.scopes("read", "write")
.authorizedGrantTypes("password", "refresh_token")
.accessTokenValiditySeconds(3600)
.refreshTokenValiditySeconds(3600);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.tokenStore(tokenStore())
.accessTokenConverter(accessTokenConverter());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
final JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
tokenConverter.setSigningKey("SigningKey_VALUE");
return tokenConverter;
}
}
然后我还有一个WebSecurityConfigurerAdapter使用userDetailsService来验证用户和一个UserDetailsService实现来获取用户。我之前提到的类的主要功能是什么可能是错误的,但这是一个配置,当我获得访问令牌时它正在工作,然后我在它发送到请求的授权标头中的 BE 时验证它。
附加到请求的每个访问令牌的验证部分都来自这个资源服务器(或者我认为它来自这个类,如果我错了,请纠正我):
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore());
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.cors()
.and()
.authorizeRequests()
.antMatchers("/api/security/oauth/**").permitAll()
.antMatchers("/api/usuarios/usuarios/**").hasAuthority("ADMINISTRADOR")
.anyRequest().authenticated()
.and()
.csrf().disable();
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
final JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
tokenConverter.setSigningKey("SigningKey_VALUE");
return tokenConverter;
}
@Bean
protected UrlBasedCorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin(CorsConfiguration.ALL);
config.addAllowedHeader(CorsConfiguration.ALL);
config.setAllowedMethods(Arrays.stream(HttpMethod.values()).map(HttpMethod::name).collect(Collectors.toList()));
source.registerCorsConfiguration("/**", config);
return source;
}
}
在我的 Angular FE 中,我能够使用 Google 帐户登录,因此我拥有来自 Google 响应的其他数据、访问令牌和 Id 令牌。我的问题是:
根据我目前的实现,如何开始验证 Google 获得的这些访问令牌?
任何帮助,将不胜感激。如果有人知道如何在 Springboot 中做到这一点,那将是最佳的,但我认为如果有人至少可以解释如何用其他语言解决这个问题,我可以让它工作。
谢谢!