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我的应用程序作为依赖项加载:

构建.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)
   }
4

1 回答 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);
    }
}
于 2015-05-31T15:08:07.803 回答