Rest 来自本机邮递员应用程序的Spring安全授权请求
我正在探索/学习通过RESTAPI实现Spring安全模块 为了测试影响,我们使用Postman本地应用程序作为rest客户端Rest 来自本机邮递员应用程序的Spring安全授权请求,rest,spring-security,postman-native-app,Rest,Spring Security,Postman Native App,我正在探索/学习通过RESTAPI实现Spring安全模块 为了测试影响,我们使用Postman本地应用程序作为rest客户端 @RestController @RequestMapping("/auth") public class Employee { @GetMapping("/status") public ResponseEntity<String> getStatus() { Respons
@RestController
@RequestMapping("/auth")
public class Employee {
@GetMapping("/status")
public ResponseEntity<String> getStatus()
{
ResponseEntity<String> responseEntity = new ResponseEntity<>("Resource is fetched", HttpStatus.OK);
return responseEntity;
}
}
现在,当在浏览器中尝试时,上面的授权代码工作正常-它使用默认的spring登录页面。
然而,我不能完全理解如何通过postman执行/测试相同的操作
在方法保护的void configureHttpSecurity http中,我尝试删除formLogin,但它不起作用。
我添加了httpBasic——它也不起作用
在postman中,我使用基本身份验证。
在互联网上搜索时,我看到了一些非常好的文章,但几乎所有的文章都使用了一些UI技术,如angular或thymleaf来演示我发现很难掌握的概念
下面是学习spring安全性的视频教程
提前谢谢!
Ashish Parab您必须为同一个应用程序实现JWT令牌,并将其添加到Postman中的请求头“Authorization”中。您可以在youtube上观看Java Brains Spring安全视频。您必须为该视频实现JWT令牌,并将其添加到Postman中的请求头“授权”中。您可以在youtube上观看Java Brains Spring安全视频 执行GET请求http://localhost:8080/login 通过邮递员,它将返回一个html。从响应中提取_csrf令牌。看起来像 按如下方式进行POST请求,以便http://localhost:8080/login,将_csrf令牌、用户名和密码复制为表单参数 注意第二步响应中的JESSIONID Cookie值。这是经过身份验证的会话的会话Id 只要您在后续请求中以cookie的形式发送JESSIONID,spring security就会知道您是谁。邮递员会自动将Cookie添加到后续请求中 您可以将其手动添加为带有该cookie标头的标头,或更新邮递员设置以始终发送JESSIONID cookie 执行GET请求http://localhost:8080/login 通过邮递员,它将返回一个html。从响应中提取_csrf令牌。看起来像 按如下方式进行POST请求,以便http://localhost:8080/login,将_csrf令牌、用户名和密码复制为表单参数 注意第二步响应中的JESSIONID Cookie值。这是经过身份验证的会话的会话Id 只要您在后续请求中以cookie的形式发送JESSIONID,spring security就会知道您是谁。邮递员会自动将Cookie添加到后续请求中 您可以将其手动添加为带有该cookie标头的标头,或更新邮递员设置以始终发送JESSIONID cookie
@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();
}
}
<input name="_csrf" type="hidden"
value="1c470a6c-dff3-43aa-9d08-d308545dc880" />