解决数据库异常 登录提示信息有误的问题;

This commit is contained in:
terrfly 2021-06-15 11:50:32 +08:00
parent 7ac96de72f
commit 0268fb97e0
5 changed files with 82 additions and 15 deletions

View File

@ -0,0 +1,54 @@
/*
* Copyright (c) 2021-2031, 河北计全科技有限公司 (https://www.jeequan.com & jeequan@126.com).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jeequan.jeepay.core.exception;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
/*
* Spring Security 框架自定义异常类
*
* @author terrfly
* @site https://www.jeepay.vip
* @date 2021/6/15 11:23
*/
@Getter
@Setter
public class JeepayAuthenticationException extends InternalAuthenticationServiceException {
private BizException bizException;
public JeepayAuthenticationException(String msg, Throwable cause) {
super(msg, cause);
}
public JeepayAuthenticationException(String msg) {
super(msg);
}
public static JeepayAuthenticationException build(String msg){
return build(new BizException(msg));
}
public static JeepayAuthenticationException build(BizException ex){
JeepayAuthenticationException result = new JeepayAuthenticationException(ex.getMessage());
result.setBizException(ex);
return result;
}
}

View File

@ -18,11 +18,11 @@ package com.jeequan.jeepay.mgr.secruity;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.entity.SysUserAuth;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.exception.JeepayAuthenticationException;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import com.jeequan.jeepay.core.utils.RegKit;
import com.jeequan.jeepay.service.impl.SysUserAuthService;
import com.jeequan.jeepay.service.impl.SysUserService;
import com.jeequan.jeepay.core.utils.RegKit;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
@ -31,7 +31,7 @@ import org.springframework.stereotype.Service;
/*
* 实现UserDetailsService 接口
*
*
* @author terrfly
* @site https://www.jeepay.vip
* @date 2021/6/8 17:13
@ -65,7 +65,7 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUserAuth auth = sysUserAuthService.selectByLogin(loginUsernameStr, identityType, CS.SYS_TYPE.MGR);
if(auth == null){ //没有该用户信息
throw new BizException("用户名/密码错误!");
throw JeepayAuthenticationException.build("用户名/密码错误!");
}
//用户ID
@ -74,11 +74,11 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUser sysUser = sysUserService.getById(userId);
if (sysUser == null) {
throw new BizException("用户名/密码错误!");
throw JeepayAuthenticationException.build("用户名/密码错误!");
}
if(CS.PUB_USABLE != sysUser.getState()){ //状态不合法
throw new BizException("用户状态不可登录,请联系管理员!");
throw JeepayAuthenticationException.build("用户状态不可登录,请联系管理员!");
}
return new JeeUserDetails(sysUser, auth.getCredential());

View File

@ -21,6 +21,7 @@ import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.cache.RedisUtil;
import com.jeequan.jeepay.core.exception.JeepayAuthenticationException;
import com.jeequan.jeepay.core.jwt.JWTPayload;
import com.jeequan.jeepay.core.jwt.JWTUtils;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
@ -29,6 +30,7 @@ import com.jeequan.jeepay.service.impl.SysRoleEntRelaService;
import com.jeequan.jeepay.service.impl.SysRoleService;
import com.jeequan.jeepay.service.impl.SysUserService;
import com.jeequan.jeepay.service.mapper.SysEntitlementMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -36,6 +38,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -48,6 +51,7 @@ import java.util.*;
* @site https://www.jeepay.vip
* @date 2021/6/8 17:12
*/
@Slf4j
@Service
public class AuthService {
@ -75,8 +79,11 @@ public class AuthService {
Authentication authentication = null;
try {
authentication = authenticationManager.authenticate(upToken);
} catch (JeepayAuthenticationException jex) {
throw jex.getBizException() == null ? new BizException(jex.getMessage()) : jex.getBizException();
} catch (AuthenticationException e) {
throw new BizException("用户名或密码有误!");
log.error("AuthenticationException:", e);
throw new BizException("认证服务出现异常, 请重试或联系系统管理员!");
}
JeeUserDetails jeeUserDetails = (JeeUserDetails) authentication.getPrincipal();

View File

@ -18,11 +18,11 @@ package com.jeequan.jeepay.mch.secruity;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.entity.SysUserAuth;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.exception.JeepayAuthenticationException;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import com.jeequan.jeepay.core.utils.RegKit;
import com.jeequan.jeepay.service.impl.SysUserAuthService;
import com.jeequan.jeepay.service.impl.SysUserService;
import com.jeequan.jeepay.core.utils.RegKit;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
@ -66,7 +66,7 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUserAuth auth = sysUserAuthService.selectByLogin(loginUsernameStr, identityType, CS.SYS_TYPE.MCH);
if(auth == null){ //没有该用户信息
throw new BizException("用户名/密码错误!");
throw JeepayAuthenticationException.build("用户名/密码错误!");
}
//用户ID
@ -75,11 +75,11 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUser sysUser = sysUserService.getById(userId);
if (sysUser == null) {
throw new BizException("用户名/密码错误!");
throw JeepayAuthenticationException.build("用户名/密码错误!");
}
if(CS.PUB_USABLE != sysUser.getState()){ //状态不合法
throw new BizException("用户状态不可登录,请联系管理员!");
throw JeepayAuthenticationException.build("用户状态不可登录,请联系管理员!");
}
return new JeeUserDetails(sysUser, auth.getCredential());

View File

@ -22,6 +22,7 @@ import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchInfo;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.exception.JeepayAuthenticationException;
import com.jeequan.jeepay.core.jwt.JWTPayload;
import com.jeequan.jeepay.core.jwt.JWTUtils;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
@ -31,6 +32,7 @@ import com.jeequan.jeepay.service.impl.SysRoleEntRelaService;
import com.jeequan.jeepay.service.impl.SysRoleService;
import com.jeequan.jeepay.service.impl.SysUserService;
import com.jeequan.jeepay.service.mapper.SysEntitlementMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@ -50,6 +52,7 @@ import java.util.*;
* @site https://www.jeepay.vip
* @date 2021-04-27 15:50
*/
@Slf4j
@Service
public class AuthService {
@ -78,8 +81,11 @@ public class AuthService {
Authentication authentication = null;
try {
authentication = authenticationManager.authenticate(upToken);
} catch (JeepayAuthenticationException jex) {
throw jex.getBizException() == null ? new BizException(jex.getMessage()) : jex.getBizException();
} catch (AuthenticationException e) {
throw new BizException("用户名或密码有误!");
log.error("AuthenticationException:", e);
throw new BizException("认证服务出现异常, 请重试或联系系统管理员!");
}
JeeUserDetails jeeUserDetails = (JeeUserDetails) authentication.getPrincipal();