From 352c837505a0564ce025f456bf6abf178756904e Mon Sep 17 00:00:00 2001 From: Tang Cheng Date: Wed, 10 Jul 2019 11:44:23 +0800 Subject: [PATCH] =?utf8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?utf8?q?=E5=8E=BB=E6=8E=89warning?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../service/impl/MobileUserServiceImpl.kt | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/mobile/service/impl/MobileUserServiceImpl.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/mobile/service/impl/MobileUserServiceImpl.kt index 197da104..2878eb32 100644 --- a/payapi/src/main/kotlin/com/supwisdom/dlpay/mobile/service/impl/MobileUserServiceImpl.kt +++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/mobile/service/impl/MobileUserServiceImpl.kt @@ -4,7 +4,7 @@ import com.supwisdom.dlpay.mobile.dao.MobileUserDao import com.supwisdom.dlpay.mobile.domain.TBMobileUser import com.supwisdom.dlpay.mobile.exception.UserLoginFailException import com.supwisdom.dlpay.mobile.service.MobileUserService -import org.slf4j.LoggerFactory +import mu.KotlinLogging import org.springframework.beans.factory.annotation.Autowired import org.springframework.security.core.GrantedAuthority import org.springframework.security.core.authority.AuthorityUtils @@ -14,42 +14,44 @@ import org.springframework.stereotype.Service @Service class MobileUserServiceImpl : MobileUserService { - var logger = LoggerFactory.getLogger(MobileUserServiceImpl::class.java) + companion object { + const val TIME_INTERVAL = 1000 * 6 * 30 + } + + val logger = KotlinLogging.logger { } @Autowired lateinit var mobileUserDao: MobileUserDao override fun loadUserByUsername(username: String?): UserDetails { - logger.error("username:"+username) - if(username.isNullOrEmpty()){ + logger.error("username:$username") + if (username.isNullOrEmpty()) { throw UsernameNotFoundException("用户不存在") } - var temp = mobileUserDao.findByPhone(username!!) - if(temp!=null) { - if(temp.loginpwd.isNullOrEmpty()){ + val temp = mobileUserDao.findByPhone(username) + if (temp != null) { + if (temp.loginpwd.isEmpty()) { throw UserLoginFailException("用户注册后未设置登录密码,请找回密码或重新注册") } - if (temp.loginpwderror != null && temp.loginpwderror!! >= 5 && (System.currentTimeMillis() - temp.loginpwderrortime!!) < 1000 * 60 * 30) { + if (temp.loginpwderror >= 5 && (System.currentTimeMillis() - temp.loginpwderrortime!!) < TIME_INTERVAL) { throw UserLoginFailException("密码错误次数过多,请30分钟后再试") - } else if (temp.loginpwderror != null && temp.loginpwderror!! >= 5 && (System.currentTimeMillis() - temp.loginpwderrortime!!) > 1000 * 60 * 30) { + } else if (temp.loginpwderror >= 5 && (System.currentTimeMillis() - temp.loginpwderrortime!!) > TIME_INTERVAL) { //更新时间 temp.loginpwderror = 0 temp.loginpwderrortime = null mobileUserDao.save(temp) } - var authorities: Collection = AuthorityUtils.createAuthorityList("ROLE_USER") + val authorities: Collection = AuthorityUtils.createAuthorityList("ROLE_USER") temp.auths = authorities - }else{ + } else { throw UsernameNotFoundException("用户不存在") } return temp } override fun getByUid(uid: String): TBMobileUser? { - var t = mobileUserDao.findById(uid) - if(t.isPresent){ - return t.get() + return mobileUserDao.findById(uid).let { + if (it.isPresent) it.get() else null } - return null } } \ No newline at end of file -- 2.17.1