Postman测试SpringSecurity用户名和密码认证访问后台请求
@RestController
@RequestMapping("/api")
public class UserResource {
@RequestMapping("greeting")
public String test(){
return "hello";
}
}
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin(Customizer.withDefaults())
.authorizeRequests(
requset->requset.antMatchers("/api/greeting")
.authenticated());
}
}
1. 浏览器测试
1、启动后台项目,consoal控制台得到一个默认的登录密码
fd16e894-172e-4f20-976d-0b07fb7a2cbb
2、在浏览器访问请求:http://localhost:8080/login,得到一个登录页面,填入默认用户名和密码 fd16e894-172e-4f20-976d-0b07fb7a2cbb
,即可登录成功。
2. 使用Postman测试
1、postman发送登录请求:
2、新建一个文件index.html,将响应结果复制到文件中并使用浏览器打开
3、使用用户名和密码登录即可