为 Spring MVC 控制器的方法传递密码 - AOP 或 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
}
}
如您所见,每个调用都会传入一个密码参数。密码从对话框中读取并传递到提交的每个调用中。
我想从方法调用中删除密码参数,以获得“更干净”的代码。
为此,我快速浏览了 Spring security,但它似乎有点重量级。也许可以使用AOP?
我缺少明显的解决方案吗?
非常感谢, - 斯科特
I have been using Spring MVC for a short while now with annotated controllers for JSP pages. I have a class similar to this:
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
}
}
As you can see, every call will have a password param passed in. The password is read from a dialog and passed into every call that is submitted.
I would like to get rid of the password parameter from the method calls to have 'cleaner' code.
I had a quick look at Spring security for this purpose but it seemed a bit heavyweight. Maybe AOP can be used?
Is there an obvious solution I'm missing?
Many thanks,
- Scott
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
a) 这是一种糟糕的做法。这意味着任何拥有网络嗅探器的人都可以在任何地方看到您的密码。提交密码一次可能没问题(尽管使用安全的方式传输密码会更好),但是会话应该包含身份验证令牌
b) 仅密码?切勿在没有用户名的情况下使用密码!通过暴力攻击,任何独立密码最终都会被黑客入侵,但用户名/密码组合更难以破解。
c) 您的控制器方法不应该知道或关心密码。这不是他们关心的事。他们有工作要做,处理密码/安全是一个跨领域的问题,不应该在控制器级别实现。这引出了我们的问题:
使用什么:AOP 还是 Spring Security?
AOP 是实现横切功能的一种非常强大的方式,但它有一些缺点:
所以我的建议是:
使用 Spring Security
Spring Security< /a> 是一个自定义解决方案,可以完全满足您的需求:保护基于 Spring 的站点(使用 Spring MVC 或其他 Web 框架)。虽然 Spring Security 可能是一个巨大的怪物,但在大多数情况下,所需的配置是最少的:(
摘自 最小
配置)a) This is an awful practice. It means that anybody with a network sniffer will be able to see your password all over the place. It might be ok to submit the password once (although it would be better to use a secure way to transmit the password), but then the session should contain the authentication token
b) Password only? Never use a password without a username! With a brute force attack, any standalone-password will eventually be hacked, but username / password combinations are much more difficult to crack.
c) Your controller methods should not know or care about passwords. It's not their concern. They have work to do, dealing with passwords /security is a cross-cutting concern and should not be implemented on the controller level. Which leads us to the Question:
What to use: AOP or Spring Security?
AOP is a very powerful way of implementing cross-cutting functionality, but it has some drawbacks:
proxy-target-class="true"
(in XML config). The latter has funny side effects like double execution of constructors. Many use it, but I would advise against it. Which means Spring AOP is not a good choice.So my suggestion is:
Use Spring Security
Spring Security is a custom solution for doing exactly what you want: securing Spring based sites (using Spring MVC or another web framework). While Spring Security can be a huge monster, in most cases, the configuration needed is minimal:
(Taken from the section A Minimal
<http>
Configuration)正如上一篇文章中提到的,最好每个会话仅提交一次登录数据,并在用户会话中存储身份验证令牌。
为了检查令牌,您可以从 springframework 实现 HandlerInterceptor 接口。
例如
}
和配置:
As mentioned in the previous post its better to submit the login data only once per session and store an authententication token inside the user session.
For checking the token you can implement the
HandlerInterceptor
interface from the springframework.For example
}
and the configuration: