tlias管理系统-过滤器Filter实现登录校验功能 ----2
This commit is contained in:
@@ -75,6 +75,13 @@
|
|||||||
<artifactId>jjwt</artifactId>
|
<artifactId>jjwt</artifactId>
|
||||||
<version>0.9.1</version>
|
<version>0.9.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--阿里巴巴-json操作依赖-fastJson-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>2.0.53</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.inmind.filter;
|
package com.inmind.filter;
|
||||||
|
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.inmind.pojo.Result;
|
||||||
import com.inmind.utils.JwtUtils;
|
import com.inmind.utils.JwtUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
@@ -33,7 +35,8 @@ public class LoginCheckFilter implements Filter {
|
|||||||
|
|
||||||
//4.判断令牌是否存在,不存在,直接返回错误结果(未登录)
|
//4.判断令牌是否存在,不存在,直接返回错误结果(未登录)
|
||||||
if (!StringUtils.hasLength(jwt)) {
|
if (!StringUtils.hasLength(jwt)) {
|
||||||
//todo 没有令牌,返回错误结果
|
//没有令牌,返回错误结果
|
||||||
|
notLogin(response);
|
||||||
log.info("没有令牌,返回错误结果");
|
log.info("没有令牌,返回错误结果");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -42,7 +45,8 @@ public class LoginCheckFilter implements Filter {
|
|||||||
try {
|
try {
|
||||||
JwtUtils.parseJWT(jwt);
|
JwtUtils.parseJWT(jwt);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//todo 令牌解析失败,返回错误结果
|
//令牌解析失败,返回错误结果
|
||||||
|
notLogin(response);
|
||||||
log.info("令牌解析失败,返回错误结果");
|
log.info("令牌解析失败,返回错误结果");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -52,4 +56,14 @@ public class LoginCheckFilter implements Filter {
|
|||||||
filterChain.doFilter(servletRequest,servletResponse);
|
filterChain.doFilter(servletRequest,servletResponse);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void notLogin(HttpServletResponse response) throws IOException {
|
||||||
|
//根据接口文档响应json,但是之前通过spring的控制器里的@ResponseBody注解,自动响应json,现在没有该注解,我们要手动发送json
|
||||||
|
Result error = Result.error("NOT_LOGIN");
|
||||||
|
//手动转换--将java对象转换为json字符串,阿里巴巴的fastJson
|
||||||
|
String errorJson = JSONObject.toJSONString(error);
|
||||||
|
//通过响应对象,将json响应给前端
|
||||||
|
response.getWriter().write(errorJson);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user