[英]Passing a password for methods for Spring MVC Controller - AOP or Spring Security?
我已經使用Spring MVC了很短的時間了,它帶有帶注釋的JSP頁面控制器。 我有一個與此類似的課程:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class AdminController {
@RequestMapping(value = "/doStuff1.htm", method = RequestMethod.POST)
public void doStuff1(@RequestParam("password")String password) {
// do some stuff
}
@RequestMapping(value = "/doStuff2.htm", method = RequestMethod.POST)
public void doStuff2(
@RequestParam("password")String password,
@RequestParam("foo")String foo{
// do some stuff
}
}
如您所見,每個呼叫都會傳入一個密碼參數。從對話框中讀取密碼,並將其傳遞到提交的每個呼叫中。
我想從方法調用中刪除password參數,以獲取“更干凈”的代碼。
為此,我快速看了一下Spring安全性,但是它似乎有點重量級。 也許可以使用AOP?
有什么明顯的解決方案我找不到嗎?
非常感謝,-斯科特
如您所見,每個呼叫都會傳入一個密碼參數。從對話框中讀取密碼,並將其傳遞到提交的每個呼叫中。
a)這是一個可怕的做法。 這意味着擁有網絡嗅探器的任何人都可以在整個地方看到您的密碼。 一次提交密碼可能沒問題(盡管最好使用安全的方式來傳輸密碼),但隨后會話應包含身份驗證令牌
b)僅密碼? 切勿使用沒有用戶名的密碼! 使用蠻力攻擊,所有獨立密碼最終都會被黑客入侵,但是用戶名/密碼組合更難以破解。
c)您的控制器方法不應該知道或關心密碼。 這與他們無關。 他們有工作要做,處理密碼/安全性是一個貫穿各領域的問題,因此不應在控制器級別上實現。 這導致我們想到一個問題:
AOP是實現跨領域功能的一種非常強大的方法,但是它有一些缺點:
proxy-target-class="true"
使用基於CGLib的子類時,才建議控制器使用proxy-target-class="true"
(在XML配置中)。 后者具有有趣的副作用,例如構造函數的雙重執行。 許多人使用它,但我建議不要這樣做。 這意味着Spring AOP不是一個不錯的選擇。 所以我的建議是:
Spring Security是一種自定義解決方案,可完全滿足您的需求:保護基於Spring的站點(使用Spring MVC或其他Web框架)。 盡管Spring Security可能是一個巨大的怪物,但在大多數情況下,所需的配置很少:
<http auto-config='true'>
<!-- restrict all URLs to role ROLE_USER -->
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<!-- define two users, jimi and bob -->
<user name="jimi" password="jimispassword"
authorities="ROLE_USER, ROLE_ADMIN" />
<user name="bob" password="bobspassword"
authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
(摘自“最小<http>
配置”部分 )
如前一篇文章所述,最好每個會話僅提交一次登錄數據,並將身份驗證令牌存儲在用戶會話內。
為了檢查令牌,您可以從springframework實現HandlerInterceptor
接口。
例如
public class MyHandlerInterceptor extends HandlerInterceptorAdapter {
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
//check authentication
}
}
和配置:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<bean id="myInterceptor" class="...MyInterceptor"/>
</list>
</property>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.