当前位置:网站首页>Small and medium-sized projects to achieve certification and authorization of hand filter
Small and medium-sized projects to achieve certification and authorization of hand filter
2022-07-02 05:27:00 【It snows at night when the boat is moored, and he can expect it】
Small and medium-sized projects to achieve certification and authorization of hand filter
Code implementation
@Component
@WebFilter(urlPatterns = "/*", filterName = "tokenFilter")
public class TokenFilter implements Filter {
// token Expiration time
@Value("${token-expired-time}")
private int tokenExpiredTime;
// uri Request white list
@Resource
private WhiteListConfig whiteListConfig;
@Resource
private RedisTemplate<String, Object> redisTemplate;
// Do exception handling
@Resource
@Qualifier("handlerExceptionResolver")
private HandlerExceptionResolver resolver;
@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
// Ask for token value , You need to pay attention to your project token Corresponding key What is it?
// I am here public static final String AUTH_TOKEN = "auth-token";
String authToken = request.getHeader(Constants.AUTH_TOKEN);
// Request address
String requestURI = request.getRequestURI();
// Verify whether the white list contains the current uri
boolean flag = whiteListConfig.getNotCheck().stream().anyMatch(u -> requestURI.equals("/"+u));
// Pass verification
if (flag) {
// It's certified , Release
filterChain.doFilter(request, response);
} else if (StringUtils.isNotBlank(authToken) && redisTemplate.hasKey(authToken)) {
// Non white list , need token authentication
// To authorize
// redis It stores the permission information set of the user , Such as : role 、 menu 、 Function and other permission information
TokenInfo tokenInfo = (TokenInfo) redisTemplate.opsForValue().get(authToken);
assert tokenInfo != null;
boolean empower = tokenInfo.getEmpowers() != null && tokenInfo.getEmpowers().size() > 0 && tokenInfo.getEmpowers().stream().anyMatch((requestURI)::equals);
// Certification and authorization will pass
if (empower){
// Refresh token The expiration time of
redisTemplate.expire(token,tokenOvertime,TimeUnit.SECONDS);
filterChain.doFilter(request, response);
}else {
// If it is certified but there is no authorized information
resolver.resolveException(request, response, null,new Err(Status.ILLEGAL_REQUEST.getStatus(),Status.ILLEGAL_REQUEST.getError()));
}
} else {
// There is no match for redis Inside key,key Namely authToken; In these request businesses, the request is directly rejected and an exception is thrown , Record relevant information
resolver.resolveException(request, response, null, StringUtils.isBlank(authToken) ? new Err(" In the request header auth-token Information cannot be empty !") : new Err(Status.NOT_LOGIN.getStatus(), Status.NOT_LOGIN.getError()));
}
}
}
summary
The general idea is as follows , The details can be changed according to the project information .
边栏推荐
- Fabric. JS free draw rectangle
- 4. Flask cooperates with a tag to link internal routes
- Innovation never stops -- the innovation process of nvisual network visualization platform for Excel import
- Online English teaching app open source platform (customized)
- Online music player app
- H5 jump applet
- 记录sentry的踩坑之路
- LeetCode 241. 为运算表达式设计优先级(分治/记忆化递归/动态规划)
- With an amount of $50billion, amd completed the acquisition of Xilinx
- LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
猜你喜欢

Fabric.js 将本地图像上传到画布背景

Gee series: unit 6 building various remote sensing indexes in Google Earth engine

Black Horse Notes - - set Series Collection

Detailed explanation of Pointer use

Fabric. JS iText sets the color and background color of the specified text

No logic is executed after the El form is validated successfully

视差特效的原理和实现方法

Gee series: Unit 4 data import and export in Google Earth engine

How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure

centos8安装mysql8.0.22教程
随机推荐
黑马笔记---Set系列集合
Fabric. JS iText set italics manually
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
Gee series: Unit 4 data import and export in Google Earth engine
Collectors.groupingBy 排序
Online music player app
Fabric.js 基础笔刷
ubuntu20.04安装mysql8
Straighten elements (with transition animation)
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
Online English teaching app open source platform (customized)
Fabric.js IText 手动设置斜体
视差特效的原理和实现方法
中小型项目手撸过滤器实现认证与授权
Centos8 installation mysql8.0.22 tutorial
XSS basic content learning (continuous update)
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
在线音乐播放器app