|  |  |  | 
|---|
|  |  |  | package com.genersoft.iot.vmp.conf.security; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.UserSetting; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.conf.security.dto.JwtUser; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.dao.dto.Role; | 
|---|
|  |  |  | import com.genersoft.iot.vmp.storager.dao.dto.User; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | 
|---|
|  |  |  | import org.springframework.security.core.context.SecurityContextHolder; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | 
|---|
|  |  |  | public class JwtAuthenticationFilter extends OncePerRequestFilter { | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private UserSetting userSetting; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 忽略登录请求的token验证 | 
|---|
|  |  |  | String requestURI = request.getRequestURI(); | 
|---|
|  |  |  | if (requestURI.equalsIgnoreCase("/api/user/login")) { | 
|---|
|  |  |  | chain.doFilter(request, response); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (!userSetting.isInterfaceAuthentication()) { | 
|---|
|  |  |  | UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(null, null, new ArrayList<>() ); | 
|---|
|  |  |  | SecurityContextHolder.getContext().setAuthentication(token); | 
|---|
|  |  |  | chain.doFilter(request, response); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        String password = SecurityUtils.encryptPassword(jwtUser.getPassword()); | 
|---|
|  |  |  | //        user.setPassword(password); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 构建UsernamePasswordAuthenticationToken,这里密码为null,是因为提供了正确的JWT,实现自动登录 | 
|---|
|  |  |  | UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, jwtUser.getPassword(), new ArrayList<>() ); | 
|---|
|  |  |  | User user = new User(); | 
|---|
|  |  |  | user.setUsername(jwtUser.getUserName()); | 
|---|
|  |  |  | user.setPassword(jwtUser.getPassword()); | 
|---|
|  |  |  | Role role = new Role(); | 
|---|
|  |  |  | role.setId(jwtUser.getRoleId()); | 
|---|
|  |  |  | user.setRole(role); | 
|---|
|  |  |  | UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, jwtUser.getPassword(), new ArrayList<>() ); | 
|---|
|  |  |  | SecurityContextHolder.getContext().setAuthentication(token); | 
|---|
|  |  |  | chain.doFilter(request, response); | 
|---|
|  |  |  | } | 
|---|