解决Spring Security 用户帐号已被锁定问题
程序员文章站
2022-07-09 17:23:31
1、问题描述主要就是org.springframework.security.authentication.lockedexception: 用户帐号已被锁定这个异常,完整异常如下:[2020-05-...
1、问题描述
主要就是org.springframework.security.authentication.lockedexception: 用户帐号已被锁定
这个异常,完整异常如下:
[2020-05-09 16:07:00 下午]:debug org.springframework.security.authentication.dao.abstractuserdetailsauthenticationprovider$defaultpreauthenticationchecks.check(abstractuserdetailsauthenticationprovider.java:353)user account is locked [2020-05-09 16:07:00 下午]:debug org.springframework.web.servlet.frameworkservlet.logresult(frameworkservlet.java:1101)failed to complete request: org.springframework.security.authentication.lockedexception: 用户帐号已被锁定 [2020-05-09 16:07:00 下午]:debug org.springframework.security.web.access.exceptiontranslationfilter.handlespringsecurityexception(exceptiontranslationfilter.java:170)authentication exception occurred; redirecting to authentication entry point org.springframework.security.authentication.lockedexception: 用户帐号已被锁定 at org.springframework.security.authentication.dao.abstractuserdetailsauthenticationprovider$defaultpreauthenticationchecks.check(abstractuserdetailsauthenticationprovider.java:355) at org.springframework.security.authentication.dao.abstractuserdetailsauthenticationprovider.authenticate(abstractuserdetailsauthenticationprovider.java:165) at org.springframework.security.authentication.providermanager.authenticate(providermanager.java:175) at cn.edu.njust.mango.security.securityutils.login(securityutils.java:82) at cn.edu.njust.mango.controller.syslogincontroller.login(syslogincontroller.java:104) at sun.reflect.nativemethodaccessorimpl.invoke0(native method) at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) at java.lang.reflect.method.invoke(method.java:498) at org.springframework.web.method.support.invocablehandlermethod.doinvoke(invocablehandlermethod.java:190) at org.springframework.web.method.support.invocablehandlermethod.invokeforrequest(invocablehandlermethod.java:138) at org.springframework.web.servlet.mvc.method.annotation.servletinvocablehandlermethod.invokeandhandle(servletinvocablehandlermethod.java:105) at org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.invokehandlermethod(requestmappinghandleradapter.java:879) at org.springframework.web.servlet.mvc.method.annotation.requestmappinghandleradapter.handleinternal(requestmappinghandleradapter.java:793) at org.springframework.web.servlet.mvc.method.abstracthandlermethodadapter.handle(abstracthandlermethodadapter.java:87) at org.springframework.web.servlet.dispatcherservlet.dodispatch(dispatcherservlet.java:1040) at org.springframework.web.servlet.dispatcherservlet.doservice(dispatcherservlet.java:943) at org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:1006) at org.springframework.web.servlet.frameworkservlet.dopost(frameworkservlet.java:909) at javax.servlet.http.httpservlet.service(httpservlet.java:660) at org.springframework.web.servlet.frameworkservlet.service(frameworkservlet.java:883) at javax.servlet.http.httpservlet.service(httpservlet.java:741) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:231) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:53) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at com.alibaba.druid.support.http.webstatfilter.dofilter(webstatfilter.java:123) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:320) at org.springframework.security.web.access.intercept.filtersecurityinterceptor.invoke(filtersecurityinterceptor.java:126) at org.springframework.security.web.access.intercept.filtersecurityinterceptor.dofilter(filtersecurityinterceptor.java:90) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.access.exceptiontranslationfilter.dofilter(exceptiontranslationfilter.java:118) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.session.sessionmanagementfilter.dofilter(sessionmanagementfilter.java:137) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.authentication.anonymousauthenticationfilter.dofilter(anonymousauthenticationfilter.java:111) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.servletapi.securitycontextholderawarerequestfilter.dofilter(securitycontextholderawarerequestfilter.java:158) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.savedrequest.requestcacheawarefilter.dofilter(requestcacheawarefilter.java:63) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at cn.edu.njust.mango.security.jwtauthenticationfilter.dofilterinternal(jwtauthenticationfilter.java:27) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.authentication.logout.logoutfilter.dofilter(logoutfilter.java:116) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.web.filter.corsfilter.dofilterinternal(corsfilter.java:92) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.header.headerwriterfilter.doheadersafter(headerwriterfilter.java:92) at org.springframework.security.web.header.headerwriterfilter.dofilterinternal(headerwriterfilter.java:77) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.context.securitycontextpersistencefilter.dofilter(securitycontextpersistencefilter.java:105) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.context.request.async.webasyncmanagerintegrationfilter.dofilterinternal(webasyncmanagerintegrationfilter.java:56) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119) at org.springframework.security.web.filterchainproxy$virtualfilterchain.dofilter(filterchainproxy.java:334) at org.springframework.security.web.filterchainproxy.dofilterinternal(filterchainproxy.java:215) at org.springframework.security.web.filterchainproxy.dofilter(filterchainproxy.java:178) at org.springframework.web.filter.delegatingfilterproxy.invokedelegate(delegatingfilterproxy.java:358) at org.springframework.web.filter.delegatingfilterproxy.dofilter(delegatingfilterproxy.java:271) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.web.filter.requestcontextfilter.dofilterinternal(requestcontextfilter.java:100) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.web.filter.formcontentfilter.dofilterinternal(formcontentfilter.java:93) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.springframework.web.filter.characterencodingfilter.dofilterinternal(characterencodingfilter.java:201) at org.springframework.web.filter.onceperrequestfilter.dofilter(onceperrequestfilter.java:119) at org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:193) at org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:166) at org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:202) at org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:96) at org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:541) at org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:139) at org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:92) at org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:74) at org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:343) at org.apache.coyote.http11.http11processor.service(http11processor.java:373) at org.apache.coyote.abstractprocessorlight.process(abstractprocessorlight.java:65) at org.apache.coyote.abstractprotocol$connectionhandler.process(abstractprotocol.java:868) at org.apache.tomcat.util.net.nioendpoint$socketprocessor.dorun(nioendpoint.java:1594) at org.apache.tomcat.util.net.socketprocessorbase.run(socketprocessorbase.java:49) at java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1149) at java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:624) at org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run(taskthread.java:61) at java.lang.thread.run(thread.java:748)
测试结果直接403
2、问题分析
明明用户名和密码正确,而且没有设置状态锁定,怎么被锁定了呢?这是由于我们在重写userdetails
接口时,有个默认实现的方法public boolean isaccountnonlocked()
,默认返回的是false
,翻译成人话就是:是否不上锁,否,即上锁。异常代码如下:
package cn.edu.njust.mango.security; import com.fasterxml.jackson.annotation.jsonignore; import lombok.data; import org.springframework.security.core.grantedauthority; import org.springframework.security.core.userdetails.userdetails; import java.util.collection; /** * @author chen * @version 1.0 * @date 2020/5/8 7:42 * @description: */ @data public class jwtuserdetails implements userdetails { private string username; private string password; private string salt; private collection<? extends grantedauthority> authorities; public jwtuserdetails(string username, string password, string salt, collection<? extends grantedauthority> authorities) { this.username = username; this.password = password; this.salt = salt; this.authorities = authorities; } @override public boolean isaccountnonexpired() { return false; } @override public boolean isaccountnonlocked() { return false; } @override public boolean iscredentialsnonexpired() { return false; } @override public boolean isenabled() { return false; } }
3、问题解决
知道原因就很好解决了。直接将返回值变成true
就行了。修改后的代码如下:
package cn.edu.njust.mango.security; import com.fasterxml.jackson.annotation.jsonignore; import lombok.data; import org.springframework.security.core.grantedauthority; import org.springframework.security.core.userdetails.userdetails; import java.util.collection; /** * @author chen * @version 1.0 * @date 2020/5/8 7:42 * @description: */ @data public class jwtuserdetails implements userdetails { private string username; private string password; private string salt; private collection<? extends grantedauthority> authorities; public jwtuserdetails(string username, string password, string salt, collection<? extends grantedauthority> authorities) { this.username = username; this.password = password; this.salt = salt; this.authorities = authorities; } // 在实体类向前台返回数据时用来忽略不想传递给前台的属性或接口。 @jsonignore @override public boolean isaccountnonexpired() { return true; } @jsonignore @override public boolean isaccountnonlocked() { return true; } @jsonignore @override public boolean iscredentialsnonexpired() { return true; } @jsonignore @override public boolean isenabled() { return true; } }
重启服务器再次访问。
返回200,访问成功!
4、总结
书上的代码直接运行绝大部分是对的,但是总有一些软件的更新使得作者无能为力。之前的api是对的,但是之后就废弃了或修改了是常有的事。所以我们需要跟踪源代码。这只是一个小小的问题,如果没有前辈的无私奉献,很难想象我们自己一天能学到多少内容。
到此这篇关于解决spring security 用户帐号已被锁定 问题的文章就介绍到这了,更多相关spring security 用户帐号已被锁定内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!