spring安全,使用http basic保护特定的api路径,并使用discord ouath2保护其他路径

dsf9zpds  于 2021-07-14  发布在  Java
关注(0)|答案(0)|浏览(249)

我正在尝试用spring security配置两种身份验证方法
端点/执行器/**应使用用户/密码进行身份验证
另一个方法应该是ouath2,以使用discord(get accesstoken)进行身份验证这里是我的configure()

http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/h2-console/**").permitAll();

        http.csrf().disable();
        http.headers().frameOptions().disable();
// (1)
        http.authorizeRequests()
                .antMatchers("/actuator/**").authenticated()
                .and().httpBasic().and().authorizeRequests().anyRequest().permitAll();
//(2)
        http.authorizeRequests()
                .and()
                .oauth2Login()
                .defaultSuccessUrl("/success")
                .tokenEndpoint().accessTokenResponseClient(new RestOAuth2AccessTokenResponseClient(this.restOperations()))
                .and()
                .userInfoEndpoint()
                .customUserType(User.class, "discord")
                .userService(new CustomOAuth2UserServiceImpl(this.restOperations(), this.userRepository, configService, jda));

通常情况下,当我运行代码时,只有代码(1)w/o(2)端点/执行器受到httpbasic的正确保护。
当我只使用代码(2)时,我可以从discord获取accesstoken,但是当我想要组合它时,我遇到了一个问题,端点/执行器需要discord auth而不是httpbasic。
我试着把它们分成两个顺序为(1)和(2)的配置,但仍然不起作用。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题