第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

你能在Spring中為@PreAuthorize設置一個動態(tài)值嗎?

你能在Spring中為@PreAuthorize設置一個動態(tài)值嗎?

慕森卡 2023-11-10 17:23:06
現(xiàn)在我用@PreAuthorize("hasAuthority('CREATE_USER_PRIVILEGE')")但我希望 CREATE_USER_PRIVILEGE 來自函數(shù)()。這可能嗎?
查看完整描述

3 回答

?
瀟瀟雨雨

TA貢獻1833條經(jīng)驗 獲得超4個贊

你可以這樣做:


@RestController

class FooController {


    @PreAuthorize("hasAuthority(@securityService.privilege)")

    @GetMapping("/")

    public ResponseEntity<String> helloSecurity(@RequestParam("id") Integer id){

        return ResponseEntity.ok("Hello World");

    }



}


@Service("securityService")

class SecurityService {


    public String getPrivilege(){

        return "CREATE_USER_PRIVILEGE";

    }


}


查看完整回答
反對 回復 2023-11-10
?
繁星coding

TA貢獻1797條經(jīng)驗 獲得超4個贊

基于上述解決方案,我實現(xiàn)了如下內(nèi)容:


@Controller

class TestController {


    //calling a PreAuthorize on method level/ can be used on class level as well

    @PreAuthorize("hasAnyAuthority(@authorityService.authorities)")

    @RequestMapping("/application")

    public ModelAndView newPage() throws{

        return new ModelAndView(view);

   }

   

}


@Service("authorityService")

class AuthorityService{


    @Value("${app.authorities}") // read roles from properties file

    private String authorities;

    

    public List<String> getAuthorities(){

        // convert the comma separated Strings to list.

        List<String> items = Arrays.asList(authorities.split("\\s*,\\s*")); 

        return items;

    }

    

}


查看完整回答
反對 回復 2023-11-10
?
波斯汪

TA貢獻1811條經(jīng)驗 獲得超4個贊

您必須首先使用構(gòu)造函數(shù)或注釋自動裝配您的服務,然后您可以使用 Spel 語言來使用它,如下例所示

@RequestMapping(value="/id/{domainObjectId}/dostuff", method=RequestMethod.POST, produces="application/json")

@PreAuthorize(value="hasRole('ROLE_DomainObjectAdmin') or @domainObjectServiceImpl.findDomainObject(#domainObjectId).getOwners().contains(#userAccount.getEmployee())")

public String setObjectiveComplete(@PathVariable String domainObjectId, UserAccount userAccount) {

// Do stuff

}


查看完整回答
反對 回復 2023-11-10
  • 3 回答
  • 0 關注
  • 236 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號