@Configuration @EnableTransactionManagement public class DatabaseConfig { @Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/library"); dataSource.setUsername("username"); dataSource.setPassword("password"); return dataSource; } @Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan("com.example.library.model"); Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); hibernateProperties.setProperty("hibernate.show_sql", "true"); sessionFactory.setHibernateProperties(hibernateProperties); return sessionFactory; } @Bean public HibernateTransactionManager transactionManager() { HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(sessionFactory().getObject()); return transactionManager; } } @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomUserDetailsService userDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/books/**").authenticated() .anyRequest().permitAll() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .logoutSuccessUrl("/") .permitAll(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } @Configuration public class ElasticsearchConfig { @Bean public RestHighLevelClient elasticsearchClient() { RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http")); RestHighLevelClient client = new RestHighLevelClient(builder); return client; } } @Repository public class BookRepository { @Autowired private RestHighLevelClient elasticsearchClient; public void index(Book book) throws IOException { IndexRequest request = new IndexRequest("library"); request.id(String.valueOf(book.getId())); request.source(new ObjectMapper().writeValueAsString(book), XContentType.JSON); IndexResponse response = elasticsearchClient.index(request, RequestOptions.DEFAULT); } public List<Book> search(String keyword) throws IOException { SearchRequest request = new SearchRequest("library"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchQuery("title", keyword)); request.source(sourceBuilder); SearchResponse response = elasticsearchClient.search(request, RequestOptions.DEFAULT); SearchHit[] hits = response.getHits().getHits(); List<Book> books = new ArrayList<>(); for (SearchHit hit : hits) { books.add(new ObjectMapper().readValue(hit.getSourceAsString(), Book.class)); } return books; } } public class BookService { @Autowired private BookRepository bookRepository; public void indexBook(Book book) throws IOException { bookRepository.index(book); } public List<Book> searchBooks(String keyword) throws IOException { return bookRepository.search(keyword); } }


上一篇:
下一篇:
切换中文