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

6
在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)
   }
1个回答

14

我在 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);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接