|
@@ -1,164 +1,164 @@
|
|
|
-//package com.imed.costaccount.common.shiro;
|
|
|
-//
|
|
|
-//import cn.hutool.core.util.StrUtil;
|
|
|
-//import cn.hutool.json.JSONUtil;
|
|
|
-//import com.imed.costaccount.common.util.Result;
|
|
|
-//import com.imed.costaccount.common.config.RedisUtil;
|
|
|
-//import com.imed.costaccount.common.token.JwtUtil;
|
|
|
-//import com.imed.costaccount.common.token.ThreadLocalToken;
|
|
|
-//import lombok.extern.slf4j.Slf4j;
|
|
|
-//import org.apache.shiro.authc.AuthenticationException;
|
|
|
-//import org.apache.shiro.authc.AuthenticationToken;
|
|
|
-//import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
|
|
|
-//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-//import org.springframework.beans.factory.annotation.Value;
|
|
|
-//import org.springframework.context.annotation.Scope;
|
|
|
-//import org.springframework.stereotype.Component;
|
|
|
-//import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
-//
|
|
|
-//import javax.servlet.FilterChain;
|
|
|
-//import javax.servlet.ServletException;
|
|
|
-//import javax.servlet.ServletRequest;
|
|
|
-//import javax.servlet.ServletResponse;
|
|
|
-//import javax.servlet.http.HttpServletRequest;
|
|
|
-//import javax.servlet.http.HttpServletResponse;
|
|
|
-//import java.io.IOException;
|
|
|
-//
|
|
|
-//@Slf4j
|
|
|
-//@Scope("prototype")
|
|
|
-//@Component
|
|
|
-//public class OAuth2Filter extends AuthenticatingFilter {
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// private JwtUtil jwtUtil;
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// private ThreadLocalToken local;
|
|
|
-//
|
|
|
-// @Autowired
|
|
|
-// private RedisUtil redisUtil;
|
|
|
-//
|
|
|
-//
|
|
|
-// @Value("${pfm.jwt.expire}")
|
|
|
-// private int expire;
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 判断是否需要交由shiro处理,一般options 请求类型不需要
|
|
|
-// * @return <code>true</code> if request should be allowed access
|
|
|
-// * @param request
|
|
|
-// * @param response
|
|
|
-// * @param mappedValue
|
|
|
-// */
|
|
|
-// @Override
|
|
|
-// protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
|
|
|
-// HttpServletRequest req = (HttpServletRequest) request;
|
|
|
-// if (req.getMethod().equals(RequestMethod.OPTIONS.name())) {
|
|
|
-// return true;
|
|
|
-// }
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 将token封装为auth2Token 返回交由shiro处理
|
|
|
-// * @param servletRequest
|
|
|
-// * @param servletResponse
|
|
|
-// * @return
|
|
|
-// * @throws Exception
|
|
|
-// */
|
|
|
-// @Override
|
|
|
-// protected AuthenticationToken createToken(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
|
|
|
-// HttpServletRequest request = (HttpServletRequest) servletRequest;
|
|
|
-// String token = this.getRequestToken(request);
|
|
|
-// if (StrUtil.isBlank(token)) {
|
|
|
-// return null;
|
|
|
-// }
|
|
|
-// return new OAuth2Token(token);
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 验证token,并进行相应处理(是否过期,续期等)
|
|
|
-// * @param request
|
|
|
-// * @param response
|
|
|
-// * @return
|
|
|
-// * @throws Exception
|
|
|
-// */
|
|
|
-// @Override
|
|
|
-// protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
|
|
|
-// HttpServletRequest req = (HttpServletRequest) request;
|
|
|
-// HttpServletResponse resp = (HttpServletResponse) response;
|
|
|
-// resp.setContentType("text/html");
|
|
|
-// resp.setCharacterEncoding("UTF-8");
|
|
|
-// // 允许跨域
|
|
|
-// resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
-// resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
|
|
|
-//
|
|
|
-// local.clear();
|
|
|
-// String token = this.getRequestToken(req);
|
|
|
-// if (StrUtil.isBlank(token)) {
|
|
|
-// String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
|
|
|
-// resp.getWriter().print(json);
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-// // 判断redis中是否存在该用户的token,如果不存在或者不一致那么标识token无效
|
|
|
-// int userId = jwtUtil.getUserId(token);
|
|
|
-// String str = (String) redisUtil.get(userId + "");
|
|
|
-// if (StrUtil.isBlank(str)) {
|
|
|
-// String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
|
|
|
-// resp.getWriter().print(json);
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-// if (!str.equalsIgnoreCase(token)) {
|
|
|
-// String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
|
|
|
-// resp.getWriter().print(json);
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-//
|
|
|
-// // 内容是否过期
|
|
|
-// try {
|
|
|
-// jwtUtil.verifierToken(token);
|
|
|
-// } catch (Exception e) {
|
|
|
-//// // 无效的令牌
|
|
|
-// resp.setStatus(400);
|
|
|
-// resp.getWriter().print("无效的令牌");
|
|
|
-// String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌"));
|
|
|
-// resp.getWriter().print(json);
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-// // 执行realm
|
|
|
-// local.setToken(token);
|
|
|
-// return executeLogin(request, response);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
|
|
|
-// HttpServletRequest req = (HttpServletRequest) request;
|
|
|
-// HttpServletResponse resp = (HttpServletResponse) response;
|
|
|
-// resp.setContentType("text/html");
|
|
|
-// resp.setCharacterEncoding("UTF-8");
|
|
|
-// // 允许跨域
|
|
|
-// resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
-// resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
|
|
|
-// resp.setStatus(400);
|
|
|
-// try {
|
|
|
-// resp.getWriter().print(e.getMessage());
|
|
|
-// } catch (IOException ioException) {
|
|
|
-// ioException.printStackTrace();
|
|
|
-// }
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// private String getRequestToken(HttpServletRequest request) {
|
|
|
-// String token = request.getHeader("token");
|
|
|
-//
|
|
|
-// if (StrUtil.isBlank(token)) {
|
|
|
-// token = request.getParameter("token");
|
|
|
-// }
|
|
|
-// return token;
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
|
|
|
-// super.doFilterInternal(request, response, chain);
|
|
|
-// }
|
|
|
-//}
|
|
|
+package com.imed.costaccount.common.shiro;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.imed.costaccount.common.token.RedisUtil;
|
|
|
+import com.imed.costaccount.common.util.Result;
|
|
|
+import com.imed.costaccount.common.token.JwtUtil;
|
|
|
+import com.imed.costaccount.common.token.ThreadLocalToken;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.shiro.authc.AuthenticationException;
|
|
|
+import org.apache.shiro.authc.AuthenticationToken;
|
|
|
+import org.apache.shiro.web.filter.authc.AuthenticatingFilter;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Scope;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
+
|
|
|
+import javax.servlet.FilterChain;
|
|
|
+import javax.servlet.ServletException;
|
|
|
+import javax.servlet.ServletRequest;
|
|
|
+import javax.servlet.ServletResponse;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Scope("prototype")
|
|
|
+@Component
|
|
|
+public class OAuth2Filter extends AuthenticatingFilter {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private JwtUtil jwtUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ThreadLocalToken local;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+
|
|
|
+
|
|
|
+ @Value("${pfm.jwt.expire}")
|
|
|
+ private int expire;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否需要交由shiro处理,一般options 请求类型不需要
|
|
|
+ * @return <code>true</code> if request should be allowed access
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @param mappedValue
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
|
|
|
+ HttpServletRequest req = (HttpServletRequest) request;
|
|
|
+ if (req.getMethod().equals(RequestMethod.OPTIONS.name())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将token封装为auth2Token 返回交由shiro处理
|
|
|
+ * @param servletRequest
|
|
|
+ * @param servletResponse
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected AuthenticationToken createToken(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
|
|
|
+ HttpServletRequest request = (HttpServletRequest) servletRequest;
|
|
|
+ String token = this.getRequestToken(request);
|
|
|
+ if (StrUtil.isBlank(token)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return new OAuth2Token(token);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证token,并进行相应处理(是否过期,续期等)
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
|
|
|
+ HttpServletRequest req = (HttpServletRequest) request;
|
|
|
+ HttpServletResponse resp = (HttpServletResponse) response;
|
|
|
+ resp.setContentType("text/html");
|
|
|
+ resp.setCharacterEncoding("UTF-8");
|
|
|
+ // 允许跨域
|
|
|
+ resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
+ resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
|
|
|
+
|
|
|
+ local.clear();
|
|
|
+ String token = this.getRequestToken(req);
|
|
|
+ if (StrUtil.isBlank(token)) {
|
|
|
+ String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
|
|
|
+ resp.getWriter().print(json);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 判断redis中是否存在该用户的token,如果不存在或者不一致那么标识token无效
|
|
|
+ int userId = jwtUtil.getUserId(token);
|
|
|
+ String str = (String) redisUtil.get(userId + "");
|
|
|
+ if (StrUtil.isBlank(str)) {
|
|
|
+ String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
|
|
|
+ resp.getWriter().print(json);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (!str.equalsIgnoreCase(token)) {
|
|
|
+ String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌,请登录"));
|
|
|
+ resp.getWriter().print(json);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 内容是否过期
|
|
|
+ try {
|
|
|
+ jwtUtil.verifierToken(token);
|
|
|
+ } catch (Exception e) {
|
|
|
+// // 无效的令牌
|
|
|
+ resp.setStatus(400);
|
|
|
+ resp.getWriter().print("无效的令牌");
|
|
|
+ String json = JSONUtil.toJsonStr(Result.errorMsg(499, "无效的令牌"));
|
|
|
+ resp.getWriter().print(json);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 执行realm
|
|
|
+ local.setToken(token);
|
|
|
+ return executeLogin(request, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected boolean onLoginFailure(AuthenticationToken token, AuthenticationException e, ServletRequest request, ServletResponse response) {
|
|
|
+ HttpServletRequest req = (HttpServletRequest) request;
|
|
|
+ HttpServletResponse resp = (HttpServletResponse) response;
|
|
|
+ resp.setContentType("text/html");
|
|
|
+ resp.setCharacterEncoding("UTF-8");
|
|
|
+ // 允许跨域
|
|
|
+ resp.setHeader("Access-Control-Allow-Credentials", "true");
|
|
|
+ resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
|
|
|
+ resp.setStatus(400);
|
|
|
+ try {
|
|
|
+ resp.getWriter().print(e.getMessage());
|
|
|
+ } catch (IOException ioException) {
|
|
|
+ ioException.printStackTrace();
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String getRequestToken(HttpServletRequest request) {
|
|
|
+ String token = request.getHeader("token");
|
|
|
+
|
|
|
+ if (StrUtil.isBlank(token)) {
|
|
|
+ token = request.getParameter("token");
|
|
|
+ }
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void doFilterInternal(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
|
|
|
+ super.doFilterInternal(request, response, chain);
|
|
|
+ }
|
|
|
+}
|