Spring Oauth2. DaoAuthenticationProvider 中未设置密码编码器

2024-05-11

我对 Spring Oauth 和 Spring Security 很陌生。我正在尝试在我的项目中使用 client_credentials 流程。现在,我设法使用自己的 CustomDetailsS​​ervice 来从系统中已存在的数据库中获取 client_id 和密码(秘密)。唯一的问题是我无法更改 AuthorizationServer 使用的 DaoAuthenticationProvider 中的密码编码器 - 它默认设置为 PlaintextPasswordEncoder。我无法以它使用 SHAPasswordEncoder 的方式配置它。它始终使用明文编码器。我可能不太了解流程,因为我是 Spring 的新手。

这是我的一些代码(DaoAuthenticationProvider 的配置不起作用):

安全配置.java

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private static final String RESOURCE_ID = "restservice";

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/register/**");

}

@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(daoAuthenticationProvider());
}

@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
    daoAuthenticationProvider.setUserDetailsService(userDetailsService());
    daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
    return daoAuthenticationProvider;
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new ShaPasswordEncoder();
}

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private MyCustomClientDetailsService myCustomClientDetailsService;

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

    @Bean
    public ResourceServerTokenServices defaultTokenServices() {
        final DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setSupportRefreshToken(true);
        defaultTokenServices.setTokenStore(tokenStore());
        return defaultTokenServices;
    }

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

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.withClientDetails(myCustomClientDetailsService);
    }

    @Bean
    public MyCustomClientDetailsService detailsService() {
        return new MyCustomClientDetailsService();
    }
}

@Configuration
@EnableResourceServer
protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    ...
}
}

以及自定义 ClientDetailsS​​ervice 类:

public class MyCustomClientDetailsService implements ClientDetailsService {

@Autowired
private UserService userService;

@Override
public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {

    User fan = userService.getFan(clientId);

    if (fan == null) {
        throw new NoSuchClientException("No client with requested id: " + clientId);
    } 

    BaseClientDetails details = new BaseClientDetails(clientId, restservice, "write", "client_credentials", "USER");

    details.setClientSecret(fan.getEncodedPassword()); 

    return details;
}
}

从我的 UserService 获取的encodedPassword 始终是一个错误的凭据,因为 DaoAuthenticationProvider 默认设置了 PlaintextPasswordEncoder。

我在那里缺少什么? 是否可以在用于检查凭据的 DaoAuthenticationProvider 中设置密码编码器?或者我是否必须编写自己的 AuthenticationProvider,以便按照我想要的方式检查它?


我发现问题的解决方案是覆盖configure on AuthorizationServerConfigurerAdapter

@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
    oauthServer.passwordEncoder(passwordEncoder);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Oauth2. DaoAuthenticationProvider 中未设置密码编码器 的相关文章

随机推荐

  • 将文本数据作为表单中的文件发布

    是否可以从 html 表单中发布一些作为文件输入类型的字符串的 XML 数据 情况是我有一个像这样的表格 form action target php method post enctype multipart form data gt
  • OpenCL 何时使用全局、私有、本地、常量地址空间

    我正在尝试学习 OpenCL 但我很难决定使用哪些地址空间 因为我只找到组装的资源声明这些地址空间是什么 但没有声明它们为什么存在或何时使用它们 资源至少太分散了 所以带着这个问题我希望把所有这些信息汇总一下 所有地址空间是什么 它们为什么
  • jquery 中 DOM 元素的手动垃圾回收是否可以提高浏览器性能?

    在性能范围内 删除不再需要的元素是否有意义 或者浏览器是否对代码中未进一步引用的 dom 元素执行自动垃圾收集 some element fadeOut 1000 function el el remove lt does this mak
  • 如何知道我的 Xcode iPhone 项目是否使用 ARC?

    我想知道我的 Xcode iPhone 项目是否正在使用 ARC 但我不记得在创建项目时是否勾选了该框 我怎样才能得到这些信息 选择您的项目 然后构建设置 寻找Objective C 自动引用计数 in the Apple LLVM 编译器
  • 为单个列表注册事件处理程序

    我有一个共享点事件处理程序 我想为单个列表激活它 而不是站点中的所有列表 我该怎么办 得到答案了 我们需要运行这段代码 也许在控制台应用程序中 不过 我仍然不知道如何在添加事件处理程序后将其删除 string siteUrl Console
  • 如何更改Python使用的SQLite版本?

    我在 Debian 9 12 上安装了 Python 3 8 和 SQLite 3 16 2 并且需要升级到较新版本的 SQLite 我已经下载并编译了 SQLite 网站上提供的合并 并将其放入 usr bin 所以当我这样做时 sqli
  • 通过列计数拆分时重复表头

    我正在 Magento 中输出产品列表 作为包装在表格中的简单列表 由于此列表可能会很长 100 个以上产品 因此我使用了来自这里的想法 https stackoverflow com questions 21001803 how to h
  • Solr PatternReplaceCharFilterFactory 未替换为指定模式

    所以我对 Solr 很陌生 但我尝试使用 PatternReplaceCharFilterFactory 对将存储的电话号码字符串进行一些预处理 这是该字段的配置
  • 在 xcode 中,有没有办法验证所有 NSLocalizedStrings 的密钥?

    除了运行其中包含 NSLocalizedString 的每个代码路径之外 是否有一种方法可以验证所有 NSLocalizedString 是否都具有实际存在于所有捆绑包的所有 Localized strings 文件中的密钥 例如 一个键中
  • FluentAssertions ShouldNotThrow 无法识别异步方法/Func

    我正在尝试检查异步方法是否抛出具体异常 为此 我使用 MSTEST 和 FluentAssertions 2 0 1 我已经检查过这个关于 Codeplex 的讨论 http fluentassertions codeplex com wo
  • 在 try 中使用零合并运算符? for 抛出并返回可选值的函数

    我想在以下两种情况下使用 nil coalescing 运算符设置默认值 函数抛出错误 函数返回 nil 请看一下下面的代码片段 我有以下问题 为什么 item1 为零 item1和item2的初始化有什么区别 enum VendingMa
  • 自定义 php 论坛 - 显示新的/未读的帖子

    我自己使用 php 编写了一个自定义论坛脚本 我决定不使用 phpbb 和其他工具 因为我希望我所做的事情具有 100 的灵活性 不过我遇到了一个问题 如何向用户显示帖子是否是新的 未读的 我想到了两种解决方案 1 饼干 2 数据库 我不想
  • mocha——手表和猫鼬模型

    如果我让 mocha 监视更改 每次保存文件时 mongoose 都会抛出以下错误 OverwriteModelError 无法覆盖Client模型一旦编译 我知道猫鼬不允许两次定义模型 但我不知道如何让它与mocha watch clie
  • 如何调试.NET Windows Service OnStart方法?

    我用 NET 编写的代码仅在作为 Windows 服务安装时才会失败 该故障甚至不允许服务启动 我不知道如何进入 OnStart 方法 如何 调试 Windows 服务应用程序 http msdn microsoft com en us l
  • 如何在查询中生成序列号?

    我们使用 PostgreSQL v8 2 3 如何在查询输出中生成序列号 我想显示查询返回的每一行的序列号 例子 SELECT employeeid name FROM employee 我希望生成并显示从一开始的每一行的序列号 你有两个选
  • 如何使用 javascript 禁用组合键?

    I would like to disable view source shortcut key for IE using JavaScript To disable Ctrl C I am using the following func
  • 超慢的表格布局性能

    我遇到了糟糕的 TableLayout 性能 我在这里读过一些帖子 谈论同样的事情 Android 动态创建表 性能不佳 https stackoverflow com questions 9813427 android dynamical
  • pdflatex: \includegraphics{} -> 找不到文件

    首先 我知道这个问题已经存在了成百上千次 但我在过去四个小时内找到的给出的答案都没有解决我的具体问题 我在这里变得疯狂 我将非常感谢任何帮助和建议 尝试编译一个非常简单的 tex 文件 其中包括 包括图形命令 我最终收到 文件未找到 错误
  • AS3 [Event(name="", type="")],有什么意义?

    我使用 FlashDevelop3 R2 和 Flex 3 3 SDK 进行开发 在很多情况下我必须使用嵌入元数据标签 如下所示 Embed source path to file private var Asset Class 我很好地理
  • Spring Oauth2. DaoAuthenticationProvider 中未设置密码编码器

    我对 Spring Oauth 和 Spring Security 很陌生 我正在尝试在我的项目中使用 client credentials 流程 现在 我设法使用自己的 CustomDetailsS ervice 来从系统中已存在的数据库