2 * Copyright 2020 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.eliot.eliotbe.eliotk8sclient.config;
18 import java.io.IOException;
20 import javax.servlet.FilterChain;
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
27 import org.springframework.security.core.context.SecurityContextHolder;
28 import org.springframework.security.core.userdetails.UserDetails;
29 import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
30 import org.springframework.stereotype.Component;
31 import org.springframework.web.filter.OncePerRequestFilter;
33 import com.eliot.eliotbe.eliotk8sclient.service.JwtUserDetailsService;
34 import com.eliot.eliotbe.eliotk8sclient.util.*;
35 import io.jsonwebtoken.ExpiredJwtException;
38 public class JwtRequestFilter extends OncePerRequestFilter {
41 private JwtUserDetailsService jwtUserDetailsService;
44 private JwtTokenUtil jwtTokenUtil;
47 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
48 throws ServletException, IOException {
50 final String requestTokenHeader = request.getHeader("Authorization");
52 String username = null;
53 String jwtToken = null;
54 // JWT Token is in the form "Bearer token". Remove Bearer word and get
56 if (requestTokenHeader != null && requestTokenHeader.startsWith("Bearer ")) {
57 jwtToken = requestTokenHeader.substring(7);
59 username = jwtTokenUtil.getUsernameFromToken(jwtToken);
60 } catch (IllegalArgumentException e) {
61 System.out.println("Unable to get JWT Token");
62 } catch (ExpiredJwtException e) {
63 System.out.println("JWT Token has expired");
66 logger.warn("JWT Token does not begin with Bearer String");
69 // Once we get the token validate it.
70 if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
72 UserDetails userDetails = this.jwtUserDetailsService.loadUserByUsername(username);
74 // if token is valid configure Spring Security to manually set
76 if (jwtTokenUtil.validateToken(jwtToken, userDetails)) {
78 UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
79 userDetails, null, userDetails.getAuthorities());
80 usernamePasswordAuthenticationToken
81 .setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
82 // After setting the Authentication in the context, we specify
83 // that the current user is authenticated. So it passes the
84 // Spring Security Configurations successfully.
85 SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
88 chain.doFilter(request, response);