|
@@ -1,5 +1,8 @@
|
|
|
package com.xiaoan.web.shiro;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.xiaoan.common.exception.BaseRuntimeException;
|
|
|
+import com.xiaoan.common.exception.JwtAuthenticationException;
|
|
|
import lombok.extern.log4j.Log4j2;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.apache.shiro.authc.AuthenticationException;
|
|
@@ -12,6 +15,7 @@ import javax.servlet.ServletResponse;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -35,7 +39,9 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
|
|
|
log.info("error Authorization is null");
|
|
|
|
|
|
// 先这样抛出异常,这个种不是接口的形式
|
|
|
- throw new AuthenticationException("Authorization is null ");
|
|
|
+ throw new AuthenticationException("token is null");
|
|
|
+// throw new JwtAuthenticationException(5001, "Authorization is null");
|
|
|
+// throw new BaseRuntimeException(5001, "token is null");
|
|
|
}
|
|
|
|
|
|
return true;
|
|
@@ -49,18 +55,13 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
|
|
|
*/
|
|
|
@Override
|
|
|
protected boolean executeLogin(ServletRequest request, ServletResponse response) {
|
|
|
-// LOGGER.warn("run executeLogin");
|
|
|
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
|
|
String authorization = httpServletRequest.getHeader("Authorization");
|
|
|
JWTToken token = new JWTToken(authorization);
|
|
|
|
|
|
- // 判断token 是否跟redis
|
|
|
-
|
|
|
// 提交给realm进行登入,如果错误他会抛出异常并被捕获
|
|
|
getSubject(request, response).login(token);
|
|
|
|
|
|
-
|
|
|
-
|
|
|
// 如果没有抛出异常则代表登入成功,返回true
|
|
|
return true;
|
|
|
}
|
|
@@ -81,16 +82,30 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
|
|
|
*/
|
|
|
@Override
|
|
|
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
|
|
|
-// LOGGER.warn("run isAccessAllowed");
|
|
|
if (isLoginAttempt(request, response)) {
|
|
|
-// try {
|
|
|
-// executeLogin(request, response);
|
|
|
-// } catch (Exception e) {
|
|
|
-//// response401(request, response);
|
|
|
-// throw new AuthenticationException("Authorization is null 123");
|
|
|
-// }
|
|
|
-
|
|
|
- executeLogin(request, response);
|
|
|
+ try {
|
|
|
+ executeLogin(request, response);
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ /** 这个异常需要自己写,全局捕获不了*/
|
|
|
+
|
|
|
+ // 认证出现异常,传递错误信息msg
|
|
|
+ String msg = e.getMessage();
|
|
|
+ // 获取应用异常(该Cause是导致抛出此throwable(异常)的throwable(异常))
|
|
|
+ Throwable throwable = e.getCause();
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ if (throwable instanceof JwtAuthenticationException) {
|
|
|
+ jsonObject.put("status", ((JwtAuthenticationException) throwable).getCode());
|
|
|
+ jsonObject.put("message", ((JwtAuthenticationException) throwable).getMsg());
|
|
|
+ }else{
|
|
|
+ log.error(msg);
|
|
|
+ jsonObject.put("status", 5002);
|
|
|
+ jsonObject.put("message", "token invalid");
|
|
|
+ }
|
|
|
+ // 直接返回Response信息
|
|
|
+ this.writeResponse(response, jsonObject);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
// return false 前端没有响应,接收不到异常
|
|
|
return true;
|
|
@@ -119,12 +134,34 @@ public class JWTFilter extends BasicHttpAuthenticationFilter {
|
|
|
/**
|
|
|
* 将非法请求跳转到 /401
|
|
|
*/
|
|
|
- private void response401(ServletRequest req, ServletResponse resp) {
|
|
|
+// private void response401(ServletRequest req, ServletResponse resp) {
|
|
|
+// try {
|
|
|
+// HttpServletResponse httpServletResponse = (HttpServletResponse) resp;
|
|
|
+// httpServletResponse.sendRedirect("/401");
|
|
|
+// } catch (IOException e) {
|
|
|
+// log.error(e.getMessage());
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无需转发,直接返回Response信息
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private void writeResponse(ServletResponse response, JSONObject msg) {
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setContentType("application/json; charset=utf-8");
|
|
|
+ PrintWriter out = null;
|
|
|
try {
|
|
|
- HttpServletResponse httpServletResponse = (HttpServletResponse) resp;
|
|
|
- httpServletResponse.sendRedirect("/401");
|
|
|
+ out = response.getWriter();
|
|
|
+ out.append(msg.toString());
|
|
|
+ out.flush();
|
|
|
+ out.close();
|
|
|
} catch (IOException e) {
|
|
|
- log.error(e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (out != null){
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|