当前位置:网站首页>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 .
边栏推荐
- KMP idea and template code
- Fabric. JS iText superscript and subscript
- Principle and implementation of parallax effect
- Fabric. JS centered element
- Fabric.js 将本地图像上传到画布背景
- Fabric. JS basic brush
- Nodejs (03) -- custom module
- How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
- 【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
- [technical notes-08]
猜你喜欢
Gee series: unit 8 time series analysis in Google Earth engine [time series]
Fabric. JS activation input box
Fabric.js 渐变
Disable access to external entities in XML parsing
Ls1046nfs mount file system
Fabric. JS right click menu
摆正元素(带过渡动画)
Principle and implementation of parallax effect
【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘
指针使用详解
随机推荐
Detailed explanation of Pointer use
Black Horse Notes - - set Series Collection
Nodejs (02) - built in module
Implementation of leetcode two number addition go
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
中小型项目手撸过滤器实现认证与授权
[technical notes-08]
小程序跳装到公众号
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share
Gee series: Unit 4 data import and export in Google Earth engine
黑马笔记---Set系列集合
Fabric.js 居中元素
记录sentry的踩坑之路
Gee series: unit 6 building various remote sensing indexes in Google Earth engine
Gee series: unit 10 creating a graphical user interface using Google Earth engine [GUI development]
7.1 Résumé du concours de simulation
操作符详解
Find the subscript with and as the target from the array