Quantcast
Channel: Active questions tagged servlets - Stack Overflow
Viewing all articles
Browse latest Browse all 675

how to customize HttpServletResponse in springboot for jwt filter response

$
0
0

i have react js and springboot application and i am using jwt token for authorization for that on java restarts i am validating one field called tokenVersion and incase of invalid tokenVersion i am trying to add a field in the http servlet response like invalidTokenVersion:true or false.

but i am not getting the field in the react axios response

please help me how to customize the jwt response

JwtFilter.java

package com.demo.services.Authentication;import org.json.JSONObject;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;import org.springframework.security.core.context.SecurityContextHolder;import org.springframework.security.core.userdetails.UserDetails;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;@Componentpublic class JwtFilter extends OncePerRequestFilter {    @Autowired    private JwtTokenUtil jwtUtil;    @Autowired    private AuthenticationService service;    @Override    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {        String authorizationHeader = httpServletRequest.getHeader("Authorization");        String token = null;        String userName = null;        if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {            token = authorizationHeader.substring(7);            userName = jwtUtil.extractUsername(token);        }        if (userName != null && SecurityContextHolder.getContext().getAuthentication() == null) {            UserDetails userDetails = service.loadUserByUsername(userName);            if (jwtUtil.validateToken(token, userDetails)) {                UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken =                        new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());                usernamePasswordAuthenticationToken                        .setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));                SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);            }else if(jwtUtil.isTokenExpired()){                JSONObject json = new JSONObject();                System.out.println("======= expied tokennnnnn");                json.put("invalidTokenVersion", true);                httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);                httpServletResponse.setContentType("application/json");                httpServletResponse.getWriter().write(json.toString());            }        }        filterChain.doFilter(httpServletRequest, httpServletResponse);    }}

react axios code:

const getAxiosHeader = () => {const API_BASE_URL = 'http://'+endPointURLIP+':9081';const AUTHENTICATION_HEADER_TOKEN = authToken ? Bearer ${authToken} : null;

return axios.create({  baseURL: API_BASE_URL,  headers: {    Authorization: AUTHENTICATION_HEADER_TOKEN,  },});

};export const authAxiosHeader = getAxiosHeader();

authAxiosHeader.interceptors.response.use(    (response) => response,    (error) => {    alert(JSON.stringify(error))      if (error.response && error.response.status === 403) {//perform some action        }        return Promise.reject(error);      }    );

in the alert print there is no invalidTokenVersion field

please help me with this

thanks


Viewing all articles
Browse latest Browse all 675

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>