登录拦截,返回信息方式修改。
This commit is contained in:
+46
@@ -0,0 +1,46 @@
|
||||
package com.jero.common.exception;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.jero.common.api.vo.Result;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class LoginException {
|
||||
|
||||
private static HttpServletResponse response;
|
||||
|
||||
@Autowired
|
||||
public void setResponse(HttpServletResponse response){
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public static void loginException(Integer errorCode,String errorMessage){
|
||||
String reponseJson = new Gson().toJson(Result.error(errorCode,errorMessage));
|
||||
response.setContentType("application/json; charset=utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = response.getOutputStream();
|
||||
out.write(reponseJson.getBytes());
|
||||
}catch (IOException ex){
|
||||
ex.printStackTrace();
|
||||
}finally {
|
||||
if(out != null){
|
||||
try {
|
||||
out.close();
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
log.error(errorMessage);
|
||||
}
|
||||
}
|
||||
+19
-27
@@ -3,6 +3,8 @@ package com.jero.config.shiro;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import com.google.gson.Gson;
|
||||
import com.jero.common.api.vo.Result;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import com.jero.common.exception.LoginException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.shiro.authc.AuthenticationException;
|
||||
import org.apache.shiro.authc.AuthenticationInfo;
|
||||
@@ -47,8 +49,8 @@ public class ShiroRealm extends AuthorizingRealm {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Autowired
|
||||
private HttpServletResponse response;
|
||||
// @Autowired
|
||||
// private HttpServletResponse response;
|
||||
|
||||
/**
|
||||
* 必须重写此方法,不然Shiro会报错
|
||||
@@ -97,12 +99,13 @@ public class ShiroRealm extends AuthorizingRealm {
|
||||
* @throws AuthenticationException
|
||||
*/
|
||||
@Override
|
||||
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException {
|
||||
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth){
|
||||
log.debug("===============Shiro身份认证开始============doGetAuthenticationInfo==========");
|
||||
String token = (String) auth.getCredentials();
|
||||
if (token == null) {
|
||||
log.info("————————身份认证失败——————————IP地址: "+ oConvertUtils.getIpAddrByRequest(SpringContextUtils.getHttpServletRequest()));
|
||||
throw new AuthenticationException("token为空!");
|
||||
LoginException.loginException(401,"token为空");
|
||||
//throw new JeroBootException("token为空!");
|
||||
}
|
||||
// 校验token有效性
|
||||
LoginUser loginUser = this.checkUserTokenIsEffect(token);
|
||||
@@ -118,41 +121,30 @@ public class ShiroRealm extends AuthorizingRealm {
|
||||
// 解密获得username,用于和数据库进行对比
|
||||
String username = JwtUtil.getUsername(token);
|
||||
if (username == null) {
|
||||
throw new AuthenticationException("token非法无效!");
|
||||
LoginException.loginException(401,"token非法无效");
|
||||
return null;
|
||||
//throw new AuthenticationException("token非法无效!");
|
||||
}
|
||||
|
||||
// 查询用户信息
|
||||
log.debug("———校验token是否有效————checkUserTokenIsEffect——————— "+ token);
|
||||
LoginUser loginUser = commonAPI.getUserByName(username);
|
||||
if (loginUser == null) {
|
||||
throw new AuthenticationException("用户不存在!");
|
||||
LoginException.loginException(401,"用户不存在!");
|
||||
return null;
|
||||
//throw new AuthenticationException("用户不存在!");
|
||||
}
|
||||
// 判断用户状态
|
||||
if (loginUser.getStatus() != 1) {
|
||||
throw new AuthenticationException("账号已被锁定,请联系管理员!");
|
||||
LoginException.loginException(401,"账号已被锁定,请联系管理员!");
|
||||
return null;
|
||||
//throw new AuthenticationException("账号已被锁定,请联系管理员!");
|
||||
}
|
||||
// 校验token是否超时失效 & 或者账号密码是否错误
|
||||
if (!jwtTokenRefresh(token, username, loginUser.getPassword())) {
|
||||
String reponseJson = new Gson().toJson(Result.error(401,"Token失效,请重新登录!"));
|
||||
response.setContentType("application/json; charset=utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = response.getOutputStream();
|
||||
out.write(reponseJson.getBytes());
|
||||
}catch (IOException ex){
|
||||
ex.printStackTrace();
|
||||
}finally {
|
||||
if(out != null){
|
||||
try {
|
||||
out.close();
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new AuthenticationException("Token失效,请重新登录!");
|
||||
LoginException.loginException(401,"Token失效,请重新登录!");
|
||||
return null;
|
||||
//throw new AuthenticationException("Token失效,请重新登录!");
|
||||
}
|
||||
|
||||
return loginUser;
|
||||
|
||||
Reference in New Issue
Block a user