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";
}
}

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;
}
}

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
}
添加回答
舉報