【问题标题】:Basic authentification ignoring password validation忽略密码验证的基本身份验证
【发布时间】:2019-07-22 00:59:12
【问题描述】:

我配置WebSecurityConfig,在内存中创建用户

@Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("username")
                .password(passwordEncoder().encode("password"))
                .authorities("READ_ORDERS")
                .roles("USER");
    }

配置 WebSecurityConfig

@Configuration
    @Order(1)
    public static class BasicAuthenticationAdapter extends WebSecurityConfigurerAdapter {

        private final AuthenticationEntryPoint authEntryPoint;

        @Autowired
        public BasicAuthenticationAdapter(AuthenticationEntryPoint authEntryPoint) {
            this.authEntryPoint = authEntryPoint;
        }

        protected void configure(HttpSecurity http) throws Exception {
            http
                    .antMatcher("/orders**")
                    .authorizeRequests()
                    .anyRequest().hasRole("USER")
                    .and()
                    .csrf().disable()
                    .httpBasic().authenticationEntryPoint(authEntryPoint)
                    .and()
                    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        }
    }

当我第一次尝试使用无效凭据进行授权时 - 401 异常,没关系。 但是授权成功后,当我使用无效的用户名和密码时, 我也授权了。 可能是什么问题?

【问题讨论】:

  • 您之前的用户还登录吗?你关闭会话了吗?
  • @Stultuske 我只是通过邮递员发送请求顺便说一句,我该如何检查?
  • 配置错误。需要在 antMather http .csrf().disable() .antMatcher("/orders/**") .authorizeRequests() .antMatchers("/orders/**").hasRole("READ_ORDERS")

标签: java spring-boot spring-security


【解决方案1】:

这就是基本身份验证的工作原理。只要您成功登录,就会始终发布有效的凭据。

Spring 安全性与 SessionCreationPolicy 一起使用,默认策略是 IF_REQUIRED。这意味着如果没有并且需要,spring 会创建会话。

为了解决您的问题,您必须更改此政策。

http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

重新启动服务器并重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-18
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多