0
votes

I am configuring endpoint access in Spring Security. What I want to accomplish:

  1. Everyone has access to resources
  2. Everyone can login/register
  3. Only authenticated users can access logout and all other mapped endpoints

Here is my configuration, it fulfills firs two requirements and prevents access to /logout for non-logged users.


    http.authorizeRequests()
                    .antMatchers("/register").permitAll()
                    .antMatchers("/register/*").permitAll()
                    .antMatchers("/favicon.ico").permitAll()
                    .antMatchers("**/*.html").permitAll()
                    .antMatchers("**/*.css").permitAll()
                    .antMatchers("**/*.js").permitAll()
                    .and()
                    .formLogin().loginPage("/login").failureUrl("/login-error").defaultSuccessUrl("/")
                    .usernameParameter("username").passwordParameter("password")
                    .and()
                    .logout().logoutSuccessUrl("/login").deleteCookies("JSESSIONID").logoutUrl("/logout");

1
As a side note, it's usual to use something like /resources/** or /public/** for static CSS/JS resources. - chrylis -cautiouslyoptimistic-

1 Answers

2
votes

to ensure that request to your application requires the user to be authenticated use .anyRequest().authenticated()