假设我们有一个使用Spring MVC和Spring Security配置的API端点.我们希望能够处理成对的@RequestMapping和@Secured注释,其中唯一的@Secured注释值因配对而异.这样,我们就可以根据同一请求的安全规则返回不同的响应主体.
这可以通过避免直接检查方法体中的安全规则来允许我们的代码更易于维护.
有一个不成功的例子,这是我们想要做的:
@Controller
@RequestMapping("/api")
public class Controller {
@Secured ({"ROLE_A"})
@RequestMapping(value="{uid}", method=RequestMethod.GET)
@ResponseBody
public Response getSomething(@PathVariable("uid") String uid) {
// Returns something for users having ROLE_A
}
@Secured ({"ROLE_B"})
@RequestMapping(value="{uid}", method=RequestMethod.GET)
@ResponseBody
public Response getSomethingDifferent(@PathVariable("uid") String uid) {
// Returns something different for users having ROLE_B
}
}
Run Code Online (Sandbox Code Playgroud)
我们怎样才能做到这一点?如果可以这样做:如何为同时拥有ROLE_A和ROLE_B的用户管理优先级?
我正在尝试创建一个函数,它应该将所有参数作为连接的char数组返回.不幸的是,我在使用该函数时遇到了"无效指针"错误.我是C的新手,所以也许我以这种方式使用realloc是错误的.
char* concat(int argc, ...) {
char* result;
va_list args;
va_start(args, argc);
int i;
for (i = 0; i < argc; i++) {
char* s = va_arg(args, char*);
int length = (result) ? strlen(result) : 1;
char* tmp = (char*)realloc(result, sizeof(char) * (strlen(s) + length - 1));
if (tmp == NULL) {
throw_error("Realloc failed in `concat`.");
}
result = tmp;
memcpy(&(result[length-1]), s, strlen(s));
printf("result: %s\n", s);
}
va_end(args);
return result;
}
Run Code Online (Sandbox Code Playgroud)
错误消息,如果它可以帮助:
*** glibc detected *** ./pascc: realloc(): invalid pointer: …
Run Code Online (Sandbox Code Playgroud)