Added Authentication Component
[eliot.git] / blueprints / common / eliot-ui / be / src / eliotk8sclient / src / main / java / com / eliot / eliotbe / eliotk8sclient / config / JwtRequestFilter.java
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.eliot.eliotbe.eliotk8sclient.config;
17
18 import java.io.IOException;
19
20 import javax.servlet.FilterChain;
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
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;
32
33 import com.eliot.eliotbe.eliotk8sclient.service.JwtUserDetailsService;
34 import com.eliot.eliotbe.eliotk8sclient.util.*;
35 import io.jsonwebtoken.ExpiredJwtException;
36
37 @Component
38 public class JwtRequestFilter extends OncePerRequestFilter {
39
40     @Autowired
41     private JwtUserDetailsService jwtUserDetailsService;
42
43     @Autowired
44     private JwtTokenUtil jwtTokenUtil;
45
46     @Override
47     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
48             throws ServletException, IOException {
49
50         final String requestTokenHeader = request.getHeader("Authorization");
51
52         String username = null;
53         String jwtToken = null;
54         // JWT Token is in the form "Bearer token". Remove Bearer word and get
55         // only the Token
56         if (requestTokenHeader != null && requestTokenHeader.startsWith("Bearer ")) {
57             jwtToken = requestTokenHeader.substring(7);
58             try {
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");
64             }
65         } else {
66             logger.warn("JWT Token does not begin with Bearer String");
67         }
68
69         // Once we get the token validate it.
70         if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
71
72             UserDetails userDetails = this.jwtUserDetailsService.loadUserByUsername(username);
73
74             // if token is valid configure Spring Security to manually set
75             // authentication
76             if (jwtTokenUtil.validateToken(jwtToken, userDetails)) {
77
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);
86             }
87         }
88         chain.doFilter(request, response);
89     }
90
91 }