[Solution] After SpringBoot introduces Spring security, the login box pops up when publishing a project

Problem Description

Recently, in the SpringBoot development process, the Spring Security package was used, but after the reference, when the background project was released, the following interface appeared

insert image description here

Reason

In fact, this is a user authentication function that security gives us by default. The user name is: user The password is printed out in the startup console:
insert image description here
but after entering the user name and password, the login is 404, indicating that we have not configured any services. , to configure your own authentication, you need to add a configuration class that inherits the WebSecurityConfigurerAdapter adapter.

Solution

Let's talk about Spring Boot's introduction of Spring Security
insert image description here

<!--Spring security-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

Add the inheritance class of WebSecurityConfigurerAdapter
Create permission package and WebSecurityConfig class
insert image description here

specific code

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    
    
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    
    
        super.configure(auth);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
    
    
        super.configure(web);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    
    
        //关闭csrf跨域
        http.csrf().disable();
        //super.configure(http);
    }
}

If you have added a configuration class, this page just means that your configuration class does not work, it may be that your project has not been recompiled, and you need to recompile the project before publishing.

Guess you like

Origin blog.csdn.net/qq_29750461/article/details/122971764