我正在通过 REST API 实现它来探索/学习 Spring 安全模块。
为了测试影响,我们使用 Postman 本机应用程序作为休息客户端。
@RestController
@RequestMapping("/auth")
public class Employee {
@GetMapping("/status")
public ResponseEntity<String> getStatus()
{
ResponseEntity<String> responseEntity = new ResponseEntity<>("Resource is fetched", HttpStatus.OK);
return responseEntity;
}
}
上面是一块资源以供消费。下面是配置身份验证和授权的代码片段
@EnableWebSecurity
public class AppSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("ashish").password("{noop}admin").roles("USER")
.and().withUser("foo").password("{noop}foo").roles("ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/auth/status").hasRole("ADMIN").and()
.formLogin()
;
}
@Bean
public PasswordEncoder getPasswordEncoder()
{
return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}
}
现在,在浏览器中尝试时,上面的授权代码工作正常 - 它使用其默认的弹簧登录页面。但是我不太明白如何通过邮递员执行/测试。
在方法 protected void configure(HttpSecurity http) 中,我尝试删除 formLogin() 它不起作用。我添加了 httpBasic - 它也没有工作。
在互联网上搜索时,我遇到了一些非常好的文章,但几乎所有文章都使用某种 UI 技术,如 angular 或 thymleaf 来展示我发现难以掌握的概念。
我指的是下面的视频教程来学习弹簧安全性。
https://www.youtube.com/watch?v=payxWrmF_0k&list=PLqq-6Pq4lTTYTEooakHchTGglSvkZAjnE&index=6&t=0s
提前致谢!阿什帕拉布