gpt4 book ai didi

java - Spring Security上下文中的身份验证和授权有什么区别?

转载 作者:行者123 更新时间:2023-12-04 13:17:52 24 4
gpt4 key购买 nike

我正在开发一个 java spring boot 项目,我正在尝试为使用 JWT 的用户身份验证设置 spring 安全性,我正在关注的教程(以及我在互联网上找到的许多教程和项目)会谈关于两个部分 - 身份验证和授权

在大多数教程中有两个过滤器类,一个处理身份验证,另一个处理授权!(我发现有些只有一个扩展 OncePerRequestFilter 类的类)。

在那些有两个过滤器类的项目中,身份验证过滤器类扩展 UsernamePasswordAuthenticationFilter 类。授权类扩展 BasicAuthenticationFilter 类。

有没有一种方法我只能在我的项目中使用身份验证部分,或者我应该使用这两个类来设置 spring security 中的用户身份验证?

任何解释将不胜感激。

最佳答案

Is there a way that I can only use authentication part in my project or should I use both classes to set up user authentication in spring security?

不,没有仅身份验证部分的概念,您对 spring security 有错误的认识,spring security 就是通过使用默认配置或通过实现您的自定义配置进行配置。 (AuthenticationFiltersAuthenticationProvidersAuthenticationToken 等)


Spring security 是关于身份验证和授权的,Spring security 是通过在 web.xml 中声明一个过滤器 DelegatingFilterProxy 来配置的(在 Spring boot 中,它将通过自动配置来完成)。

Spring 安全性在代理过滤器或 spring 托管 beans 方面在您的应用程序之前放置了一个WALL(HttpFireWall)。如果在身份验证和授权部分都成功,则请求可以到达您的应用程序。

1。身份验证就是关于用户的身份识别。

它将经历

  • 凭据验证或
  • 验证授权 header 内容或
  • 验证与请求关联的 cookie(JSESSIONID cookie),即 session
  • 如果以上都不匹配,则用户被识别为匿名。

在这一步Authentication对象将被创建。从 auth 对象你可以得到

  • details 对象(有关身份验证请求的其他详细信息)
  • 主体对象(UserDetailsAuthenticatedPrincipalPrincipal)
  • 凭据(通常是密码,但可以是与 AuthenticationManager 相关的任何内容)
  • grantedAuthorites 的集合
  • 和一个 boolean 值已验证

2。授权就是访问决策。

FilterSecurityInterceptor 几乎排在过滤器链的最后,它从 SecurityContext 获取 Authentication 对象并获得授权权限列表(授予角色) 并决定是否允许此请求到达所请求的资源,通过与 HttpSecurityConfiguration 中配置的允许的 AntMatchers 进行匹配来做出决定。

考虑异常 401-UnAuthorized 和 403-Forbidden。这些决定将在过滤器链的最后完成
401-UnAuthorized:未经身份验证的用户试图访问安全资源。
403-Forbidden:经过身份验证的用户试图访问受限资源。
未经身份验证的用户将被允许访问不受限制的资源,他不会收到未经授权的错误,但它由 AnonymousAuthenticationFilter 处理,它为未经身份验证的用户设置权限 ROLE_ANONYMOUS

注意事项
下面给出过滤器顺序。在哪里,
身份验证是@order-4
授权为@Order-9(Last)

From Doc
Spring Security has several areas where patterns you have defined are tested against incoming requests in order to decide how the request should be handled. This occurs when the FilterChainProxy decides which filter chain a request should be passed through and also when the FilterSecurityInterceptor decides which security constraints apply to a request. It's important to understand what the mechanism is and what URL value is used when testing against the patterns that you define.

Filter Ordering
The order that filters are defined in the chain is very important. Irrespective of which filters you are actually using, the order should be as follows:
1. ChannelProcessingFilter, because it might need to redirect to a different protocol
2. SecurityContextPersistenceFilter, so a SecurityContext can be set up in the SecurityContextHolder at the beginning of a web request, and any changes to the SecurityContext can be copied to the HttpSession when the web request ends (ready for use with the next web request)
3. ConcurrentSessionFilter, because it uses the SecurityContextHolder functionality but needs to update the SessionRegistry to reflect ongoing requests from the principal
4. Authentication processing mechanisms - UsernamePasswordAuthenticationFilter, CasAuthenticationFilter, BasicAuthenticationFilter etc - so that the SecurityContextHolder can be modified to contain a valid Authentication request token
5. The SecurityContextHolderAwareRequestFilter, if you are using it to install a Spring Security aware HttpServletRequestWrapper into your servlet container
6. RememberMeAuthenticationFilter, so that if no earlier authentication processing mechanism updated the SecurityContextHolder, and the request presents a cookie that enables remember-me services to take place, a suitable remembered Authentication object will be put there
7. AnonymousAuthenticationFilter, so that if no earlier authentication processing mechanism updated the SecurityContextHolder, an anonymous Authentication object will be put there
8. ExceptionTranslationFilter, to catch any Spring Security exceptions so that either an HTTP error response can be returned or an appropriate AuthenticationEntryPoint can be launched
9. FilterSecurityInterceptor, to protect web URIs and raise exceptions when access is denied

只是给出一些关于 spring 安全过滤器的概念

enter image description here

最后,如果您是 Spring Security 的新手。我的建议是尝试最多的示例并花更多时间在调试日志上并尝试理解流程。

关于java - Spring Security上下文中的身份验证和授权有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58350894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com