Commit 0268fb97 authored by terrfly's avatar terrfly
Browse files

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

parent 7ac96de7
/*
* 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;
}
}
...@@ -18,11 +18,11 @@ package com.jeequan.jeepay.mgr.secruity; ...@@ -18,11 +18,11 @@ package com.jeequan.jeepay.mgr.secruity;
import com.jeequan.jeepay.core.constants.CS; import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.SysUser; import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.entity.SysUserAuth; 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.SysUserAuthService;
import com.jeequan.jeepay.service.impl.SysUserService; 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.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
...@@ -31,7 +31,7 @@ import org.springframework.stereotype.Service; ...@@ -31,7 +31,7 @@ import org.springframework.stereotype.Service;
/* /*
* 实现UserDetailsService 接口 * 实现UserDetailsService 接口
* *
* @author terrfly * @author terrfly
* @site https://www.jeepay.vip * @site https://www.jeepay.vip
* @date 2021/6/8 17:13 * @date 2021/6/8 17:13
...@@ -65,7 +65,7 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService { ...@@ -65,7 +65,7 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUserAuth auth = sysUserAuthService.selectByLogin(loginUsernameStr, identityType, CS.SYS_TYPE.MGR); SysUserAuth auth = sysUserAuthService.selectByLogin(loginUsernameStr, identityType, CS.SYS_TYPE.MGR);
if(auth == null){ //没有该用户信息 if(auth == null){ //没有该用户信息
throw new BizException("用户名/密码错误!"); throw JeepayAuthenticationException.build("用户名/密码错误!");
} }
//用户ID //用户ID
...@@ -74,11 +74,11 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService { ...@@ -74,11 +74,11 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUser sysUser = sysUserService.getById(userId); SysUser sysUser = sysUserService.getById(userId);
if (sysUser == null) { if (sysUser == null) {
throw new BizException("用户名/密码错误!"); throw JeepayAuthenticationException.build("用户名/密码错误!");
} }
if(CS.PUB_USABLE != sysUser.getState()){ //状态不合法 if(CS.PUB_USABLE != sysUser.getState()){ //状态不合法
throw new BizException("用户状态不可登录,请联系管理员!"); throw JeepayAuthenticationException.build("用户状态不可登录,请联系管理员!");
} }
return new JeeUserDetails(sysUser, auth.getCredential()); return new JeeUserDetails(sysUser, auth.getCredential());
......
...@@ -21,6 +21,7 @@ import com.jeequan.jeepay.core.constants.CS; ...@@ -21,6 +21,7 @@ import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.SysUser; import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.exception.BizException; import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.cache.RedisUtil; 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.JWTPayload;
import com.jeequan.jeepay.core.jwt.JWTUtils; import com.jeequan.jeepay.core.jwt.JWTUtils;
import com.jeequan.jeepay.core.model.security.JeeUserDetails; import com.jeequan.jeepay.core.model.security.JeeUserDetails;
...@@ -29,6 +30,7 @@ import com.jeequan.jeepay.service.impl.SysRoleEntRelaService; ...@@ -29,6 +30,7 @@ import com.jeequan.jeepay.service.impl.SysRoleEntRelaService;
import com.jeequan.jeepay.service.impl.SysRoleService; import com.jeequan.jeepay.service.impl.SysRoleService;
import com.jeequan.jeepay.service.impl.SysUserService; import com.jeequan.jeepay.service.impl.SysUserService;
import com.jeequan.jeepay.service.mapper.SysEntitlementMapper; import com.jeequan.jeepay.service.mapper.SysEntitlementMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
...@@ -36,6 +38,7 @@ import org.springframework.security.core.Authentication; ...@@ -36,6 +38,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -48,6 +51,7 @@ import java.util.*; ...@@ -48,6 +51,7 @@ import java.util.*;
* @site https://www.jeepay.vip * @site https://www.jeepay.vip
* @date 2021/6/8 17:12 * @date 2021/6/8 17:12
*/ */
@Slf4j
@Service @Service
public class AuthService { public class AuthService {
...@@ -75,8 +79,11 @@ public class AuthService { ...@@ -75,8 +79,11 @@ public class AuthService {
Authentication authentication = null; Authentication authentication = null;
try { try {
authentication = authenticationManager.authenticate(upToken); authentication = authenticationManager.authenticate(upToken);
} catch (JeepayAuthenticationException jex) {
throw jex.getBizException() == null ? new BizException(jex.getMessage()) : jex.getBizException();
} catch (AuthenticationException e) { } catch (AuthenticationException e) {
throw new BizException("用户名或密码有误!"); log.error("AuthenticationException:", e);
throw new BizException("认证服务出现异常, 请重试或联系系统管理员!");
} }
JeeUserDetails jeeUserDetails = (JeeUserDetails) authentication.getPrincipal(); JeeUserDetails jeeUserDetails = (JeeUserDetails) authentication.getPrincipal();
......
...@@ -18,11 +18,11 @@ package com.jeequan.jeepay.mch.secruity; ...@@ -18,11 +18,11 @@ package com.jeequan.jeepay.mch.secruity;
import com.jeequan.jeepay.core.constants.CS; import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.SysUser; import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.entity.SysUserAuth; 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.SysUserAuthService;
import com.jeequan.jeepay.service.impl.SysUserService; 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.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UserDetailsService;
...@@ -66,7 +66,7 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService { ...@@ -66,7 +66,7 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUserAuth auth = sysUserAuthService.selectByLogin(loginUsernameStr, identityType, CS.SYS_TYPE.MCH); SysUserAuth auth = sysUserAuthService.selectByLogin(loginUsernameStr, identityType, CS.SYS_TYPE.MCH);
if(auth == null){ //没有该用户信息 if(auth == null){ //没有该用户信息
throw new BizException("用户名/密码错误!"); throw JeepayAuthenticationException.build("用户名/密码错误!");
} }
//用户ID //用户ID
...@@ -75,11 +75,11 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService { ...@@ -75,11 +75,11 @@ public class JeeUserDetailsServiceImpl implements UserDetailsService {
SysUser sysUser = sysUserService.getById(userId); SysUser sysUser = sysUserService.getById(userId);
if (sysUser == null) { if (sysUser == null) {
throw new BizException("用户名/密码错误!"); throw JeepayAuthenticationException.build("用户名/密码错误!");
} }
if(CS.PUB_USABLE != sysUser.getState()){ //状态不合法 if(CS.PUB_USABLE != sysUser.getState()){ //状态不合法
throw new BizException("用户状态不可登录,请联系管理员!"); throw JeepayAuthenticationException.build("用户状态不可登录,请联系管理员!");
} }
return new JeeUserDetails(sysUser, auth.getCredential()); return new JeeUserDetails(sysUser, auth.getCredential());
......
...@@ -22,6 +22,7 @@ import com.jeequan.jeepay.core.constants.CS; ...@@ -22,6 +22,7 @@ import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchInfo; import com.jeequan.jeepay.core.entity.MchInfo;
import com.jeequan.jeepay.core.entity.SysUser; import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.exception.BizException; 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.JWTPayload;
import com.jeequan.jeepay.core.jwt.JWTUtils; import com.jeequan.jeepay.core.jwt.JWTUtils;
import com.jeequan.jeepay.core.model.security.JeeUserDetails; import com.jeequan.jeepay.core.model.security.JeeUserDetails;
...@@ -31,6 +32,7 @@ import com.jeequan.jeepay.service.impl.SysRoleEntRelaService; ...@@ -31,6 +32,7 @@ import com.jeequan.jeepay.service.impl.SysRoleEntRelaService;
import com.jeequan.jeepay.service.impl.SysRoleService; import com.jeequan.jeepay.service.impl.SysRoleService;
import com.jeequan.jeepay.service.impl.SysUserService; import com.jeequan.jeepay.service.impl.SysUserService;
import com.jeequan.jeepay.service.mapper.SysEntitlementMapper; import com.jeequan.jeepay.service.mapper.SysEntitlementMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
...@@ -50,6 +52,7 @@ import java.util.*; ...@@ -50,6 +52,7 @@ import java.util.*;
* @site https://www.jeepay.vip * @site https://www.jeepay.vip
* @date 2021-04-27 15:50 * @date 2021-04-27 15:50
*/ */
@Slf4j
@Service @Service
public class AuthService { public class AuthService {
...@@ -78,8 +81,11 @@ public class AuthService { ...@@ -78,8 +81,11 @@ public class AuthService {
Authentication authentication = null; Authentication authentication = null;
try { try {
authentication = authenticationManager.authenticate(upToken); authentication = authenticationManager.authenticate(upToken);
} catch (JeepayAuthenticationException jex) {
throw jex.getBizException() == null ? new BizException(jex.getMessage()) : jex.getBizException();
} catch (AuthenticationException e) { } catch (AuthenticationException e) {
throw new BizException("用户名或密码有误!"); log.error("AuthenticationException:", e);
throw new BizException("认证服务出现异常, 请重试或联系系统管理员!");
} }
JeeUserDetails jeeUserDetails = (JeeUserDetails) authentication.getPrincipal(); JeeUserDetails jeeUserDetails = (JeeUserDetails) authentication.getPrincipal();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment