Spring Boot 2.0.0 + OAuth2

后端 未结 4 1880
迷失自我
迷失自我 2020-12-07 01:55

Does Spring Boot 2 + Sping Security OAuth2 still support the @AuthorizationServer annotation? From reading the release notes some things haven\'t been ported ov

4条回答
  •  感情败类
    2020-12-07 02:19

    OAuth2 AuthorizationServer uses basic authentication.

    So, you also need to encode your client secret with delegatedPasswordEncoder in AuthorizationServerConfig to completely solve "There is no PasswordEncoder mapped for the id "null" " exception.

    Yao Liu's answer solved my problem.

    1) created a bean to auto wire PasswordEncoder;

    @Bean
    public PasswordEncoder passwordEncoder() {
        String idForEncode = "bcrypt";
        Map encoderMap = new HashMap<>();
        encoderMap.put(idForEncode, new BCryptPasswordEncoder());
        return new DelegatingPasswordEncoder(idForEncode, encoderMap);
    }
    

    2) Auto wired passwordEncoder in AuthorizationServerConfig class;

    @Autowired
    private PasswordEncoder passwordEncoder;
    

    3) encoded CLIENT_SECRET with passwordEncoder.

    @Override
    public void configure(ClientDetailsServiceConfigurer configurer) throws Exception {
        configurer
             .inMemory()
             .withClient(CLIENT_ID)
             .secret(passwordEncoder.encode(CLIENT_SECRET))
             .authorizedGrantTypes(GRANT_TYPE_FOR_LOGIN, GRANT_TYPE_FOR_REFRESH)
             .scopes(SCOPE_READ, SCOPE_WRITE)
             .accessTokenValiditySeconds(TOKEN_VALIDITY_SECONDS)
             .refreshTokenValiditySeconds(TOKEN_VALIDITY_SECONDS)
             .resourceIds(RESOURCES_IDS);
    }
    

    That's it.

提交回复
热议问题