inMemoryAuthentication()工作正常。但我是春季安全新手,我想從MySQL數據庫進行身份驗證。使用jdbcAuthentication時Spring Security登錄不起作用
.inMemoryAuthentication()
.withUser("[email protected]").password("vikram").roles("ADMIN");
我需要完全一樣的東西,但從MySQL表。我需要使用登錄頁面上輸入的用戶標識進行身份驗證。我嘗試以下
.jdbcAuthentication().usersByUsernameQuery("select user_id, password from logindetails where user_id=?");
和堆棧跟蹤是:
ERROR 8608 --- [io-9393-exec-10] w.a.UsernamePasswordAu
henticationFilter : An internal error occurred while trying to authenticate the
user.
org.springframework.security.authentication.InternalAuthenticationServiceExcept
on: null
at org.springframework.security.authentication.dao.DaoAuthenticationPro
ider.retrieveUser(DaoAuthenticationProvider.java:126) ~[spring-security-core-4.
.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.security.authentication.dao.AbstractUserDetailsA
thenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.jav
:143) ~[spring-security-core-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.security.authentication.ProviderManager.authenti
ate(ProviderManager.java:167) ~[spring-security-core-4.0.3.RELEASE.jar!/:4.0.3.
ELEASE]
at org.springframework.security.authentication.ProviderManager.authenti
ate(ProviderManager.java:192) ~[spring-security-core-4.0.3.RELEASE.jar!/:4.0.3.
ELEASE]
at org.springframework.security.web.authentication.UsernamePasswordAuth
nticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java
93) ~[spring-security-web-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.security.web.authentication.AbstractAuthenticati
nProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:217) ~[s
ring-security-web-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain
doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.3.RELEASE.jar!/:4.
.3.RELEASE]
at org.springframework.security.web.authentication.logout.LogoutFilter.
oFilter(LogoutFilter.java:120) [spring-security-web-4.0.3.RELEASE.jar!/:4.0.3.R
LEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain
doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.3.RELEASE.jar!/:4.
.3.RELEASE]
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(Cs
fFilter.java:120) [spring-security-web-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePer
equestFilter.java:107) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain
doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.3.RELEASE.jar!/:4.
.3.RELEASE]
at org.springframework.security.web.header.HeaderWriterFilter.doFilterI
ternal(HeaderWriterFilter.java:64) [spring-security-web-4.0.3.RELEASE.jar!/:4.0
3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePer
equestFilter.java:107) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain
doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.3.RELEASE.jar!/:4.
.3.RELEASE]
at org.springframework.security.web.context.SecurityContextPersistenceF
lter.doFilter(SecurityContextPersistenceFilter.java:91) [spring-security-web-4.
.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain
doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.3.RELEASE.jar!/:4.
.3.RELEASE]
at org.springframework.security.web.context.request.async.WebAsyncManag
rIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53) [
pring-security-web-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePer
equestFilter.java:107) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain
doFilter(FilterChainProxy.java:330) [spring-security-web-4.0.3.RELEASE.jar!/:4.
.3.RELEASE]
at org.springframework.security.web.FilterChainProxy.doFilterInternal(F
lterChainProxy.java:213) [spring-security-web-4.0.3.RELEASE.jar!/:4.0.3.RELEASE
at org.springframework.security.web.FilterChainProxy.doFilter(FilterCha
nProxy.java:176) [spring-security-web-4.0.3.RELEASE.jar!/:4.0.3.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(
elegatingFilterProxy.java:346) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(Delega
ingFilterProxy.java:262) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(App
icationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(Application
ilterChain.java:206) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal
RequestContextFilter.java:99) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePer
equestFilter.java:107) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(App
icationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(Application
ilterChain.java:206) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInte
nal(HttpPutFormContentFilter.java:87) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.REL
ASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePer
equestFilter.java:107) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(App
icationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(Application
ilterChain.java:206) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterIntern
l(HiddenHttpMethodFilter.java:77) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePer
equestFilter.java:107) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(App
icationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(Application
ilterChain.java:206) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInter
al(CharacterEncodingFilter.java:121) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELE
SE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePer
equestFilter.java:107) [spring-web-4.2.4.RELEASE.jar!/:4.2.4.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(App
icationFilterChain.java:239) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(Application
ilterChain.java:206) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapper
alve.java:212) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContext
alve.java:106) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentic
torBase.java:502) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.
ava:141) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.
ava:79) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVa
ve.java:88) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.ja
a:521) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHtt
11Processor.java:1096) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process
AbstractProtocol.java:674) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndp
int.java:1500) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoi
t.java:1456) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [n
:1.8.0_66]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [
a:1.8.0_66]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskT
read.java:61) [tomcat-embed-core-8.0.30.jar!/:8.0.30]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_66]
Caused by: java.lang.NullPointerException: null
at org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl.loadU
ersByUsername(JdbcDaoImpl.java:216) ~[spring-security-core-4.0.3.RELEASE.jar!/:
.0.3.RELEASE]
at org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl.loadU
erByUsername(JdbcDaoImpl.java:173) ~[spring-security-core-4.0.3.RELEASE.jar!/:4
0.3.RELEASE]
at org.springframework.security.authentication.dao.DaoAuthenticationPro
ider.retrieveUser(DaoAuthenticationProvider.java:114) ~[spring-security-core-4.
.3.RELEASE.jar!/:4.0.3.RELEASE]
你們能幫我什麼我做錯
你有沒有設置數據源? – holmis83
沒有我根本沒有設置數據源配置。你能告訴我該怎麼做才能做到這一點 – Vikram
你的查詢是錯誤的。它需要3列(用戶名,密碼,啓用),而不是2.因此,重寫您的查詢'選擇user_id作爲用戶名,密碼,1從logindetails其中user_id =?'啓用。 –