grails - 配置Spring Boot Security在Grails 3.0中使用BCrypt密码编码

标签 grails spring-security spring-boot bcrypt 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);
    }
}

关于grails - 配置Spring Boot Security在Grails 3.0中使用BCrypt密码编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30490862/

相关文章:

Grails 2.3.x : How to get plugin name when running plugin by itself?

grails - 使用RxJava在Grails 3中将RabbitMQ队列视为可观察到的

grails - Grails Criteria使用ilike查询列表

java - Spring Boot ConnectException 没有被捕获

spring-boot - 无法连接到 Eureka 服务器 - Spring Boot | Eureka | docker

java - 如何在两个实体之间使用 foreach

string - 什么函数用于格式化/替换 Grails/Groovy 中字符串中的 {0} {1} 参数?

java - Spring LDAP 3.1 自定义用户映射器

java - Spring Security中如何防止多次登录

java - 如何防止spring security在登录成功后重定向到上一页?