首页 > 解决方案 > 启动应用程序时“未经授权:访问此资源需要完全身份验证”

问题描述

这有点烦人,因为我已经解决了最初的问题(就是这个),但现在这是我自己无法调试的另一件事。

我正在为我的项目使用 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]

尝试注册时,下拉列表仍然没有显示任何内容:

下拉列表为空

但是如果我再次回家,终端显示所有查询都已正确完成,如果我回到注册页面,果然:

下拉菜单现在有效

我想知道我是否遗漏了某些东西,SecurityConfiguration.java或者配置的顺序是否需要不同才能正常工作。

标签: javareactjsspring-bootjhipster

解决方案


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

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

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

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

希望这对你有用 :)


推荐阅读