Как получить данные базы данных при загрузке приложения с помощью h2 db в конфигурации Spring Resourse и передать эти данные для antMatcher и доступа.
Бывший: открытый класс ResourceServerConfig расширяет ResourceServerConfigurerAdapter {
public void configure (HttpSecurity http) выдает исключение {
System.out.println(dataSource.getConnection());
//here i am getting db object
http.requestMatchers().and().authorizeRequests()
.antMatchers("/bitcash/profile/find-user**").access("hasRole('ROLE_USER')")
.antMatchers("/bitcash/profile/find-admin**").access("hasRole('ROLE_ADMIN')").and()
.exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
http.csrf().disable();
http.headers().frameOptions().disable();
}
}
Пожалуйста, помогите мне выбраться из этого.
public void configure (HttpSecurity http) выдает исключение {
System.out.println(repo.getResourse());
System.out.println(dataSource.getConnection());
http.requestMatchers().and().authorizeRequests()
.antMatchers("/bitcash/profile/find-user**").access("hasRole('ROLE_USER')")
.antMatchers("/bitcash/profile/find-admin**").access("hasRole('ROLE_ADMIN')").and()
.exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
http.csrf().disable();
http.headers().frameOptions().disable();
}




Spring Web предоставляет следующий интерфейс
public interface RequestMatcher {
boolean matches(HttpServletRequest request);
}
и Spring Security вы можете настроить его с помощью
http
.authorizeRequests()
.requestMatchers(new MyMatcher(db), someOtherMatcher)
.fullyAuthenticated()
Как вы реализуете мой сопоставитель, полностью зависит от вас. Возможно
public class MyMatcher implements RequestMatcher {
@Override
public boolean matches(HttpServletRequest request) {
String antPatternFromDB = db.getAntPattern(request);
return new AntPathRequestMatcher(antPatternFromDB).matches(request);
}
}
Это предполагает, что db является bean-компонентом с доступом к БД.
@Bean
public Db db() {
return new Db(DriverManager.getConnection("jdbc:h2:~/test", "sa", ""));
}
Обратите внимание: вместо DriverManager.getConnection следует использовать пул соединений. Я считаю, что это выходит за рамки этого ответа.