title: Rest Api 项目添加 Basic Auth 认证
date: 2022-10-10 22:09
tags: [Java,Spring Boot,Rest Api,Basic Auth]
一、前言
一直浅显的认为Basic Auth是一种过时的,不安全的认证方式。但是最近接触IBPM的Rest Api,就是使用的Basic Auth认证。在此学习一下如何在通过Spring Security在Rest Api项目中使用Basic Auth认证。
二、步骤
2.1 添加spring-boot-starter-security依赖
// pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
2.2 配置Spring Security
创建一个继承了_WebSecurityConfigurerAdapter_的类:
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception{
http
.csrf().disable()
.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
@Autowired
public void configGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth
.inMemoryAuthentication()
.withUser("admin")
.password("{noop}password")
.roles("USER");
}
}
2.3 Postman调用