[英]Spring security - Secure a request by validating principle against @RequestParam parameter?
我使用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中,您可以為此使用interceptors
。
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.