当前位置:网站首页>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 .
边栏推荐
- Determine whether there is an element in the string type
- Php/js cookie sharing across domains
- 中小型项目手撸过滤器实现认证与授权
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- Collectors.groupingBy 排序
- MySQL foundation --- query (learn MySQL foundation in 1 day)
- LeetCode 1175. 质数排列(质数判断+组合数学)
- Feign realizes file uploading and downloading
- [technical notes-08]
- Operator details
猜你喜欢

【pyinstaller】_ get_ sysconfigdata_ name() missing 1 required positional argument: ‘check_ exists‘

No logic is executed after the El form is validated successfully

函数栈帧的创建和销毁

Fabric.js IText 上标和下标

Using QA band and bit mask in Google Earth engine

7. Eleven state sets of TCP

Paddlepaddle project source code

Gee series: Unit 2 explore datasets

kmp思想及模板代码

Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
随机推荐
中小型项目手撸过滤器实现认证与授权
Implementation of go language for deleting duplicate items in sorting array
The reason why sizeof (ARR) / sizeof (arr[0]) is used in the function to calculate the length of the array is incorrect
Disable access to external entities in XML parsing
Online music player app
Fabric.js 背景不受视口变换影响
Fabric. JS centered element
Splice characters in {{}}
Storage of data
How matlab marks' a 'in the figure and how matlab marks points and solid points in the figure
Global and Chinese market of pressure gauges 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 1175. 质数排列(质数判断+组合数学)
Fabric.js 将本地图像上传到画布背景
Global and Chinese market of hydrocyclone desander 2022-2028: Research Report on technology, participants, trends, market size and share
With an amount of $50billion, amd completed the acquisition of Xilinx
Fabric. JS basic brush
RNN recurrent neural network
Gee: remote sensing image composite and mosaic
Collectors.groupingBy 排序
Thread pool batch processing data