Настройка Spring Boot Security для использования кодировки пароля 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)
   }

person Dem Pilafian    schedule 27.05.2015    source источник


Ответы (1)


У меня есть следующий код в 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);
    }
}
person Julian Ooi    schedule 31.05.2015