Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set cookies as secure flag in spring boot

I am working on spring boot and completely unaware how it's work . while authenticating the login JSESSIONID is created as cookie . Login code is as below

 protected void configure(HttpSecurity http) throws Exception {
    http
            .formLogin()
            .loginProcessingUrl("/authenticate")
            .usernameParameter("username")
            .passwordParameter("password")
            .loginPage("/")
            .permitAll()
            .and()
            .logout()
            .permitAll()
            .and()
            .authorizeRequests()
            .antMatchers("/index.html", "/home.html", "/login.html", "/app/**", "/js/**", "/css/**", "/fonts/**", "/favicon.ico", "/").permitAll().anyRequest()
            .authenticated().and().csrf()
            .csrfTokenRepository(csrfTokenRepository()).and()
            .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
}

How to make the cookie have secure flag

like image 740
abhishek vashistha Avatar asked Dec 02 '22 11:12

abhishek vashistha


2 Answers

In application.properties set the following property:

server.servlet.session.cookie.secure=true

... or in older versions (before ~2018):

server.session.cookie.secure=true
like image 180
diginoise Avatar answered Dec 24 '22 21:12

diginoise


Property 'server.session.cookie.secure' is Deprecated:
Use 'server.servlet.session.cookie.secure' instead.

In the application.properties put it:

server.servlet.session.cookie.secure=true
like image 43
RivanMota Avatar answered Dec 24 '22 19:12

RivanMota