我有一个使用 Spring Security 进行身份验证和授权的 Web 应用程序。
在我的 securityConfiguration 文件中,我有这些行
@Autowired
private DataSource dataSource;
@Value("${spring.queries.users-query}")
private String usersQuery;
@Value("${spring.queries.roles-query}")
private String rolesQuery;
稍后用于身份验证。
在 application.properties 文件中,我有:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.jpa.show-sql= true
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false
spring.queries.users-query=select email, password from users where email=?
spring.queries.roles-query=select u.email, r.role from users u inner join
user_role ur on(u.user_id=ur.user_id) inner join role r
on(ur.role_id=r.role_id) where u.email=?
但我得到:'spring.datasource.testWhileIdle' 是一个未知的属性。你的意思是“spring.datasource.tomcat.abandon-when-percentage-full”?
还有一个关于 spring.validationQuery 以及 spring.queries 的错误......它是黄色下划线。
所以登录过程总是返回false。
谁能帮我找出问题出在哪里?我是 Spring 框架的新手,任何帮助将不胜感激。
最佳答案
假设您使用的是连接池数据库请更改
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
对此:
spring.datasource.tomcat.test-while-idle=true
spring.datasource.tomcat.validation-query=SELECT 1
您可以在 Spring Documentation - Working with Database. 中查看更多相关信息。
也改
spring.queries.users-query
和 spring.queries.roles-query
至 myapp.queries.users-query
和 myapp.queries.roles-query
分别。所有以 spring.*
开头的属性是标准属性,所有可用的标准属性都列在 Spring Documentation 中.如果您需要使用自定义属性更改名称,例如使用 myapp.mycustomproperty
关于application.properties 中的 Spring.queries 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45901031/