【problem】
We are creating a search function based on pagination in Spring.
When you put the characters "%" and "_" in the search box,
"%" and "_" are not searched, and all search results are acquired.
I think it's probably due to being identified as an escape character,
I'm not sure how to correct the code.
Please tell me to someone.
【progress】
・ JPA is used to obtain data from the DB, and it is possible to display without problems other than the escape character in question.
・ Pagination coding is also over, and I found [problem] at the final verification stage.
[Language/Tool]
・Java
・MYSQL
・Spring Boot
・STS (spring tools site)
【Source code】
Reference code
・TaskController(Controller)
・TaskRepository(Repository)
*Reference code will be added according to the answer
TaskController(Controller)
@Service
@Transactional
public class TaskService {
@Autowired
private TaskRepository taskrepository;
@Autowired
private TaskSpecification taskspecification;
public Page<TaskEntity>searchmaterial(String title, String titleKana,Pageable pageable) {
return (Page<TaskEntity>) taskrepository.findAll(Specification
.where(taskspecification.titleContains(title))
.and(taskspecification.titleKanaContains(titleKana))
,pageable
);
}
}
TaskRepository(Repository)
@Repository
public interface TaskRepository extends JpaRepository<TaskEntity,Long>, JpaSpecificationExecutor<TaskEntity>{
Page<TaskEntity>findByTitleAndTitleKana(String title, String titleKana, Pageable pageable);
}
-
Answer # 1
-
Answer # 2
Yasumichi
xebme
Thank you for your polite answer.
I tried to correct it according to the contents you received, but it seems that it can be solved using org.apache.commons.lang3.StringUtils, so by replacing the escape characters "%" "_" with StringUtils, the problem is solved did.
I'm really thankful to you.
Related articles
- java - i want to build an environment of httpd + tomacat using docker-compose, but the host name cannot be used in the address a
- java - okhttp cannot be imported in android
- java - cannot pass data (screen transition) using servlet
- java - cannot get data from a column that should exist ### cause: orgpostgresqlutilpsqlexception: error: column "s002"
- [java] cannot pass values by jsp/servlet
- java - cannot declare scanner twice
- java - cannot get value from postgresql
- java - i want to perform fuzzy search using bind variables
- java - search multiple items about how to write findby 〇 and △ like()
- java - fragmenttransaction transaction = fragmentmanagerbegintransaction() cannot be done
- java - about fuzzy search function of spring boot
- cannot connect google analytics and search console
- cannot standardize java code
- java - i want to know the reason why it cannot be executed with eclipse and the solution
- java - what is the form to hold the processing to get the search result in arraylist?
- cannot switch java version with jenv
- java - cannot get menu fxid
- java - cannot enter data in double type
- java - about generics of binary search
- How to pull data from a database (MySQL) using PHP?
- How to extract data from MySQL using php in java?
- how to sum column values in mysql database in java
- Question marks when writing to mySQL (SQL). Java
- java : In which class is the interaction with the web application performed?
- java : How to check if the same login exists when registering in the database?
- java : Cascading delete in Spring
- java : I want to divide the data source for each user in the multi-tenant service
- java : collect Map
from DB data - Java. Long data retrieval with ResultSet mysql
★★★ Note ★★★
As pointed out by xebme, allowing "%" and "_" in the user input risks SQL injection. I haven't scrutinized that the links below are appropriate, but I hope you find them helpful.
Complete SQL injection protection – yohgaki's blog
Java to be worried about, but I can't end it. Spring Data JPA JPQL injection countermeasures
Please understand the following as an answer to the need to use it in a limited place where you are aware of such risks and do not disclose it to the outside.
[First answer]
I feel that the method name is not for LIKE search.
[Spring Data JPA] Naming rules for automatically implemented methods-Per Qiita, isn't it helpful?
[Addition]
Since TaskService is calling taskrepository.findAll(), it seems that it needs to be modified to use the method additionally defined in TaskRepository.
[Additional Part 2]
Below, I have not confirmed the operation. Please check the operation when corrected as follows.
However, with this modification, the partial match search in the input that does not use "%" and "_" can be used.
xebme
As you can see, you will need a conditional branch.