保护您的应用程序 Securing your application
要使用Spring Security与单个Web页面应用程序(如JHipster生成的应用程序),您需要Ajax登录/注销/错误视图。我们已经配置了Spring Security,以便正确使用这些视图,当然,我们为您生成所有JavaScript和HTML代码。
默认情况下,JHipster配有4个不同的用户:
- "系统用户 system", who is mainly used by our audit logs, when something is done automatically
- "匿名用户 anonymousUser", who is given to anonymous users when they do an action
- "user", who is a normal user with "ROLE_USER" authorization. His default password is "user"
- "admin", who is an admin user with "ROLE_USER" and "ROLE_ADMIN" authorizations. His default password is "admin"
出于安全考虑,您应该更改这些默认密码。
HTTP会话认证 HTTP Session Authentication
这是“经典”的Spring Security认证机制,但是我们已经有了很大的改进。它使用HTTP会话,因此它是一种有状态的机制:如果您计划在多个服务器上扩展应用程序,则需要具有粘性会话的负载平衡器,以便每个用户驻留在同一服务器上。
This is the "classical" Spring Security authentication mechanism, but we have improved it quite significantly. It uses the HTTP Session, so it is a stateful mechanism: if you plan to scale your application on multiple servers, you need to have a load balancer with sticky sessions so that each user stays on the same server.
Improved remember-me mechanism
我们修改了Spring Security remember-me机制,以便您拥有一个独特的令牌,它存储在数据库中(SQL或NoSQL数据库,具体取决于您在生成期间的选择)。我们还存储比标准实现更多的信息,所以您可以更好地了解这些令牌来自哪里:IP地址,浏览器,日期...我们生成一个完整的管理屏幕,以便您可以使会话无效,例如,如果您忘记了在另一台电脑上登出。
We have modified the Spring Security remember-me mechanism so that you have a unique token, that is stored in your database (SQL or NoSQL database, depending on your choice during generation!). We also store more information than the standard implementation, so you have a better understanding of where those tokens come from: IP address, browser, date... And we generate a complete administration screen, so that you can invalidate sessions, for example if you forgot to log out on another computer.
Cookie theft protection
我们添加了一个非常完整的Cookie防盗机制:我们将您的安全信息存储在一个cookie以及数据库中,每次用户登录时,我们会修改这些值,并检查它们是否已被更改。这样一来,如果用户窃取您的cookie,他最多只能使用一次。
We have added a very complete cookie theft protection mechanism: we store your security information in a cookie, as well as in the database, and each time a user logs in we modify those values and check if they have been altered. That way, if a user ever steals your cookie, he will be able to use only once, at most.
CSRF protection
Spring Security和AngularJS都具有开箱即用的CSRF保护,但不幸的是他们不使用相同的Cookie或HTTP标头!实际上,实际上对于CSRF攻击实际上没有任何保护。当然,我们重新配置这两个工具,使它们正确地协同工作。
Spring Security and AngularJS both have CSRF protection out-of-the-box, but unfortunately they don't use the same cookies or HTTP headers! In practice, you have in fact no protection at all for CSRF attacks. Of course, we re-configure both tools so that they correctly work together.
Social Login
JHipster使用Spring Social提供“社交登录”,因此用户可以使用Google,Facebook或Twitter身份验证连接到您的应用程序。这是使用Sping Boot的启动器模块进行配置的。
JHipster provide "social login", using Spring Social, so users can connect to your application using their Google, Facebook or Twitter authentication. This is configured using Sping Boot's starter modules.
JWT authentication
JSON Web Token (JWT) 身份验证是一种无状态的安全机制,因此如果要在几个不同的服务器上扩展应用程序,这是一个很好的选择。
请注意,这是使用 microservices architecture
时的默认选项。.
默认情况下,这种认证机制不存在于Spring Security中, the Java JWT 项目是JHipster特有的集成。它比OAuth2更易于使用和实现,因为它不需要持久性机制,因此它适用于所有SQL和NoSQL选项。
此解决方案使用保存用户登录名和权限的安全令牌。当令牌被签名时,它不能被用户改变。
应在 application.yml
文件中配置秘密密钥作为 jhipster.security.authentication.jwt.secret
属性.
OAuth2 Authentication
OAuth2是一种无状态的安全机制,如JWT。Spring Security提供了由JHipster配置的OAuth2实现。
OAuth2最大的问题是需要有多个数据库表才能存储其安全令牌。如果您正在使用SQL数据库,我们将提供必要的Liquibase changlog,以便为您自动创建这些表。
由于Spring Security仅支持OAuth2与SQL数据库,我们还实现了我们自己的MongoDB版本。JHipster生成MongoDB的OAuth2实现以及必要的MongoDB配置。
This solution uses a secret key, which should be configured in the application-*.yml
files, using the jhipster.security.authentication.oauth
properties. See the the common application properties documentation for more information on this configuration.