启动应用程序时,出现“未经授权:需要完全验证才能访问此资源”

时间:2020-10-09 07:29:59

标签: java reactjs spring-boot jhipster

自从我解决了原来的问题(就是这个问题)以来,这有点烦人,但是现在这是我无法完全调试的另一件事。

我正在为我的项目使用JHispter 6.10(Spring Boot 2.2.7.RELEASE)+ React。最近,我需要将实体用作目录(以便可以通过管理员轻松管理它们),并且需要在注册页面上使用它们。我的第一个问题是他们不会在注册页面中下拉菜单,但是这个问题与SecurityConfiguration.java有关,所以我将允许添加的实体添加到“全部”中:

.antMatchers("/api/comunidad-famdals").permitAll()
.antMatchers("/api/ciudads").permitAll()
.antMatchers("/api/estados").permitAll()
.antMatchers("/api/ladrillo-famdals").permitAll()
.antMatchers("/api/pais").permitAll()
.antMatchers("/api/**").authenticated()

这似乎很好用,但是第一次加载应用程序(在开发模式下)时,它会引发下一个错误:

2020-10-09 02:03:33.337 DEBUG 63312 --- [  XNIO-1 task-9] c.f.m.r.CustomAuditEventRepository       : Enter: add() with argument[s] = [AuditEvent [timestamp=2020-10-09T07:03:33.302498Z, principal=anonymousUser, type=AUTHORIZATION_FAILURE, data={details=org.springframework.security.web.authentication.WebAuthenticationDetails@b364: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: null, type=org.springframework.security.access.AccessDeniedException, message=Access is denied}]]
2020-10-09 02:03:33.342 DEBUG 63312 --- [  XNIO-1 task-9] c.f.m.r.CustomAuditEventRepository       : Exit: add() with result = null
2020-10-09 02:03:33.473  WARN 63312 --- [  XNIO-1 task-9] o.z.problem.spring.common.AdviceTraits   : Unauthorized: Full authentication is required to access this resource
2020-10-09 02:03:33.564  WARN 63312 --- [  XNIO-1 task-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource]

并且在尝试注册时,下拉列表仍然不显示任何内容:

Dropdown empty

但是,如果我再次回到家,终端将显示所有查询均已正确完成,如果返回到注册页面,果然可以肯定:

Dropdown now works

我想知道我的SecurityConfiguration.java上是否缺少某些东西,或者是否需要更改配置顺序才能使其正常工作。

1 个答案:

答案 0 :(得分:1)

您必须在 Spring Boot 配置 (SecurityConfiguration.java) 中授予端点权限

antMatchers 添加一个新的 HttpSecurity.authorizeRequests() 参数,它应该如下所示:

http
  .authorizeRequests()
  .antMatchers("/api/yourEndpoint").permitAll()

当然你必须选择谁有权调用这个端点

希望它对你有用:)