0

i am trying to send session id in response header in rest controller but excludepathpattern() seems not working

** the configuration class is not triggering **

i have tried changing the sevlet version but it didnt work

ContextListener

@Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        Map<String, HttpSession> map = new HashMap<>();
        context.setAttribute("activeUsers", map);

HttpSessionListener

ServletContext context = session.getServletContext();
        Map<String, HttpSession> activeUsers = (Map<String, HttpSession>) context.getAttribute("activeUsers");

        activeUsers.put(session.getId(), session);

HandlerInterceptor

ServletContext context = request.getServletContext();
        Map<String, HttpSession> activeUsers = (Map<String, HttpSession>) context.getAttribute("activeUsers");
        String sessionId = request.getHeader("sessionId");
        String requestUrl = request.getRequestURL().toString();
        if (requestUrl.contains("/getOtp") || requestUrl.contains("/validateOtp")) {
            return true;
        } else {
            if (activeUsers.containsKey(sessionId)) {
                return true;
            } else {
                response.setStatus(401);
                return false;
            }
        }

interceptorconfigurartion by extendig websecurityconfigure

@Configuration
@EnableAutoConfiguration
public class SessionInterceptorConfig implements WebMvcConfigurer  {
@Autowired
    private SessionHanlderInterceptor sessionHandlerIntercepto;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//      List<String> paths = new ArrayList<String>();
//      paths.add("/auth/*");
        registry.addInterceptor(sessionHandlerIntercepto).excludePathPatterns("/auth/**");
    }

    @Bean
    public ServletListenerRegistrationBean<CustomSessionListener> filterRegistrationBean() {
        ServletListenerRegistrationBean<CustomSessionListener> registrationBean = new ServletListenerRegistrationBean<CustomSessionListener>();
        CustomSessionListener customURLFilter = new CustomSessionListener();

        registrationBean.setListener(customURLFilter);
        registrationBean.setOrder(1); // set precedence
        return registrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean<CustomServletContextListener> filterContextRregistration() {
        ServletListenerRegistrationBean<CustomServletContextListener> registrationBean = new ServletListenerRegistrationBean<CustomServletContextListener>();
        CustomServletContextListener customURLFilter = new CustomServletContextListener();
        registrationBean.setListener(customURLFilter);
        registrationBean.setOrder(1); // set precedence
        return registrationBean;
    }

Sprinboot main class

@SpringBootApplication
public class CustomerApplication extends SpringBootServletInitializer  {

i expect to add the session id to header in response and to check for the sessionid in request

4
  • why dont you just use a Filter, its a lot easier baeldung.com/spring-boot-add-filter Commented Aug 7, 2019 at 21:50
  • It is. I use a filter as well to enrich the response with custom header. How does your implementation looks like Commented Aug 8, 2019 at 10:23
  • please just dont write "that is also not working". If you want help you please explain why it isnt working so that we can help. Do you think anyone can help you if you just say "it isnt working"? Commented Aug 8, 2019 at 16:19
  • ohhh thanks for the response the issue with registering the filter , i tried to register it and tried to debug the same but with setting the breakpoints in configuration class but the debugger never took the controller to that configuration class Commented Aug 8, 2019 at 17:34

1 Answer 1

2

You can use spring web component "OncePerRequestFilter". You need to inject a bean which extends OncePerRequestFilter. Example:

public class CustomHeaderFilter extends OncePerRequestFilter {

    @Override
    public void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response,
                                 final FilterChain chain) throws IOException, ServletException {
           response.setHeader(customHeaderName, customHeaderValue);
        chain.doFilter(request, response);
    }
 }

Sign up to request clarification or add additional context in comments.

2 Comments

doest it require any special configuration to be considered while registering with servletfilterregistration
It doesn't require any special configuration for registering with the servlet. The documentation says: Filter base class that aims to guarantee a single execution per request dispatch, on any servlet container. It provides a {@link #doFilterInternal} *method with HttpServletRequest and HttpServletResponse arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.