У меня есть работающая реализация сервера аутентификации OAuth2, в которой используются JWT.Однако теперь я хотел бы перейти на использование хранилища JDBC (вместо хранилища JWT).Тем не менее, я сталкиваюсь с проблемами при попытке сделать переключатель.Если я выберу следующий подход:
@EnableAuthorizationServer
@Configuration
public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private TokenStore tokenStore;
// Bcrypt, specified in AppConfig
@Autowired
private PasswordEncoder passwordEncoder;
@Value("${spring.datasource.url}")
private String url;
@Value("${spring.datasource.username")
private String userName;
@Value("${spring.datasource.password}")
private String password;
@Value("${spring.datasource.driverClassName}")
private String driverName;
private final AppConfig appConfig;
@Autowired
private OAuthUserDetailsService userService;
private AuthenticationManager authenticationManager;
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverName);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource());
}
, я получу следующее:
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'entityManagerFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?
Исходя из моих исследований, кажется, что проблема в том, что я JPA / Hibernate в моемприложение тоже (в первую очередь, чтобы настроить мои UserDetails и роли).Для быстрого примера:
// Reference: http://www.baeldung.com/role-and-privilege-for-spring-security-registration
@Service
public class OAuthUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository userRepo;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
OAuthUser user = userRepo.findByUserName(username);
if(user == null) {
throw new UsernameNotFoundException("Could not find " + username);
}
UserDetails details = new User(user.getUserName(), user.getPassword(), getGrantedAuthorities(user.getRoles()));
return details;
}
private List<GrantedAuthority> getGrantedAuthorities(List<Role> roles) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
if(roles != null) {
for(Role role: roles) {
authorities.add(new SimpleGrantedAuthority(role.getRoleTitle()));
}
}
return authorities; }
}
Как можно использовать JdbcTokenStore
, если также используется Spring JPA?Мои соответствующие application.properties следующие:
spring.datasource.url=jdbc:postgresql://xxx
spring.datasource.username=x
spring.datasource.password=x
spring.datasource.driverClassName=org.postgresql.Driver
security.oauth2.resource.filter-order = 3
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
Спасибо за любые рекомендации.