通过JWT进行身份验证并通过本地数据库进行授权

问题描述 投票:0回答:1

在我的Spring Boot应用程序中,我的用户已通过第三方认证,我收到一个Jwt,我使用Spring Security中的NimbusJwtDecoder解析了这个Jwt。

 @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个回答
0
投票

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

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

在SecurityConfig中创建一个FilterRegistrationBean,该过滤器将过滤器设置为AuthorisationFilter,将urlPatterns设置为(“ / *”)或适当时将其设置为Ordered.LOWEST_PRECEDENCE

© www.soinside.com 2019 - 2024. All rights reserved.