【问题标题】:Spring boot security - inMemoryAuthentication not workingSpring Boot 安全性 - inMemoryAuthentication 不起作用
【发布时间】:2019-08-12 05:06:09
【问题描述】:

这是我的安全配置代码:

@EnableWebSecurity
@EnableGlobalMethodSecurity (
        prePostEnabled=true
    )
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.inMemoryAuthentication()
                .withUser("chandra").password("{noop}1234").roles("USER").and()
                .withUser("admin").password("{noop}admin123").roles("ADMIN");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/addItem","/delete").hasRole("ADMIN")
                .antMatchers("/getAllItems").hasRole("USER")
                .and().csrf().disable().headers().frameOptions().disable()
                .and()
                .formLogin();
    }
}

但是在编译 spring 时仍然为我生成密码。

Using generated security password: 49f04bde-ac1f-4e30-870b-ba0dd93d50f3

我通过打印语句检查了配置是否正在加载,发现安全配置正在加载。是否应该进行任何更改以使其与给定的用户 ID 和密码一起使用。

提前致谢。

【问题讨论】:

  • 您是否尝试使用指定用户登录?
  • 是的,在我尝试过的邮递员中并且正在加载表单,所以我删除了 .and.formLogin() 并使用我的凭据('chandra','1234')再次尝试,但被禁止了 403。@安德鲁萨沙
  • 嗨,我现在面临同样的问题。这个问题有公认的答案或解决方案吗?
  • enter link description here你应该设置密码编码

标签: spring-boot spring-security


【解决方案1】:

只是关于纯文本密码的默认提示,您可以想象接下来会发生什么。 :-)

无论如何,出于测试目的,您可以在 @Configuration 类中定义一个像这样的无操作密码编码器:

要使您的 noopworking 前置,请务必公开以下 bean:

@Bean
public PasswordEncoder passwordEncoder() {
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

如需更多信息,请咨询各自的chapter in the reference manual。 作为替代方案,您可以提供良好的旧密码管理器本身:

import org.springframework.security.crypto.password.PasswordEncoder;

[…]

@Bean
public NoOpPasswordEncoder passwordEncoder() {
    return NoOpPasswordEncoder.getInstance();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-18
    • 2018-05-06
    • 2021-12-27
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2011-07-03
    相关资源
    最近更新 更多