当前位置: 首页 > 知识库问答 >
问题:

Spring Boot Oauth2资源服务器UserDetailsService

侯涵煦
2023-03-14

试图让UserDetailsService为我设置的oauth2资源服务器工作。我能够成功地对jwt进行身份验证,但是我所做的一切似乎都无法让它调用loadUserByUsername方法。它最初使用的是SAML,并且可以工作,但现在我切换到了Oauth2,我无法让它工作。

     @Service
     public class OauthUsersDetailsServiceImpl implements UserDetailsService{
         @Override
         public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
             //some user loading junk here - this is never called
         }
     }
     @Configuration
        @EnableGlobalMethodSecurity(prePostEnabled = true)
        @EnableWebSecurity
         public class SecurityConfig extends WebSecurityConfigurerAdapter {
            
            @Override
            protected void configure(HttpSecurity http) throws Exception
            {
                //test key for now
                SecretKeySpec key = new SecretKeySpec("private key0000000000000000000000000000000".getBytes(), "HMACSHA256");
                

                http
                    .authorizeRequests()
                    .antMatchers(/*some endpoints im excluding from auth - this all works*/)
                    .permitAll().and()
                    .authorizeRequests()
                    .anyRequest().authenticated().and()
                    .oauth2ResourceServer().jwt().decoder(NimbusJwtDecoder.withSecretKey(key).build());
            }
         }

我在google上发现,我只需要用@service将类注册为bean,spring就会把它捡起来,但它不起作用。我还尝试通过AuthenticationManagerBuilder添加它,但这也不起作用。我的猜测是jwt端有它自己的UserDetailsService,它实现了它,并且优先于我的。也就是说,让我的用户调用的正确方法是什么,还是在身份验证完成后手动调用我的用户加载逻辑并覆盖主体对象更好?我需要在调用endpoint之前执行此操作,以便PreAuthorize可以检查UserDetailsService加载的角色。

共有1个答案

邹俊友
2023-03-14

想通了。希望这能帮助任何遇到同样问题的人。我不得不向链中添加一个自定义过滤器来调用我的用户详细信息服务并覆盖上下文:

public class Oauth2AuthorizationFilter extends GenericFilterBean {

        @Autowired
        private OauthUsersDetailsServiceImpl oauthUsersDetailsServiceImpl;
      
      public Oauth2AuthorizationFilter (OauthUsersDetailsServiceImpl userDetailsService) {
        this.oauthUsersDetailsServiceImpl = userDetailsService;
      }
      
      
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
          throws IOException, ServletException {

        SecurityContext context = SecurityContextHolder.getContext();
        if(context.getAuthentication() != null && !(context.getAuthentication().getPrincipal() instanceof Users)) {
          
          UserDetails user = oauthUsersDetailsServiceImpl.loadUserByUsername(((Jwt)context.getAuthentication().getPrincipal()).getClaimAsString("user_name")); 
          UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
          context.setAuthentication(authentication);
        }
        
        chain.doFilter(request, response);
      }

    }
@Override
        protected void configure(HttpSecurity http) throws Exception
        {
            //test key for now
            SecretKeySpec key = new SecretKeySpec("private key0000000000000000000000000000000".getBytes(), "HMACSHA256");
            
            http.authorizeRequests().antMatchers(/*bunch of junk...*/).permitAll().and().authorizeRequests().anyRequest().authenticated().and()
                .oauth2ResourceServer().jwt().decoder(NimbusJwtDecoder.withSecretKey(key).build());
            
            http.addFilterAfter(jwtAuthTokenFilterBean(), SwitchUserFilter.class);

        }

这终于满足了我的需要

 类似资料:
  • 问题内容: 我们已经设置了OAuth2授权服务器,因此我需要创建一个相应的资源服务器(单独的服务器)。我们计划使用Spring Security OAuth2项目。他们关于设置资源服务器的文档: https://github.com/spring-projects/spring-security- oauth/wiki/oAuth2#resource-server-configuration 应该

  • 我们已经设置了一个OAuth2授权服务器,所以我需要创建一个相应的资源服务器(单独的服务器)。我们计划使用Spring Security OAuth2项目。他们关于设置资源服务器的文档: https://github.com/spring-projects/spring-security-oauth/wiki/oauth2#资源-服务器-配置 应该指向令牌处理bean。然而,令牌处理似乎是由服务器

  • 我有以下场景:一个客户端应用程序试图访问API网关后面的APIendpoint。我想验证(使用id和秘密)应用程序,如果它是应用程序A允许它访问endpoint/信用,如果它是应用程序B允许它访问endpoint/借方。 我假设API网关应该验证一个调用是否应该被拒绝。你能告诉我我的工作流应该是什么样子,我应该使用什么Keycloak功能吗?

  • 资源类型配置 下面讲解如何根据所要访问资源的IP地址和端口,在SSL VPN设备上配置资源。 1. 确定要访问的资源IP地址和端口号,例如,本例要连接的服务器IP地址是200.200.73.65,端口号为4420; 2. 输入用户名和密码登陆SSL VPL控制台,按如图4.1.1所示操作新建资源:SSL VPN设置 > 资源管理 > 新建 > L3VPN。 3. 输入资源信息,包括资源名字(由用户

  • 我需要了解在我的项目范围内使用autheorizaion服务器的便利性。 我们正在实现我们的服务并将其部署到客户环境中。 客户基础结构已经提供了一种身份验证机制,以便对用户进行身份验证。 此机制拦截所有通信并将用户重定向到“登录”表单。 之后,用户被重定向到我们的服务,我们必须处理和消化它,并使用JWT令牌进行响应。 这是我感到迷茫的地方: 我在想: 使用Spring oauth2 向授权服务器请

  • 我有一个keydeport服务器在运行。以及作为资源服务器的Spring Boot应用程序。我可以验证并获取令牌,spring boot应用程序将接受令牌。但是当我想从校长那里得到用户名时,我会得到一个UUID,而不是我的电子邮件或用户名。我还为我的KeyClope添加了一个映射器,将preferred_用户名映射到用户名。它正在工作。 JWT信息 我的spring应用程序属性: