From 1ba164159bd988c7b8f0feac65d13225fa4abbf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E7=9A=93=E9=9B=AF?= <18868469283@qq.com> Date: Wed, 9 Jan 2019 14:32:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JwtAuthenticationTokenFilter.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 代码库/mall-admin/src/main/java/com/sock/mall/component/JwtAuthenticationTokenFilter.java diff --git a/代码库/mall-admin/src/main/java/com/sock/mall/component/JwtAuthenticationTokenFilter.java b/代码库/mall-admin/src/main/java/com/sock/mall/component/JwtAuthenticationTokenFilter.java new file mode 100644 index 0000000..b7d8ed5 --- /dev/null +++ b/代码库/mall-admin/src/main/java/com/sock/mall/component/JwtAuthenticationTokenFilter.java @@ -0,0 +1,61 @@ +package com.macro.mall.component; + +import com.macro.mall.util.JwtTokenUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * JWT¼Ȩ + * Created by macro on 2018/4/26. + */ +public class JwtAuthenticationTokenFilter extends OncePerRequestFilter { + private static final Logger LOGGER = LoggerFactory.getLogger(JwtAuthenticationTokenFilter.class); + /*Log4jUtils࣬LoggerIJ*/ + @Autowired + private UserDetailsService userDetailsService;//ûϸϢ + @Autowired + private JwtTokenUtil jwtTokenUtil;//jwt¼Ȩ + @Value("${jwt.tokenHeader}") + private String tokenHeader;//¼ͷ + @Value("${jwt.tokenHead}") + private String tokenHead; + + @Override + protected void doFilterInternal(HttpServletRequest request, + HttpServletResponse response, + FilterChain chain) throws ServletException, IOException { + /*Filterʵֹ˹*/ + String authHeader = request.getHeader(this.tokenHeader); + if (authHeader != null && authHeader.startsWith(this.tokenHead)) { + String authToken = authHeader.substring(this.tokenHead.length());// The part after "Bearer " + String username = jwtTokenUtil.getUserNameFromToken(authToken); + LOGGER.info("checking username:{}", username); + if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) { + UserDetails userDetails = this.userDetailsService.loadUserByUsername(username); + if (jwtTokenUtil.validateToken(authToken, userDetails)) { + /*֤ϢSecurityContextHolderУUsernamePasswordAuthenticationTokenSecurity֤˺Ĺ*/ + UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); + authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request)); + LOGGER.info("authenticated user:{}", username); + SecurityContextHolder.getContext().setAuthentication(authentication); + } + } + } + chain.doFilter(request, response);//תһ + } +} \ No newline at end of file