Spring Security 5-UsernamePasswordAuthenticationFilter和基本身份验证

时间:2020-05-25 06:52:27

标签: spring spring-security

我正在尝试为一个小型的API学校项目实现简单的安全性,这有点困惑和不知所措。我跟着 this blog post.

一切正常,我能够登录并收到jwt令牌。但是,当前登录是通过发送用户名和密码以及URL作为查询参数来执行的。我当然想避免这种事情。

我尝试将httpbasic添加到这样的安全配置中:

@Override
protected void configure(HttpSecurity http) throws Exception {
     http.cors().and().csrf().disable()
           .addFilter(new JwtAuthenticationFilter(authenticationManager(), jwtAudience, jwtIssuer, jwtSecret, jwtType))
           .authorizeRequests(authorizeRequests ->
                   authorizeRequests
                          .antMatchers("/board/**").hasAnyRole("MEMBER", "BOARD")
                          .antMatchers("/members/**").hasRole("MEMBER")
                          .anyRequest().authenticated()
           )
           .httpbasic().and().
           .sessionManagement()
           .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}

但是登录无法正常工作,在尝试与邮递员进行basicAuth时,我不断遭到未经授权的访问。

所以我的问题是:如何更改这些代码段的行为以接受基本身份验证,而不通过URL发送用户凭据?我也必须重写AttemptAuthentication方法吗?

0 个答案:

没有答案