首页 > 解决方案 > 我们可以在编写自定义身份验证提供程序时添加密码编码器吗?

问题描述

我是 Spring Security 的新手。我正在尝试编写我的自定义身份验证提供程序。它可以工作,但我找不到与它一起使用密码编码器的选项。

请指导。

我的代码位于https://github.com/payalbnsl/MvcSecurityCustomAuthenticationProvider

它是一个基本的 spring mvc 应用程序,只有一个使用我的自定义身份验证提供程序编写的 usernamepasswordtoken 类型的端点。我想在其中添加密码编码器。可能吗?

标签: spring-security

解决方案


您可以在客户提供商中创建密码编码器,而不是将编码器注入您自己的身份验证提供商。

但我认为没有必要写你自己的客户提供商,因为我读了你的CustomAuthenticationProvider 这很简单,你可以使用JdbcUserDetailsManager

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.jdbcAuthentication()
           .dataSource(datasource)
           .usersByUsernameQuery("select username,password,1 "+ "from xxx " + "where username = ?")
           .authoritiesByUsernameQuery("select username, role "+ "from xxx " + "where username = ?")
           .passwordEncoder(passwordEncoder())
}

推荐阅读