简体   繁体   English

如何处理数据库中用户的身份验证/授权?

[英]How to handle authentication/authorization with users in a database?

Currently, I am working on a web project using JSF 2.0, Tomcat 7 and MongoDB.目前,我正在使用 JSF 2.0、Tomcat 7 和 MongoDB 进行 Web 项目。 I have a big question of how to handle the session management and authentication/authorization with users in a database.我有一个关于如何处理数据库中用户的会话管理和身份验证/授权的大问题。

The structure I want is as follows: only logged in users can create events and everyone can see the created events.我想要的结构是这样的:只有登录的用户才能创建事件,每个人都可以看到创建的事件。

  • create.xhtml --> only for logged in users. create.xhtml --> 仅适用于登录用户。
  • events.xhtml --> public for everyone. events.xhtml --> 对所有人公开。

The basic structure I'm planning is:我计划的基本结构是:

  • Check if the page requires logged in user (eg create.xhtml )检查页面是否需要登录用户(例如create.xhtml
  • If yes, check if user is logged in如果是,检查用户是否登录
  • If user is not logged in, go to login.xhtml如果用户未登录,请转到login.xhtml
  • If successfully logged in, come back to requested page如果登录成功,返回请求页面
  • Keep the "User is logged in" information unless user clicks log out button.除非用户单击注销按钮,否则保留“用户已登录”信息。 (there I guess @SessionScoped gets into play) (我猜@SessionScoped起作用了)

The question is:问题是:

  1. What is the less complicated way of doing this?这样做的不太复杂的方法是什么?
  2. Where should I use the @SessionScoped annotation?我应该在哪里使用@SessionScoped注释? In Create.java or LoginManager.java ?Create.javaLoginManager.java
  3. Spring security looks kind of complicated for my issue, do I really need it?对于我的问题,Spring 安全性看起来有点复杂,我真的需要它吗? if yes, can you explain a little bit of how the implementation works together with JSF 2.0 and Mongo DB?如果是,您能否解释一下该实现如何与 JSF 2.0 和 Mongo DB 一起工作?

There are several options.有几种选择。 Which to choose is fully up to you.选择哪个完全取决于您。 Just objectively weigh the concrete advantages and disadvantages conform your own situation.只是客观地权衡具体的优缺点符合你自己的情况。


1. Use Java EE provided container managed authentication 1. 使用 Java EE 提供的容器管理认证

Just declare a <security-constraint> in web.xml which refers a security realm which is configured in servletcontainer.只需在web.xml声明一个<security-constraint> ,它指的是在 servletcontainer 中配置的安全领域。 You can for your webapp specify URL pattern(s) which should be checked for login and/or role(s), eg /secured/* , /app/* , /private/* , etc.您可以为您的 web 应用程序指定 URL 模式,这些模式应该检查登录和/或角色,例如/secured/*/app/*/private/*等。

Before Java EE 8, you unfortunately still need to configure a security realm in a servletcontainer-specific way.在 Java EE 8 之前,不幸的是,您仍然需要以特定于 servletcontainer 的方式配置安全领域。 It's usually described in servletconainer-specific documentation.它通常在 servletconainer 特定文档中描述。 In case of Tomcat 8, that's the Realm HOW-TO .如果是 Tomcat 8,那就是Realm HOW-TO For example, a database based realm based on users/roles tables is described in section "JDBCRealm".例如,“JDBCRealm”部分描述了基于用户/角色表的基于数据库的领域。

Since Java EE 8, there will finally be a standard API based on JSR-375 .从 Java EE 8 开始,最终会有一个基于JSR-375的标准 API。

Advantages:好处:

  • Relatively quick and easy to setup and use.相对快速且易于设置和使用。
  • Since Java EE 8 there's finally a robust and flexible standard API.从 Java EE 8 开始,终于有了一个健壮且灵活的标准 API。

Disadvantages:缺点:

  • Before Java EE 8, realm configuration is container-specific.在 Java EE 8 之前,领域配置是特定于容器的。 In Java EE 8, the new JSR-375 Security Spec should solve that with help of JASPIC .在 Java EE 8 中,新的JSR-375 安全规范应该在JASPIC 的帮助下解决这个问题。
  • Before Java EE 8, , there is no fine grained control.在 Java EE 8 之前,没有细粒度控制。
  • Before Java EE 8, it's very spartan;在 Java EE 8 之前,它非常简陋; no "remember me", poor error handling, no permission based restriction.没有“记住我”,糟糕的错误处理,没有基于权限的限制。

See also:也可以看看:


2. Homegrow a servlet filter 2. 自己开发一个servlet 过滤器

This allows for much more fine grained control, but you're going to need to write all the code yourself and you should really know/understand how you should implement such a filter to avoid potential security holes.这允许进行更细粒度的控制,但是您将需要自己编写所有代码,并且您应该真正知道/理解应该如何实现这样的过滤器以避免潜在的安全漏洞。 In JSF side, you could for example just put the logged-in user as a session attribute by sessionMap.put("user", user) and check in the filter if session.getAttribute("user") is not null .例如,在 JSF 方面,您可以通过sessionMap.put("user", user)将登录用户作为会话属性,并在session.getAttribute("user")不为null检查过滤器。

Advantages:好处:

  • Fine grained control.细粒度控制。
  • Completely container independent.完全独立于容器。

Disadvantages:缺点:

  • Reinvention of the wheel;轮子的再造; new features require a lot of code.新功能需要大量代码。
  • As starter, you're never sure if your code is 100% robust.首先,您永远不确定您的代码是否 100% 健壮。

See also:也可以看看:


3. Adapt a 3rd party framework 3. 调整第 3 方框架

For example, Apache Shiro , Spring Security , etc. This offers usually much more fine grained configuration options than standard container managed authentication and you don't need to write any code for this yourself, expect of the login page and some (XML) configuration of course.例如, Apache ShiroSpring Security等。这通常提供比标准容器管理的身份验证更细粒度的配置选项,您不需要自己为此编写任何代码,除了登录页面和一些(XML)配置之外当然。

Advantages:好处:

  • Fine grained control.细粒度控制。
  • Completely container independent.完全独立于容器。
  • No reinvention of the wheel;无需重新发明轮子; minimum of own code.最少自己的代码。
  • Thoroughly developed and tested by lot of users, so most likely 100% robust.由大量用户彻底开发和测试,因此很可能 100% 健壮。

Disadvantages:缺点:

  • Some learning curve.一些学习曲线。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM