【问题标题】:Spring boot basic auth always gives 401 errorSpring Boot基本身份验证总是给出401错误
【发布时间】:2018-04-18 21:28:24
【问题描述】:

我已经通过在 pom.xml 中添加依赖项来配置 Spring Boot 安全性

 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>

还有以下类:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(final HttpSecurity http) throws Exception {
  http.authorizeRequests().antMatchers(HttpMethod.GET, "/api").authenticated().and().httpBasic();
http.csrf().disable();
  }
}

我还在 yml 中添加了这些属性:

security:
 user: user
 Password: user

我正在使用邮递员, 我要保护的 URL 是:

http://localhost:8080/api

并且正在添加授权:类型是基本身份验证,用户名是用户,密码是用户也被添加

但每次我访问此 URL 时,都会收到 401 未经授权的错误。

任何帮助都很好

【问题讨论】:

  • 你的 yml 文件在哪里?我倾向于使用以下结构进行基本身份验证: http.authorizeRequests() .antMatchers("/api").authenticated() .anyRequest().permitAll() .and() .csrf().disable();
  • 感谢我尝试了类似的方法,它有效

标签: http spring-boot basic-authentication


【解决方案1】:

请更正您的application.yml 中的设置,如下所示:

spring:
  name:
    user: user
    password: user

顺便说一句,如果没有正确读取配置,在启动应用程序后,您可能会在控制台输出中看到以下消息:

使用默认安全密码:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2018-12-15
    • 2017-05-04
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多