配置 Spring 引导安全以在 Grails 3.0 中使用 BCrypt 密码编码

Configuring Spring Boot Security to use BCrypt password encoding in Grails 3.0

在 Grails 3.0 中,如何指定 Spring Boot Security 应该使用 BCrypt 进行密码编码?

以下几行应该提供了我认为需要完成的事情的感觉(但我主要只是猜测):

import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder

PasswordEncoder passwordEncoder

passwordEncoder(BCryptPasswordEncoder)

我的应用程序加载 spring-boot-starter-security 作为依赖项:

build.gradle

dependencies {
   ...
   compile "org.springframework.boot:spring-boot-starter-security"

并且我使用以下方式为 userDetailsService 连接了一项服务:

conf/spring/resources.groovy

import com.example.GormUserDetailsService
import com.example.SecurityConfig

beans = {
   webSecurityConfiguration(SecurityConfig)
   userDetailsService(GormUserDetailsService)
   }

我在grails-app/conf/spring/resources.groovy

中有以下代码
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder

beans = {
    bcryptEncoder(BCryptPasswordEncoder)
}

我有一个 java 文件,它按照 spring-security 所述进行配置。 groovy应该也可以,但我是java.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    BCryptPasswordEncoder bcryptEncoder;

    @Autowired
    UserDetailsService myDetailsService

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            // userDetailsService should be changed to your user details service
            // password encoder being the bean defined in grails-app/conf/spring/resources.groovy
            auth.userDetailsService(myDetailsService)
                .passwordEncoder(bcryptEncoder);
    }
}