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

时间:2015-05-27 18:54:42

标签: 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 /弹簧/ resources.groovy

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

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

1 个答案:

答案 0 :(得分: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);
    }
}