【问题标题】:Authentication by JWT and Authorization by local databaseJWT认证和本地数据库授权
【发布时间】:2020-09-06 00:43:25
【问题描述】:

在我的 Spring Boot 应用程序中,我的用户由第三方进行身份验证,我收到一个 Jwt,我使用 Spring Security 中的 NimbusJwtDecoder 对其进行解析。

 @Override
 public void configure(HttpSecurity http) throws Exception {
       http.authorizeRequests()
            .mvcMatchers("/api/private").authenticated()
            .mvcMatchers("/api/organisations").authenticated()
            .mvcMatchers("/api/project").authenticated()
            .mvcMatchers("/api/user").authenticated()
            .and()
            .oauth2ResourceServer().jwt();
    }

这很好,现在我想通过检索我将存储在我自己的数据库中的角色和权限来添加授权。我有来自 Jwt 的用户名,但我不知道如何将其全部插入。任何帮助将不胜感激。

【问题讨论】:

    标签: spring-security oauth jwt authorization auth0


    【解决方案1】:

    当您使用 JWT 对用户进行身份验证时,将用户名添加到 SecurityContext 的主体中。

    创建一个实现 javax.servelet.Filter 接口的 AuthorisationFilter 类。在此类中,使用 securityContextHolder 中的用户名从数据库中获取用户详细信息,并将grantedAuthorities 添加到上下文中。

    在 SecurityConfig 中创建一个 FilterRegistrationBean,它将过滤器设置为 AuthorisationFilter,将 urlPatterns 设置为 ("/*") 或视情况而定,并将 Order 设置为 Ordered.LOWEST_PRECEDENCE

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 2017-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2013-02-19
      • 2019-03-07
      相关资源
      最近更新 更多