我有以下示例:
无论如何控制器:
@Controller
@RequestMapping(value = "api/whatever")
public class WhateverController {
@Autowired private WhateverService whateverService;
@RequestMapping(value = "/list", method = GET)
@Secured({ "ROLE_WHATEVER_CANSEARCH" })
@ResponseBody
public List<WhateverDTO> findList(@RequestParam(value = "values") String[] values) {
return whateverService.findThings(values);
}
}
Run Code Online (Sandbox Code Playgroud)
任何服务:
@Service
public class WhateverService {
@Autowired private WhateverDAO whateverDAO;
public List<WhateverDTO> findThings(String[] values) {
//...
validate();
return whateverDAO.findThings(values);
}
@Secured({ "ROLE_SPECIFICPERMISSION" }) // Throws AccessDeniedException
private void validate() {
if(thing) throw new RuntimeException("You can't...");
}
}
Run Code Online (Sandbox Code Playgroud)