我使用 Spring 3.1.2 使用 Restful API(具有许多 url),并且希望对每个请求应用安全过滤器,该请求使用来自经过身份验证的用户“主体”的数据和请求发送的数据。
示例:
从请求网址验证 accountId:
http://www.foo.com/accountRecords?accountId=23
包含主体数据principal.accountId = 23
。
我知道这可以在 @requestMapping 方法内完成,但希望在实际方法外部执行此操作,因此它将是可维护和可配置的,而不会干扰业务逻辑。
但我没有找到任何关于它的建议\示例..
我相信它应该看起来像:
@PreAuthorize("principal.accountId == requestParam.accountId")
最佳答案
我认为您正在寻找过滤器来过滤请求和响应。在 Spring 中,您可以使用拦截器
来实现此目的。
public class MyInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession(false);
String requestParam = request.getParameter("your request param");
}
}
最后,您需要在 context.xml 文件中配置这些拦截器:
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.example.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
关于java - Spring security - 通过验证 @RequestParam 参数的原理来保护请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24278208/