简体   繁体   English

Spring 启动和 Angular 身份验证 - 如何保护应用程序?

[英]Spring Boot and Angular authentication - how to secure the app?

So I have this legacy app running with spring MVC and JSP, Reading from mongo DB所以我有这个遗留应用程序运行 spring MVC 和 JSP,从 mongo DB 读取
, I am thinking of replacing it with a modern app using Spring boot and angular JS ,我正在考虑将其替换为使用 Spring 引导和 angular JS 的现代应用程序

I am not looking for a framework, My question is more conceptual regarding authentication, and how it works between angular and Spring boot.我不是在寻找一个框架,我的问题是关于身份验证的更多概念,以及它如何在 angular 和 Spring 引导之间工作。

I don't want to use a third party for authentication,I would like to continue using my internal DB user and password.我不想使用第三方进行身份验证,我想继续使用我的内部数据库用户和密码。
I also have a rest API that clients need to register first and then send a token on every request bypassing angular and authentication.我还有一个 rest API 客户端需要先注册,然后在绕过 angular 和身份验证的每个请求上发送一个令牌。

So in the past ( ancient j2ee ) I had Servlet filter, this filter war running on any request, it checked if the session is authenticated, if not - it would forward to the authentication page, then store the result in the session.所以在过去(古老的 j2ee)我有 Servlet 过滤器,这个过滤器对任何请求都运行,它检查 session 是否经过身份验证,如果没有 - 它会转发到身份验证页面,然后将结果存储在 session 中。

something along the lines of this:类似这样的东西:

@WebFilter("/*")
public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {    
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);
        String loginURI = request.getContextPath() + "/login";

        boolean loggedIn = session != null && session.getAttribute("user") != null;
        boolean loginRequest = request.getRequestURI().equals(loginURI);

        if (loggedIn || loginRequest) {
            chain.doFilter(request, response);
        } else {
            response.sendRedirect(loginURI);
        }
    }

    // ...
}

How to do the same authentication mechanism with angular JS?如何与 angular JS 做同样的认证机制?
what is the equivalent to filters in servlets? servlets 中的过滤器相当于什么?
since Angular is client side rendering, how to secure it?由于 Angular 是客户端渲染,如何保护它?

Thank you for any input!感谢您的任何意见!

The obvious thought: https://spring.io/guides/tutorials/spring-security-and-angular-js/显而易见的想法: https://spring.io/guides/tutorials/spring-security-and-angular-js/

If you tried to serve angular on the same port using static resources, you will have a hard time setting it up with spring boot because there will be conflicts about who should resolve a particular resource: angular or spring boot. If you tried to serve angular on the same port using static resources, you will have a hard time setting it up with spring boot because there will be conflicts about who should resolve a particular resource: angular or spring boot. For eg: /home can be resolved by angular but spring isn't aware of the fact that there is a /home and if you hit /home directly, it won't be resolved by angular unless index.html was loaded and you will see a 404 Whitelabel page. For eg: /home can be resolved by angular but spring isn't aware of the fact that there is a /home and if you hit /home directly, it won't be resolved by angular unless index.html was loaded and you will请参阅 404 Whitelabel 页面。

If you serve angular and spring boot on separate ports then you must read this quote from above spring guides:如果您在单独的端口上提供 angular 和 spring 引导,那么您必须阅读上述 spring 指南中的此引用:

You can't have a secure, stateless application.你不能拥有一个安全的、无状态的应用程序。

Adding to what Hopey One already pointed out: Using JHipster is not as bad as you think.补充一下Hopey One已经指出的内容:使用JHipster并没有您想象的那么糟糕。 It will hardly take a day to build 80% of your project and then you can tweak the rest 20% accordingly.构建 80% 的项目几乎不需要一天的时间,然后您可以相应地调整 rest 20%。 It will take care of most of the gaps in knowledge and let you write the code that is actually required.它将解决大部分知识空白,让您编写实际需要的代码。 And since you already have a running application, most of it would be a copy-paste work.而且由于您已经有一个正在运行的应用程序,因此其中大部分将是复制粘贴工作。 You can read this guide to set up the environment.您可以阅读本指南来设置环境。

I am not looking for a framework, My question is more conceptual regarding authentication, and how it works between angular and Spring boot.我不是在寻找一个框架,我的问题是关于身份验证的更多概念,以及它如何在 angular 和 Spring 引导之间工作。

I found this particular tricky to reply, since you are asking for a conceptual answer beyond particular framework, but you are referring to Angular and Spring which are two frameworks, so let's take one step back.我发现这个回答特别棘手,因为您要求的是超出特定框架的概念性答案,但您指的是 Angular 和 Spring 这两个框架,所以让我们退后一步。

Proper authentication always has to be performed server side, since authentication on the client is simple to bypass.正确的身份验证始终必须在服务器端执行,因为客户端上的身份验证很容易绕过。 You can have some watchdogs for disallowing certain routes client side, but just to improve UX.您可以设置一些看门狗来禁止客户端的某些路由,但只是为了改善用户体验。

There are basically two flavours to choose from: state or stateless基本上有两种口味可供选择:state 或无状态

State authentication is when after the successful authentication, the server will create some session, store some identifier of it, and validate against it on successive requests. State 身份验证是在身份验证成功后,服务器将创建一些 session,存储它的一些标识符,并在连续请求时对其进行验证。 This is the traditional session with cookies approach, named JSESSION in spring.这是传统的 session 和 cookies 方法,在 spring 中命名为 JSESSION。 The drawback of this approach is that the state has to be maintained somewhere, and unless you have sticky sessions (where you can get away by keeping this info in memory) you will need some distributed system to store/retrieve it (a SQL db, cassandra, your pet project, etc).这种方法的缺点是 state 必须在某个地方维护,除非您有粘性会话(您可以通过将这些信息保存在内存中来摆脱),否则您将需要一些分布式系统来存储/检索它(SQL db, cassandra,您的宠物项目等)。 But as we learned, distributed state with high traffic turns a bottleneck and is better to avoid it when possible.但正如我们所知,高流量的分布式 state 会成为瓶颈,最好尽可能避免。

Stateless Authentication: As is the case of the JWT contains all the information needed to recreate the session on any server, so there is no distributed state, or better said, the state is in the client (browser), and is cryptographically signed so users can access/tamper them. Stateless Authentication: As is the case of the JWT contains all the information needed to recreate the session on any server, so there is no distributed state, or better said, the state is in the client (browser), and is cryptographically signed so users可以访问/篡改它们。 The drawback is that the encryption/decryption of the token could be a bit CPU demanding, but nothing noticeable for a user.缺点是令牌的加密/解密可能需要一点 CPU,但对用户来说没有什么明显的。

So once you decided one the above, the rest is not that different.因此,一旦您确定了上述一项,rest 并没有什么不同。 In both cases you will have to implement some interceptor on angular, to send along the cookie or the Auth header on each request depending the case, and on the server side you will have to handle the request thru the corresponding filter.在这两种情况下,您都必须在 angular 上实现一些拦截器,以便根据具体情况在每个请求上发送 cookie 或 Auth header,并且在服务器端,您必须通过相应的过滤器处理请求。 Instead of the cookie in the first example you could just send the session value and as a header, in a more RESTful fashion.除了第一个示例中的 cookie,您可以以更 RESTful 的方式发送 session 值和 header 值。

I also have a rest API that clients need to register first and then send a token on every request bypassing angular and authentication.我还有一个 rest API 客户端需要先注册,然后在绕过 angular 和身份验证的每个请求上发送一个令牌。

This looks like a new use case for your API but the above stated holds, you could still use those authentication headers for Authorisation after the login was done and token/sessions were returned to the authenticating user.这看起来像是您的 API 的新用例,但上述情况成立,您仍然可以在登录完成并将令牌/会话返回给身份验证用户后使用这些身份验证标头进行授权。 You could have also a different authentication token for this type of client, in which case it would be just a matter to chain a new filter for checking this header, without any changes on your API.您还可以为这种类型的客户端使用不同的身份验证令牌,在这种情况下,只需链接一个新的过滤器来检查此 header,而无需对您的 API 进行任何更改。

I'm sure you will find the right resources online to implement your desired authentication/authorisation solution我相信您会在网上找到合适的资源来实施您想要的身份验证/授权解决方案

Have a look at the JHipster project.看看JHipster项目。 Without much effort you can generate a simple project built on angular and spring boot.无需太多努力,您就可以生成一个基于 angular 和 spring 引导的简单项目。 It implements JWT for authentication with a DB of your choice.它实现了 JWT 以使用您选择的数据库进行身份验证。 Might serve as a good bootstrap or as a reference implementation for implementing your project.可以作为一个很好的引导程序或作为实现项目的参考实现。

I recommend exploring these two topics:我建议探索这两个主题:

Filter is part of servlet-api , it means that filter can be implemented in any library, and web server will register and map to specific url. Filterservlet-api的一部分,这意味着过滤器可以在任何库中实现,并且 web 服务器将注册 map 到特定的 url。 In our case your LoginFilter is all most the same like JWTAuthenticationFilter , but with additional functionality of Spring Framework, both of these classes implement the Filter interface.在我们的例子中,您的LoginFilterJWTAuthenticationFilter相同,但具有 Spring 框架的附加功能,这两个类都实现了Filter接口。

With Spring Boot your want to use Spring Security and build on top of the framework as much as possible.使用 Spring 启动,您想使用 Spring 安全性并尽可能建立在框架之上。 I see three approaches:我看到三种方法:

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

相关问题 具有基本身份验证的Secure Spring Boot REST应用程序 - Secure Spring Boot REST app with basic authentication 使用 SSL 保护 Spring Boot 和 Angular 应用程序的最佳方法 - Best way to secure Spring Boot & Angular app with SSL 如何使用 keycloak 保护 angular/spring 应用程序? - How to secure angular/spring app with keycloak? 如何使基本身份验证作为 Angular JS/Spring 启动应用程序中 keycloak 的替代方案 - How to make Basic Authentication work as an alternative for keycloak in a Angular JS/Spring boot app Spring(引导)+ Angular(2 | 4)身份验证选项 - Spring (boot) + Angular (2|4) authentication options 如何将 Angular 4 应用程序与 Spring Boot 堆栈集成? - How to integrate an Angular 4 app with a Spring Boot stack? Spring Boot应用程序中的LDAP身份验证 - LDAP authentication in spring boot app 使用自定义范围/角色保护 Spring 启动应用程序 - Secure Spring Boot app with custom scopes/roles 如何使用Spring Boot将OAuth2身份验证与前端绑定 - How bind angular front with oauth2 authentication with spring boot 如何保护具有身份提供者的 Spring Boot 微服务以无状态方式进行身份验证? - How to secure spring boot microservices having identity provider for authentication in a stateless manner?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM