Spring Security OAuth 2.0 と JWT を使用して、認証サーバーにパスワード付与を実装しました。AuthorizationServerConfigurerAdapter を拡張してサーバーを作成します。サーバーにユーザー名/パスワードを与えて、JWT トークンを取り戻すことができます。他のサービスの ResourceServerConfigurerAdapter クラスを拡張する ResourceConfiguration を使用して、Web サービス呼び出しが行われるたびに JWT を検証します。以下に貼り付けたのは私のコードです。
ネイティブ モバイル アプリに、Facebook、Gmail、Linked などを使用してログインできるようにしたいと考えています。添付の記事の手順に従いたいと思います。
https://ole.michelsen.dk/blog/social-signin-spa-jwt-server.html
- ユーザーはモバイル側で OAuth ダンスを実行し、使用しているソーシャル サービスのアクセス トークンを私に送信します。
- アクセス トークンを受け取り、それを使用してそれぞれのソーシャル サービスを呼び出します。
- トークンが有効な場合、ユーザーはログインできず、エラーがスローされます。
- トークンが有効な場合、ソーシャル サービスからユーザーの詳細を取得し、それを使用して既存または新規のシステム ユーザーに関連付けられる「ソーシャル ユーザー」をデータ ストアに作成します。
- システム ユーザーがソーシャル ユーザーと共に作成されるか、ソーシャル ユーザーが既存のシステム ユーザーに関連付けられると、JWT トークンがモバイル アプリに送り返されます。
- この JWT トークンは、Spring Security OAuth 2.0 パスワード付与フローによって作成された JWT トークンに似ている必要があり、ユーザーの承認時に ResourceServerConfiguration によって受け入れられる必要があります。
基準を満たすソリューションをオンラインで検索しましたが、見つかりません。私の要件は合理的ですか?JWTトークンを取得しながら、ユーザーがユーザー名/パスワードおよびソーシャルメディア認証を介してサインインできるようにする、これを行う簡単な方法はありますか? 私が見つけた 1 つの例では、OAuth2ClientAuthenticationProcessingFilter を使用して上記のロジックを実行していますが、OAuth2ClientAuthenticationProcessingFilter がどのように機能するかはわかりませんし、ドキュメントも見つかりません。同様の技術スタックを使用して同様の要件を実装する必要があった場合は、このソリューションを実装するために使用した手法を教えてください。
認証サーバー:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Value("${clientId}")
private String clientId;
@Value("${clientSecret}")
private String clientSecret;
@Value("${jwtSigningKey}")
private String jwtSigningKey;
@Value("${accessTokenValiditySeconds}")
private String accessTokenValiditySeconds;
@Value("${refreshTokenValiditySeconds}")
private String refreshTokenValiditySeconds;
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public JwtAccessTokenConverter accessTokenConverter(){
JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
accessTokenConverter.setSigningKey(jwtSigningKey);
return accessTokenConverter;
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public TokenEnhancer tokenEnhancer() {
return new JWTTokenEnhancer();
}
// Added for refresh token capability
@Bean
@Primary
public DefaultTokenServices tokenServices(){
final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
return defaultTokenServices;
}
@Override
public void configure(final ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient(clientId)
.secret(clientSecret)
.authorizedGrantTypes("password", "refresh_token")
.scopes("read","write")
.accessTokenValiditySeconds(Integer.valueOf(accessTokenValiditySeconds)) // 1 hour
.refreshTokenValiditySeconds(Integer.valueOf(refreshTokenValiditySeconds));// 30 days
}
@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
// Add the JWT token enhancer to the token enhancer chain then add to endpoints
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhancer(), accessTokenConverter()));
endpoints.tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancerChain)
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService)
.allowedTokenEndpointRequestMethods(HttpMethod.GET,HttpMethod.POST)
.accessTokenConverter(accessTokenConverter());
}
@Override
public void configure(final AuthorizationServerSecurityConfigurer securityConfigurer) throws Exception {
securityConfigurer.checkTokenAccess("permitAll()");
super.configure(securityConfigurer);
}
}
public class JWTTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(final OAuth2AccessToken accessToken,
final OAuth2Authentication authentication) {
Map<String, Object> additionalInfo = new HashMap<>();
// Get the user detail implementation
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
// add userId and roles to the JWT token
additionalInfo.put("user_id", userDetails.getUserId());
additionalInfo.put("email", userDetails.getEmail());
additionalInfo.put("user_name", userDetails.getUsername());
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
return accessToken;
}
}
各マイクロサービスで:
@Configuration
@EnableResourceServer
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
@ComponentScan("com.test.security")
@Profile({"prod", "qa", "dev"})
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Value("${jwtSigningKey}")
private String jwtSigningKey;
// http security concerns
@Override
public void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/swagger-ui.html").permitAll()
.antMatchers("/hystrix/**").permitAll()
.antMatchers("/admin/hystrix.stream/**").permitAll()
.antMatchers("/admin/health/**").permitAll()
.antMatchers("/admin/info/**").permitAll()
.antMatchers("/admin/**").authenticated()
.antMatchers("/greetings/**").authenticated()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and().csrf().disable();
}
@Override
public void configure(final ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
accessTokenConverter.setSigningKey(jwtSigningKey);
return accessTokenConverter;
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
// Added for refresh token capability
@Bean
@Primary
public DefaultTokenServices tokenServices() {
final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(true);
return defaultTokenServices;
}
}