春のセキュリティのOAuth2が出现マップされたPasswordEncoderありません

問題の説明

コンフィギュレーションは、メモリベースのストレージトークン、認証サーバを運ぶPOSTリクエストを使用して、認証コードに従って成功した場合、サーバはエラーメッセージを返し
バージョン
2.1.3.RELEASE:春ブート
スプリングセキュリティ:5.1.4.RELEASE
ログ

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

ソリューション

インターフェイス用の5.0以前のバージョン春のセキュリティPasswordEncoder NoOpPasswordEncoderデフォルト実装は、この時点で使用できるパスワードが5.0後に暗号化された形式で格納されている必要がありDelegatingPasswordEncoderにテキストのパスワードは、デフォルトの実装クラスは明らかです。

application.yml

削除spring.security設定、変更

spring:
  application:
    name: oauth2-server

server:
  port: 8080

WebSecurityConfiguration

package com.funtl.oauth2.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        // 设置默认的加密方式
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        auth.inMemoryAuthentication()
                // 在内存中创建用户并为密码加密
                .withUser("user").password(passwordEncoder().encode("123456")).roles("USER")
                .and()
                .withUser("admin").password(passwordEncoder().encode("123456")).roles("ADMIN");

    }
}

AuthorizationServerConfiguration

package com.funtl.oauth2.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    // 注入 WebSecurityConfiguration 中配置的 BCryptPasswordEncoder
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("client")
                // 还需要为 secret 加密
                .secret(passwordEncoder.encode("secret"))
                .authorizedGrantTypes("authorization_code")
                .scopes("app")
                .redirectUris("http://www.funtl.com");

    }
}

テストアクセス

ポストマンの要求またはCURLことで

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=authorization_code&code=1JuO6V' "http://client:secret@localhost:8080/oauth/token"

ここに画像を挿入説明
以下のように回答得られた結果は以下の通りでした。

{
    "access_token": "016d8d4a-dd6e-4493-b590-5f072923c413",
    "token_type": "bearer",
    "expires_in": 43199,
    "scope": "app"
}
公開された45元の記事 ウォンの賞賛1 ビュー1048

おすすめ

転載: blog.csdn.net/lqq404270201/article/details/104414927