多重身份验证使用jdbcAuthentification和AD身份验证

huangapple 未分类评论41阅读模式
英文:

Multiple Authentification using jdbcAuthentification and AD authentification

问题

我再次需要您的帮助。我需要通过JDBC或AD对我的应用进行身份验证,但它必须同时适用于两种方式。例如,当我尝试使用JDBC用户进行身份验证时,我的程序必须与数据库中的用户连接,但当我使用AD登录时,它必须与AD用户连接。

以下是我的代码,但是当我按照下面的顺序(1)连接到JDBC时,我只能连接到JDBC,而当我有代码来创建authentificationProvider时,我只能在使用AD登录时登录。

  1. @Configuration
  2. @EnableWebSecurity
  3. public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
  4. @Autowired
  5. private ClientDetailsService clientDetailsService;
  6. @Autowired
  7. private DataSource primaryDataSource;
  8. // @Autowired
  9. // private AuthenticationProvider authenticationProvider;
  10. // @Autowired
  11. // private AuthenticationProvider authenticationProviderAD;
  12. @Value("${security.authentication.provider}")
  13. private String authProvider;
  14. @Value("${ad.domain:#{null}}")
  15. private String adDomain;
  16. // TODO 将系统类似的配置转移到Java中,并且不再使用系统类似的配置,使其变为可选
  17. @Value("${ad.url:#{null}}")
  18. private String adUrl;
  19. @Override
  20. @Order(1)
  21. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  22. RecursiveAdProvider adProvider = new RecursiveAdProvider(adDomain, adUrl);
  23. adProvider.setConvertSubErrorCodesToExceptions(true);
  24. adProvider.setUseAuthenticationRequestCredentials(true);
  25. auth.authenticationProvider(adProvider);
  26. auth.eraseCredentials(false);
  27. }
  28. @Order(2)
  29. protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  30. auth.jdbcAuthentication().dataSource(primaryDataSource)
  31. .usersByUsernameQuery("select username, password, enabled from users where username=?")
  32. .authoritiesByUsernameQuery(
  33. "select username, authority from user_authority JOIN authorities ON user_authority.authority_id = authorities.id where username=?")
  34. .passwordEncoder(new BCryptPasswordEncoder());
  35. }
  36. @Override
  37. protected void configure(HttpSecurity http) throws Exception {
  38. http.csrf().disable().anonymous().disable().authorizeRequests().antMatchers("/oauth/token").permitAll();
  39. }
  40. @Override
  41. @Bean
  42. public AuthenticationManager authenticationManagerBean() throws Exception {
  43. return super.authenticationManagerBean();
  44. }
  45. @Bean
  46. public TokenStore tokenStore() {
  47. return new InMemoryTokenStore();
  48. }
  49. @Bean
  50. @Autowired
  51. public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore) {
  52. TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
  53. handler.setTokenStore(tokenStore);
  54. handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
  55. handler.setClientDetailsService(clientDetailsService);
  56. return handler;
  57. }
  58. @Bean
  59. @Autowired
  60. public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
  61. TokenApprovalStore store = new TokenApprovalStore();
  62. store.setTokenStore(tokenStore);
  63. return store;
  64. }
  65. }

您能帮助我吗?

英文:

I need your help again. I need to authentificate to my app by JDBC or AD, but it must work all. For example, when I try to authentificate using JDBC user, my program has to connect with user from database, but when i put there AD login, it has to connect with AD user.

Here is my code, but when I have below order(1) code to connect to JDBC , I Can connect ONLY with JDBC and when i have there code to create authentificationProvider i can sign in just when i am using AD login.

  1. @Configuration
  2. @EnableWebSecurity
  3. public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {
  4. @Autowired
  5. private ClientDetailsService clientDetailsService;
  6. @Autowired
  7. private DataSource primaryDataSource;
  8. // @Autowired
  9. // private AuthenticationProvider authenticationProvider;
  10. // @Autowired
  11. // private AuthenticationProvider authenticationProviderAD;
  12. @Value("${security.authentication.provider}")
  13. private String authProvider;
  14. @Value("${ad.domain:#{null}}")
  15. private String adDomain;
  16. // TODO shift system like configuration into java, and no system like
  17. // configuration make it optional
  18. @Value("${ad.url:#{null}}")
  19. private String adUrl;
  20. @Override
  21. @Order(1)
  22. protected void configure(AuthenticationManagerBuilder auth) throws Exception {
  23. RecursiveAdProvider adProvider = new RecursiveAdProvider(adDomain, adUrl);
  24. adProvider.setConvertSubErrorCodesToExceptions(true);
  25. adProvider.setUseAuthenticationRequestCredentials(true);
  26. auth.authenticationProvider(adProvider);
  27. auth.eraseCredentials(false);
  28. }
  29. @Order(2)
  30. protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
  31. auth.jdbcAuthentication().dataSource(primaryDataSource)
  32. .usersByUsernameQuery("select username, password, enabled from users where username=?")
  33. .authoritiesByUsernameQuery(
  34. "select username, authority from user_authority JOIN authorities ON user_authority.authority_id = authorities.id where username=?")
  35. .passwordEncoder(new BCryptPasswordEncoder());
  36. }
  37. @Override
  38. protected void configure(HttpSecurity http) throws Exception {
  39. http.csrf().disable().anonymous().disable().authorizeRequests().antMatchers("/oauth/token").permitAll();
  40. }
  41. @Override
  42. @Bean
  43. public AuthenticationManager authenticationManagerBean() throws Exception {
  44. return super.authenticationManagerBean();
  45. }
  46. @Bean
  47. public TokenStore tokenStore() {
  48. return new InMemoryTokenStore();
  49. }
  50. @Bean
  51. @Autowired
  52. public TokenStoreUserApprovalHandler userApprovalHandler(TokenStore tokenStore) {
  53. TokenStoreUserApprovalHandler handler = new TokenStoreUserApprovalHandler();
  54. handler.setTokenStore(tokenStore);
  55. handler.setRequestFactory(new DefaultOAuth2RequestFactory(clientDetailsService));
  56. handler.setClientDetailsService(clientDetailsService);
  57. return handler;
  58. }
  59. @Bean
  60. @Autowired
  61. public ApprovalStore approvalStore(TokenStore tokenStore) throws Exception {
  62. TokenApprovalStore store = new TokenApprovalStore();
  63. store.setTokenStore(tokenStore);
  64. return store;
  65. }}

Can you help me?

huangapple
  • 本文由 发表于 2020年7月24日 19:41:00
  • 转载请务必保留本文链接:https://java.coder-hub.com/63072827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定