为什么我应该使用 JAAS 来对抗手写安全性?
我得到了手写的安全性、简单的 servlet 过滤器,它将未经授权的用户重定向到他们的登录页面。登录控制器在成功验证后将它们重定向到请求的 URL 或其主页。这种方法工作正常,唯一的缺点是我必须通过堆栈跟踪将存储在 HttpSession 中的 User 对象传递给 EJB beans。
现在我重写了一些代码并使用 Spring-security 作为基于 http 的身份验证。它自动与 Glassfish JAAS 集成。
我不再需要通过 stacktrace 传递 User,调用 sessionContext.getCallerPrincipal()
就足够了。但主体对象仅返回我 userName,而不是 userId,因此如果我需要 userId,我必须执行附加选择。
1)是否有办法扩展Principal对象,以便它可以存储更多属性?
2)为什么我应该使用JAAS或Spring Security或其他安全框架,为什么不直接手写servlet过滤器?
I got hand-written security, simple servlet-filter which redirect not-authorized user to their login pages. Login controller redirect them to the requested URL after successfull authentication or their main page. This approach work fine, the only disadvantage, that I have to pass User object which is stored in the HttpSession through stacktrace to EJB beans.
Now I rewrote some code and use Spring-security as http based authentication. It is integrated automatically with Glassfish JAAS.
I don't need to pass User through stacktrace anymore, invocation sessionContext.getCallerPrincipal()
is enough. But the principal object return me only userName, not userId, so i have to perform addition select if i need userId for example.
1) Is there anyway to extend Principal object, so it can store more properties ?
2) Why i should use JAAS or Spring Security or another security framework, why not just hand writen servlet filter ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
2) 使用像 JAAS 这样的标准安全机制有很多优点:
您可以仅通过配置服务器轻松更改用户身份验证的方式 - 无需更改代码内的任何内容。
您可以确保您的安全性是最新的,支持最强的算法,以安全的方式存储主体等等。同样,只要与您的服务器、框架等保持最新状态即可。手写的安全模块很容易出错并且很快就会过时。
您可以利用框架安全性 - 例如。 web.xml 安全标记、EJB 安全注释。因为 JAAS 是一种标准的身份验证方式,所以您可以确信采用未来的技术会更容易,因为所有重要的技术都将支持 JAAS(Spring security 等)。如果您的软件计划增长,您肯定需要一个标准。
它将节省您的时间和精力。 JAAS 提供身份验证和授权,整齐地打包并在几分钟内完成配置。
我建议进一步阅读J2EE 安全性或者您可以在OWASP指南中找到更多资源。
2) Using a standard security mechanism like JAAS has many advantages:
You can easily change the way user authenticates solely by configuring your server - without need to change anything inside your code.
You can be sure your security is up-to-date, supporting strongest algorithms, storing Principal in a secure manner and so on. Again just by staying up-to-date with your server, framework etc. Having a hand-written security module is prone to errors and to be outdated soon.
You can leverage framework security - eg. web.xml security tags, EJB security annotations. Because JAAS is a standard way to authenticate, you can be sure adopting future technologies will be easier, because all serious technologies will support JAAS (Spring security etc.). If your software is planned to grow, you will definitely need a standard.
It will save you time and effort. JAAS provides both authentication and authorization, neatly packed and configurable within minutes.
I recommend futher reading on J2EE security or you can find more resources in OWASP guides.
1)我不知道你是否可以扩展类 校长。但请注意,在您的 LoginModule,在调用 commit() (可能在您的 login() 方法),可以在 主题。为此,只需将该对象添加到列表之一:Subject.getPrivateCredentials() 或主题。getPublicCredentials() (不带参数)。您可以添加许多对象,例如您自己的类、字符串或任何您想要的对象。
要检索应用程序中的对象,请使用我的其他答案中详细说明的过程。
1) I don't know if you can extend the class Principal. But note, in your LoginModule, before you finish the authentication calling the commit() (probably in your login() method), it is possible to add credentials in the Subject. For this, just add the object to one of the lists: Subject.getPrivateCredentials() or Subject.getPublicCredentials() (with no arguments). You can add many objects like your own class, a String, or whatever you want.
To retrieve the objects in your application, use the procedure detailed in my other answer.