Skip to content

how to configure spring security for spring boot project

I’m trying to make a web application that uses: SpringBoot, Mysql, JDBC , MVC, DAO Thymeleaf, IntelliJ

And I’m trying to figure out how Spring security works (which I’m having a lot of difficulty with). My views are organized as follows:

resources(folder): - ________static(folder)
                                                      |                       |__help.html
                                                      |                       |__menu.html
                                                      |                       |__newDocForm.html
                                                      |                       |__profil.html

I would like to do that unidentified users can access all views except those contained in “userOnly” and that my “loginPage” page is used as the login page.

If I understood correctly, I must create a class that inherits from “WebSecurityConfigurerAdapter”. What I have done. And then configure “configure”, which I can’t do correctly 🙁

public class SecSecurityConfig extends WebSecurityConfigurerAdapter {

    protected void configure(final HttpSecurity http) throws Exception {

Sorry if my questions seems strange but english is not my first language



As of Spring-Boot 2.7 the use of WebSecurityConfigurerAdapter is deprecated. If you’re using Spring-Boot 2.6 or older the other answers might suit you better.

To my best knowledge the recommended way for defining security config in Spring-Boot 2.7 is as follows:

public class WebSecurityConfig
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception
        // @formatter:off
        // @formatter:on

The use of web.ignoring() in the answer from voucher_wolves is, I believe, not recommended, instead one should add those cases to http.mvcMatcher().permitAll(). On a side note, I would personally recommend whitelisting the public pages and adding authentication to everything else, (for example a public folder). This way if you forget to add security to a link it’s not public by default.