[英]Jhipster gateway Full authentication is required to access this resource
[英]Jhipster new Roles - Full authentication is required
我有一個問題,即在使用自定義定義的角色登錄帳戶時無法訪問任何 url。
為了為我的應用程序創建新角色,我在AuthoritiesConstants
類和authorities.csv
添加了我的新角色。 然后我在我的 h2 數據庫中手動插入了我想要的新角色: ROLE_STUDENT
和ROLE_PROFESOR
。
然后我登錄管理員帳戶並嘗試並成功創建了一個具有ROLE_STUDENT
的新用戶。 然后我登錄到這個新帳戶並嘗試訪問http://localhost:9000/api/users
以獲取http://localhost:9000/api/users
的完整列表。 我收到以下錯誤:
2020-03-19 10:59:05.687 DEBUG 13892 --- [ XNIO-1 task-15] base.aop.logging.LoggingAspect : Enter: base.repository.CustomAuditEventRepository.add() with argument[s] = [AuditEvent [timestamp=2020-03-19T08:59:05.686Z, 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-03-19 10:59:05.691 DEBUG 13892 --- [ XNIO-1 task-15] base.aop.logging.LoggingAspect : Exit: base.repository.CustomAuditEventRepository.add() with result = null
2020-03-19 10:59:05.693 WARN 13892 --- [ XNIO-1 task-15] o.z.problem.spring.common.AdviceTraits : Unauthorized: Full authentication is required to access this resource
2020-03-19 10:59:05.695 WARN 13892 --- [ XNIO-1 task-15] .m.m.a.ExceptionHandlerExceptionResolver : Resolved [org.springframework.security.authentication.InsufficientAuthenticationException: Full authentication is required to access this resource]
在我的SecurityConfiguration
類中,這個 url 屬於.antMatchers("/api/**").authenticated()
。 所以我應該能夠從任何帳戶訪問它,只要我登錄。
令我沮喪的是,除了主頁之外,我似乎無法從此帳戶訪問任何 URL。 我手動檢查了我的數據庫以查看用戶是否已創建並具有正確的角色。 一切都很好。 有人可以幫我解決這個問題嗎?
您還必須打開通往新角色的路線,這是在客戶端完成的。 如果您使用 angular,這或多或少是這樣的。
正如您在文件home.route.ts
看到的那樣,home 組件對任何人開放。
export const HOME_ROUTE: Route = {
path: '',
component: HomeComponent,
data: {
authorities: [], // <- Empty, so anyone can access the home
pageTitle: 'home.title'
}
};
另一方面,如果您想授予對常規組件中新角色的訪問權限,則必須將其添加到[entity-name].route.ts
的有效權限數組中。
export const fooRoute: Routes = [
{
...
data: {
authorities: ['ROLE_STUDENT', 'ROLE_PROFESOR'],
...
},
...
這可以訪問具有ROLE_STUDENT
或ROLE_PROFESOR
任何用戶,但不是普通用戶(只有ROLE_USER
)。 這只是一個例子。
無論如何,如果我正確理解了您的問題,那么您是在嘗試直接在瀏覽器中訪問api/...
映射。 這不是一個好主意,它失敗是件好事,因為客戶端通常會向大多數請求添加內容,以便服務器正確處理和驗證它們( XSRF
, auth token
,...)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.