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

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

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

  1. http.authorizeRequests()
  2. .antMatchers("/").permitAll()
  3. .antMatchers("/h2-console/**").permitAll();
  4. http.csrf().disable();
  5. http.headers().frameOptions().disable();
  6. // (1)
  7. http.authorizeRequests()
  8. .antMatchers("/actuator/**").authenticated()
  9. .and().httpBasic().and().authorizeRequests().anyRequest().permitAll();
  10. //(2)
  11. http.authorizeRequests()
  12. .and()
  13. .oauth2Login()
  14. .defaultSuccessUrl("/success")
  15. .tokenEndpoint().accessTokenResponseClient(new RestOAuth2AccessTokenResponseClient(this.restOperations()))
  16. .and()
  17. .userInfoEndpoint()
  18. .customUserType(User.class, "discord")
  19. .userService(new CustomOAuth2UserServiceImpl(this.restOperations(), this.userRepository, configService, jda));

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

暂无答案!

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

相关问题