7

JWTでSpring AuthorizationServerを実装しようとしています。私はJWTトークンを生成し、BCryptをミックスに追加するまでログインすることができました。今、私がログインしようとしているとき、私はAPIから「Bad credentials」を取得します。あなたの助けのためのOAuth2とJWTのスプリングセキュリティ:エンコードされたパスワードがBCryptのように見えない

OAuth2Configuration.java

@Configuration 
@EnableAuthorizationServer 
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter { 

    private DataSource dataSource; 
    private AuthenticationManager authenticationManager; 
    private BCryptPasswordEncoder passwordEncoder; 

    public OAuth2Configuration(AuthenticationManager authenticationManager) { 
     this.authenticationManager = authenticationManager; 
     this.dataSource = new Jdbc3PoolingDataSource(); 
     this.passwordEncoder = new BCryptPasswordEncoder(); 
    } 

    @Override 
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 
     security.passwordEncoder(passwordEncoder); 
    } 

    @Override 
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception { 
     clients.inMemory() 
       .withClient("api-client") 
       .secret("verysecretivesecret") 
       .scopes("READ", "WRITE", "DELETE") 
       .authorizedGrantTypes("implicit", "refresh_tokens", "password", "authorization_code"); 
    } 

    @Override 
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { 
     endpoints.authorizationCodeServices(authorizationCodeServices()) 
       .tokenStore(tokenStore()) 
       .tokenEnhancer(jwtTokenEnhancer()) 
       .authenticationManager(authenticationManager); 
    } 

    @Bean 
    public TokenStore tokenStore() { 
     return new JwtTokenStore(jwtTokenEnhancer()); 
    } 

    @Bean 
    protected JwtAccessTokenConverter jwtTokenEnhancer() { 
     return new JwtAccessTokenConverter(); 
    } 

    @Bean 
    protected AuthorizationCodeServices authorizationCodeServices() { 
     return new JdbcAuthorizationCodeServices(dataSource); 
    } 

} 

WebSecurityConfig.java

@Configuration 
class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    private AccountDetailsService accountDetailsService; 
    private BCryptPasswordEncoder passwordEncoder; 
    private DataSource dataSource; 

    WebSecurityConfig(AccountDetailsService accountDetailsService) { 
     this.accountDetailsService = accountDetailsService; 
     this.dataSource = new Jdbc3PoolingDataSource(); 
     this.passwordEncoder = new BCryptPasswordEncoder(); 
    } 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.userDetailsService(accountDetailsService).passwordEncoder(passwordEncoder).and().jdbcAuthentication().dataSource(dataSource); 
    } 
} 

SeedData.java

@Override 
public void run(String... args) throws Exception {  

    Stream.of("alan,test").map(x -> x.split(",")) 
      .forEach(tuple -> { 
       Account user = new Account(); 
       user.setUsername(tuple[0]); 
       user.setPassword(new BCryptPasswordEncoder().encode(tuple[1])); 
       user.setEmail(tuple[0]); 
       user.setRoles(Collections.singletonList(role)); 
       user.setActive(true); 
       this.accountRepository.save(user); 
      }); 
} 

感謝。

+0

データベースのパスワードは暗号化されていますか? – Jeff

+0

@ジェフはいそうです。 user.setPassword(new BCryptPasswordEncoder()。encode(tuple [1])); –

答えて

3

。誰かがそれを必要とするなら。

+0

共有ありがとう!本当に助けてくれました – mario

+0

あなたが私が間違っていることを教えてもらう機会はありますか?(こちら)(https://stackoverflow.com/questions/49138227/oauth-token-full-authentication-is-required-to-access-このリソース) - 私は同じ問題を抱えているようですが、その背後に別の原因があるようです。 – displayname

4

これは、WebSecurityとAuthorizationServerの両方にBCryptを適用したためです。したがって、BCryptの暗号化されたユーザーパスワードだけでなく、OAuth2の暗号化されたクライアント秘密をBCryptで保護する必要があります。私はこれがあなたがアプローチしようとしたものではなかったと思う。私はそれを動作させるために、以下の変更を行うために必要なあなたのコードの作業をするためには

、どちらかあなたの「verysecretivesecret」を暗号化

@Override 
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { 
     security.passwordEncoder(passwordEncoder); 
    } 

または手動での削除

+0

ええ、私は同じだと思ったので、私はそれを削除したが、それはどちらも動作しませんでした。 –

関連する問題