私は複数のロール(ROLE1、ROLE2)を持つ安全なスプリングブートアプリケーションを持っています。一方のユーザーは両方の役割を持ち、もう一方のユーザーは1つの役割しか持ちません。ログインが成功すると、ユーザーはリンク先ページに送られます。ユーザーが1つの役割しか持たない場合、要素を無効にします。ロールに基づいてHTML要素を無効/有効にするSpringboot + Thymeleaf
私はthymeleaf-extras-springsecurity3を試しましたが、成功しませんでした。これは私のコードです:(すべてのオプションを試してみました)
のpom.xml
...
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity3</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
...
landing.html
!${currentUser.user.hasAuthority('ROLE1')}
!${#authorization.expression('hasRole('ROLE1')')}
${#authentication.getPrincipal().getUser().getRoles()}
${#authentication.getPrincipal().getRoles()}
しかし、成功しません、私はいつも、そのオブジェクトにnullエラーが発生しますこのように
org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method getPrincipal() on null context object
ご協力いただければ幸いです!ありがとう!