我正在使用spring编写一个RESTful web服务。我正在使用jwt承载令牌进行认证和授权。
下面是我的RestController
@RestController("api/v1/users")
public class UserController {
@Autowired
UserService userService;
@PostMapping
public User saveUser(@RequestBody User user) {
return userService.saveUser(user);
}
@GetMapping
public List<User> getUsers(@RequestParam(required = false) String pageNumber, String pageSize, String role, String status) {
return userService.findAll(pageNumber, pageSize, role, status);
}
}当我用请求-url访问api时
http://localhost:8080/api/v1/users?pageNumber=0&pageSize=6&role=admin它的工作很完美
但是,如果我将url端点更改为某个无效端点,如
http://localhost:8080/api/v1/hhh?pageNumber=0&pageSize=6&role=admin它仍然返回与第一个正确端点相同的结果。
下面是springframework调试日志记录中的一些日志语句
检查请求的匹配:'/api/v1/hhh';针对'/api/test/secureTest‘ 2019年-12-28 19:16:47.601调试5591 - nio-8080-exec-5 o.s.s.w.u.matcher.AntPathRequestMatcher :检查请求的匹配:'/api/v1/hhh';与‘api/身份验证’ 2019-12-28 19:16:47.601 DEBUG 5591 - nio-8080-exec-5 o.s.s.w.u.matcher.AntPathRequestMatcher :检查请求的匹配:'/api/v1/hhh';针对‘/api/v1/user/me’ 2019年-12-28 19:16:47.601调试5591 - nio-8080-exec-5 o.s.s.w.u.matcher.AntPathRequestMatcher :检查请求的匹配:'/api/v1/hhh';针对‘/api/v1/学生’ 2019年-12-28 19:16:47.601调试5591 - nio-8080-exec-5 o.s.s.w.u.matcher.AntPathRequestMatcher :检查请求的匹配:'/api/v1/hhh';针对‘/api/v1/教员’ 2019年-12-28 19:16:47.601调试5591 - nio-8080-exec-5 o.s.s.w.u.matcher.AntPathRequestMatcher :检查请求的匹配:'/api/v1/hhh';针对'/api/v1/admin‘ 2019年-12-28 19:16:47.601调试5591 - nio-8080-exec-5 o.s.s.w.u.matcher.AntPathRequestMatcher :检查请求的匹配:'/api/v1/hhh';针对‘/api/v1/用户’ 2019年-12-28 19:16:47.601调试5591 -- nio-8080-exec-5 o.s.s.w.a.i.FilterSecurityInterceptor :公共对象-身份验证未尝试 2019年-12-28 19:16:47.601调试5591 - nio-8080-exec-5 o.s.security.web.FilterChainProxy :o.s.security.web.FilterChainProxy:到达附加过滤链的末端;继续原始链 2019年-12-28 19:16:47.602痕量5591 - nio-8080-exec-5 o.s.web.servlet.DispatcherServlet : GET o.s.web.servlet.DispatcherServlet parameters={掩蔽},headers={蒙面} in DispatcherServlet 'dispatcherServlet‘ 2019年-12-28 19:16:47.602跟踪5591 - nio-8080-exec-5 o.s.b.f.s.DefaultListableBeanFactory :返回单例bean api/v1/的缓存实例 2019年-12-28 19:16:47.602跟踪5591 - nio-8080-exec-5 s.w.s.m.m.a.RequestMappingHandlerMapping :映射到public java.util.List java.util.List java.lang.String,java.lang.String,java.lang.String) 2019年-12-28 19:16:47.602跟踪5591 - nio-8080-exec-5 .w.s.m.m.a.ServletInvocableHandlerMethod :参数: 0,6,admin,null
我感觉Spring缓存端点url,并在没有找到匹配的情况下使用
知道怎么阻止这一切吗?
发布于 2019-12-28 20:17:21
如果您阅读了@RestController的api文档
您可以看到注释构造函数接受一个value,描述为:
该值可以指示逻辑组件名称的建议,在发生自动检测的组件时,该名称将被转换为Spring。
因此,它用于为创建vill的Bean设置名称。
这是,而不是像您所做的那样用来设置url映射的。
@RestController("api/v1/users")您需要用@RequestMapping对类进行注释,并将映射添加到@PostMapping和@GetMapping。
@RestController
@RequestMapping("/api/v1") // Add request mapping
public class FooBar {
@PostMapping("/users") // Add mapping here
public User bar() {
...
}
@GetMapping("/users") // Add mapping here
public List<User> foo() {
...
}
}https://stackoverflow.com/questions/59511618
复制相似问题