亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用@PreAuthorize 時,Spring SpelExpression

使用@PreAuthorize 時,Spring SpelExpression

慕標5832272 2022-11-30 14:37:40
我正在嘗試使用 @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 { }

我的每個控制器都必須有一個存儲角色的道具對象。這樣角色是可配置的。


查看完整回答
反對 回復 2022-11-30
  • 1 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號