【问题标题】:Configuring Spring Boot Security to use BCrypt password encoding in Grails 3.0在 Grails 3.0 中配置 Spring Boot Security 以使用 BCrypt 密码编码
【发布时间】:2015-08-10 00:50:04
【问题描述】:

在 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 spring-security spring-boot bcrypt grails-3.0


    【解决方案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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 2019-10-26
      • 2016-01-01
      • 2021-08-29
      • 2015-01-10
      • 2016-01-23
      • 2021-03-20
      • 2020-05-26
      • 2018-10-23
      相关资源
      最近更新 更多