提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.framework.security.filter; |
懒 |
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import javax.servlet.FilterChain; |
|
5 |
import javax.servlet.ServletException; |
|
6 |
import javax.servlet.http.HttpServletRequest; |
|
7 |
import javax.servlet.http.HttpServletResponse; |
|
8 |
import org.springframework.beans.factory.annotation.Autowired; |
|
9 |
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
|
10 |
import org.springframework.security.core.context.SecurityContextHolder; |
|
11 |
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
|
12 |
import org.springframework.stereotype.Component; |
|
13 |
import org.springframework.web.filter.OncePerRequestFilter; |
|
14 |
import com.jcdm.common.core.domain.model.LoginUser; |
|
15 |
import com.jcdm.common.utils.SecurityUtils; |
|
16 |
import com.jcdm.common.utils.StringUtils; |
|
17 |
import com.jcdm.framework.web.service.TokenService; |
|
18 |
|
|
19 |
/** |
|
20 |
* token过滤器 验证token有效性 |
|
21 |
* |
|
22 |
* @author jc |
|
23 |
*/ |
|
24 |
@Component |
|
25 |
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter |
|
26 |
{ |
|
27 |
@Autowired |
|
28 |
private TokenService tokenService; |
|
29 |
|
|
30 |
@Override |
|
31 |
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) |
|
32 |
throws ServletException, IOException |
|
33 |
{ |
|
34 |
LoginUser loginUser = tokenService.getLoginUser(request); |
|
35 |
if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication())) |
|
36 |
{ |
|
37 |
tokenService.verifyToken(loginUser); |
|
38 |
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities()); |
|
39 |
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); |
|
40 |
SecurityContextHolder.getContext().setAuthentication(authenticationToken); |
|
41 |
} |
|
42 |
chain.doFilter(request, response); |
|
43 |
} |
|
44 |
} |