简体   繁体   English

Spring 安全-登录架构

[英]Spring security - login architecture

I have been following the example in this tutorial: https://spring.io/blog/2015/01/28/the-api-gateway-pattern-angular-js-and-spring-security-part-iv我一直在关注本教程中的示例: https : //spring.io/blog/2015/01/28/the-api-gateway-pattern-angular-js-and-spring-security-part-iv

In brief:简单来说:

  1. I have a server called UI that has some html and angular js.我有一个名为 UI 的服务器,它有一些 html 和 angular js。

  2. I have a server called resource that has a RestController who is serving the content from a DB.我有一个名为资源的服务器,它有一个 RestController,它从数据库提供内容。 All the calls must be authenticated.所有调用都必须经过身份验证。

The UI server has a login page which works with spring http basic login and creates a spring session that is stored in a Redis server and it is shared to the resource server. UI 服务器有一个登录页面,它与 spring http 基本登录一起工作,并创建一个存储在 Redis 服务器中的 spring 会话,并将其共享给资源服务器。 When i have some dummy users in memory authentication everything works fine.当我在内存身份验证中有一些虚拟用户时,一切正常。

The question is: I want my UI server to be able to perform a login with real users, that exist in the DB.问题是:我希望我的 UI 服务器能够使用数据库中存在的真实用户执行登录。 The UI server should not have any DB related code (not knowing its existence) but it should call a REST service in the resource server. UI 服务器不应该有任何与数据库相关的代码(不知道它的存在),但它应该调用资源服务器中的 REST 服务。 The only way i was thinking (but is sounds wrong to me) is to implement a userDetailsService bean in the UI and the loadUserByUsername method should call a rest service from the resource server (eg /getUser).我想的唯一方法(但对我来说听起来不对)是在 UI 中实现 userDetailsS​​ervice bean,并且 loadUserByUsername 方法应该从资源服务器(例如 /getUser)调用一个休息服务。 The rest service should return all the user details including credentials and roles for the given username.其余服务应返回所有用户详细信息,包括给定用户名的凭据和角色。 However, to my understanding, this service cannot be secured (for the call to be successful) which compromises the entire security.但是,据我所知,无法保护此服务(为了调用成功),这会危及整个安全性。

I am open to all suggestions and recommendations.我愿意接受所有建议和建议。 Bare in mind this is my first attempt to work with Spring.请记住,这是我第一次尝试使用 Spring。

Thank you in advance,先感谢您,

Nicolas尼古拉斯

In case that someone is interested how i tackled this..如果有人对我如何解决这个问题感兴趣..

I decided to do the prudent thing and study spring security.. :)我决定做谨慎的事情并研究弹簧安全性.. :)

My answer is to use a custom AuthenicationProvider in my UI server, which will call an unprotected rest login service in the resource server, which in turn validate the user against the DB.我的答案是在我的UI服务器中使用自定义AuthenicationProvider ,它将调用资源服务器中未受保护的休息登录服务,然后根据数据库验证用户。

If the response is successful (eg a user object could be returned with username, password, roles) then i will create a UsernamePasswordAuthenticationToken object out of it and return it.如果响应成功(例如,可以使用用户名、密码、角色返回用户对象),那么我将创建一个 UsernamePasswordAuthenticationToken 对象并返回它。

If the response is NOT successful (eg return object was null or an exception was thrown) then i will either return null or throw an AuthenticationException, it depends on how Spring behaves... I haven't reached that part of studying yet..如果响应不成功(如返回的对象为空或抛出异常),那么我要么返回null或抛出的AuthenticationException,这取决于如何在Spring的行为......我还没有达到那个尚未研究的一部分..

http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#tech-intro-authentication http://docs.spring.io/spring-security/site/docs/3.1.x/reference/springsecurity-single.html#tech-intro-authentication

Spring Security is a powerful and highly customizable authentication and access-control framework. Spring Security 是一个功能强大且高度可定制的身份验证和访问控制框架。 It is the de-facto standard for securing Spring-based applications.它是保护基于 Spring 的应用程序的事实上的标准。

Spring Security is a framework that focuses on providing both authentication and authorization to Java applications. Spring Security 是一个专注于为 Java 应用程序提供身份验证和授权的框架。 Like all Spring projects, the real power of Spring Security is found in how easily it can be extended to meet custom requirements与所有 Spring 项目一样,Spring Security 的真正强大之处在于它可以轻松扩展以满足自定义要求

Features Comprehensive and extensible support for both Authentication and Authorization特性 对身份验证和授权的全面且可扩展的支持

Protection against attacks like session fixation, clickjacking, cross site request forgery, etc防止会话固定、点击劫持、跨站点请求伪造等攻击

Servlet API integration Servlet API 集成

Optional integration with Spring Web MVC与 Spring Web MVC 的可选集成

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

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