【发布时间】: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