【发布时间】:2020-01-26 09:44:14
【问题描述】:
在我的 Spring Boot 应用程序中,我使用以下配置创建了一个 WebSecurityConfigurerAdapter:
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("student").password(passwordEncoder().encode("password")).roles("STUDENT")
.and()
.withUser("teacher").password(passwordEncoder().encode("admin")).roles("TEACHER");
}
我使用浏览器登录,并获得一个 Cookie 令牌,存储为 JSESSIONID。 然后我继续获取该cookie,并在邮递员中将其用作:
从 Postman 调用以下端点时:
@PostMapping(consumes = "application/json", produces = "application/json", path="/rest/class/{classId}/student")
@org.springframework.security.access.annotation.Secured({"ROLE_TEACHER"})
我得到 403 Forbidden(没有 cookie,我得到 401,可以理解)。
我也在使用以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
使用这些依赖,自动生成我的thymeleaf登录页面。
如何解决此身份验证/授权问题?
我需要禁用 csrf 吗?如果没有,我怎么能在我的邮递员请求中使用 csrf 令牌? 我尝试使用 X-CSRF-TOKEN 将其添加到 Postman 标头请求中,并在登录过程中使用发送到后端的 _csrf 令牌。 如果我禁用 csrf,那么登录页面甚至不会出现,并且我得到以下异常:
NotReadablePropertyException:无效的属性“principal.authorities” 豆类的
【问题讨论】:
-
希望我也知道如何使用邮递员或类似工具来做到这一点,喜欢。
标签: spring spring-boot spring-security thymeleaf postman