title: Spring Security - Need to add custom cookies
body: <p>I am new to Servlets. I need to generate the custom value cookie. In mention below code the session cookie is generated but I need one more with custom value.</p>
  
  <pre><code>public ServletContextInitializer servletContextInitializer() {
      servletContext -&gt; servletContext.getSessionCookieConfig().setName("sessiondemo");
  }
  </code></pre>
  
  <p>My Servlet code is as below. In the comment line I need to add cookies. Basically I am redirect the request which comes here to init the servlet session in mean time I also need to generate a cookie with name hello and value world in servletcontext.</p>
  
  <pre><code>public class WebSecurityConfigurer extends WebMvcConfigurerAdapter {
  
      @Autowired
      private Environment environment;
      UserDetails user;
  
      @Override
      public void addViewControllers(ViewControllerRegistry registry) {
          registry.addViewController("/login").setViewName("login");
      }
  
      @Bean
      public ServletContextInitializer servletContextInitializer() {
          /* This code doesn't work here. I need to use here to set up the cookie
          Cookie cookie = new Cookie("YourCookieName", "CookieStringValue");
          cookie.setMaxAge(10 * 365 * 24 * 60 * 60); // set cookie for 10 years
          response.addCookie(cookie); */
          return servletContext -&gt; servletContext.getSessionCookieConfig()
                  .setName("oneKosmosIdpSessionId");
      }
  
      @Configuration
      @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
      protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
  
          @Autowired
          private IdpConfiguration idpConfiguration;
  
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                      .csrf().disable()
                      .authorizeRequests()
                      .antMatchers("/", "/metadata", "/favicon.ico", "/api/**", "/*.css",
                              "/css/**", "/js/**", "/img/**", "/fonts/**").permitAll()
                      .antMatchers("/admin/**").hasRole("ADMIN")
                      .anyRequest().hasRole("USER")
                      .and()
                      .formLogin()
                      .loginPage("/login")
                      .permitAll()
                      .defaultSuccessUrl("/user.html", true)
                      .failureUrl("/login?error=true")
                      .permitAll()
                      .and()
                      .logout()
                      .logoutSuccessUrl("/");
          }
  
          @Override
          public void configure(AuthenticationManagerBuilder auth) throws Exception {
              auth.authenticationProvider(new AuthenticationProvider(idpConfiguration));
          }
      }
  }
  </code></pre>