我正在嘗試使用 @PreAuthorize 但需要將其配置為使不同的環境具有不同的訪問權限,但是當我嘗試使用 @PreAuthorize("hasAuthority(#bean)") 時,沒有任何內容被讀取到權限中。我試過使用#bean 和@bean,但都沒有用,但如果我有一個硬編碼字符串,它就可以正常工作。當使用#bean 時,沒有任何內容被傳入,而當使用@bean 時,我得到 No bean resolver registered in the context to resolve access to bean 'readRole' @Bean public String readRole() { return "Domain Users"; } @Target({ ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @PreAuthorize("hasAuthority(@readRole)") public @interface UserCanSeeRestricted { }然后在我的休息控制器中: @SecurityConfigExample.UserCanSeeRestricted @GetMapping("/all") public Collection<Office> getAllOffices() { return officeService.getAll(); }我希望能夠使用 readRole 根據環境返回不同的組,并讓 @PreAuthorize 讀取它們我嘗試了什么:public class SecurityConfig { public String readRole = "Domain Users"; } @Bean public SecurityConfig readRole() { return new SecurityConfig(); } @Target({ ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @PreAuthorize("hasAuthority(@readRole.readRole)") public @interface UserCanSeeRestricted { }--- 第二次更新: public class SecurityConfig { public String readRole() { return "Domain Users"; } } @Bean public SecurityConfig readRole() { return new SecurityConfig(); } @Target({ ElementType.METHOD, ElementType.TYPE }) @Retention(RetentionPolicy.RUNTIME) @PreAuthorize("hasAuthority(@readRole.readRole())") public @interface UserCanSeeRestricted { }我試著更換 @SecurityConfigExample.UserCanSeeRestricted和@PreAuthorize("hasAuthority(@readRole.readRole())")但它沒有用
1 回答

波斯汪
TA貢獻1811條經驗 獲得超4個贊
我找到的解決方案是:
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@PreAuthorize("hasAnyAuthority(#root.getThis().getProps().getWriteRole())")
public @interface UserCanEditRestricted { }
我的每個控制器都必須有一個存儲角色的道具對象。這樣角色是可配置的。
添加回答
舉報
0/150
提交
取消