Spring Security:未调用自定义UserDetailsS​​ervice(使用Auth0身份验证)

Joa*_*kim 8 java spring spring-security auth0

我是Spring框架的新手,所以我提前为我理解中的任何漏洞道歉.

我正在使用Auth0来保护我的API,这非常有效.我的设置和配置与Auth0文档中的建议设置相同:

// SecurityConfig.java
@Configuration
@EnableWebSecurity(debug = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    // auth0 config vars here

    @Override
    protected void configure(HttpSecurity http) {
        JwtWebSecurityConfigurer
                .forRS256(apiAudience, issuer)
                .configure(http)
                .authorizeRequests()
                .antMatchers(HttpMethod.GET, "/api/public").permitAll()
                .antMatchers(HttpMethod.GET, "/api/private").authenticated();
    }
}
Run Code Online (Sandbox Code Playgroud)

采用这种设置,弹簧安全主体被设置为用户ID( sub)从JWT令牌:auth0|5b2b....但是,我不想仅仅使用userId,而是将其设置为匹配的用户(来自我的数据库).我的问题是如何做到这一点.

我试过的

我已经尝试实现从本教程复制的自定义数据库支持的UserDetailsS​​ervice .然而,无论我如何尝试将其添加到我的conf中,它都不会被调用.我尝试过几种不同的方式添加它没有效果:

// SecurityConfig.java (changes only)

    // My custom userDetailsService, overriding the loadUserByUsername
    // method from Spring Framework's UserDetailsService.
    @Autowired
    private MyUserDetailsService userDetailsService;

    protected void configure(HttpSecurity http) {
        http.userDetailsService(userDetailsService);  // Option 1
        http.authenticationProvider(authenticationProvider());  // Option 2
        JwtWebSecurityConfigurer
                [...]  // The rest unchanged from above
    }

    @Override  // Option 3 & 4: Override the following method
    protected void configure(AuthenticationManagerBuilder auth) {
        auth.authenticationProvider(authenticationProvider());  // Option 3
        auth.userDetailsService(userDetailsService);  // Option 4
    }

    @Bean  // Needed for Options 2 or 4
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService);
        return authProvider;
    }
Run Code Online (Sandbox Code Playgroud)

不幸的是,由于我需要将其与Auth0身份验证相结合,所以没有类似的"userDetails not called called"问题帮助了我.

我并不认为我正走在正确的道路上.我觉得很奇怪,在这个极为常见的用例中我找不到Auth0的任何文档,所以也许我错过了一些明显的东西.

PS:不确定是否相关,但始终在init期间记录以下内容.

Jun 27, 2018 11:25:22 AM com.test.UserRepository initDao
INFO: No authentication manager set. Reauthentication of users when changing passwords will not be performed.
Run Code Online (Sandbox Code Playgroud)

编辑1:

基于Ashish451的回答,我尝试复制他的CustomUserDetailsS​​ervice,并将以下内容添加到我的SecurityConfig中:

@Autowired
private CustomUserDetailsService userService;

@Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

@Autowired
public void configureGlobal( AuthenticationManagerBuilder auth ) throws Exception {
    auth.userDetailsService( userService );
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,通过这些更改,仍未调用CustomUserDetailsS​​ervice.

编辑2:

添加@Norberto Ritzmann建议的日志记录方法时的输出:

Jul 04, 2018 3:49:22 PM com.test.repositories.UserRepositoryImpl initDao
INFO: No authentication manager set. Reauthentication of users when changing passwords will not be performed.
Jul 04, 2018 3:49:22 PM com.test.runner.JettyRunner testUserDetailsImpl
INFO: UserDetailsService implementation: com.test.services.CustomUserDetailsService
Run Code Online (Sandbox Code Playgroud)

Joa*_*kim 2

我最终向 Auth0 支持询问了此事,他们说目前无法在不修改库源的情况下修改主体。

然而,他们提供了另一种方法,即使用 JWT 验证库(例如https://github.com/auth0/java-jwt)而不是 Spring Security API SDK。

我的解决方案是修改我的代码以仅使用令牌作为主体。