Я новичок в сервлетах.Мне нужно создать собственное значение cookie.В приведенном ниже коде сессионный cookie создается, но мне нужен еще один с пользовательским значением.
public ServletContextInitializer servletContextInitializer() {
servletContext -> servletContext.getSessionCookieConfig().setName("sessiondemo");
}
Мой код сервлета такой, как показано ниже.В строке комментария мне нужно добавить куки.По сути, я перенаправляю запрос, который приходит сюда, чтобы начать сеанс сервлета. Мне также нужно сгенерировать cookie с именем hello и значением world в servletcontext.
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 -> 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));
}
}
}