Home  >  Article  >  Java  >  How to solve the problem that username and password are required to log in to the web page after enabling springboot security

How to solve the problem that username and password are required to log in to the web page after enabling springboot security

WBOY
WBOYforward
2023-05-16 22:02:181732browse

    Question

    How to solve the problem that username and password are required to log in to the web page after enabling springboot security

    Note: I use Spring Boot 2.0.2, which may not be useful for the 1.5.x series.

    Direct solution

    0, remove spring-boot-starter-security dependency

    If you do not actually use the security function, you can directly remove spring-boot-starter -security depends on

    1, use the default user and password to log in

    The default user name is user
    The password is a string automatically generated when the program starts

    How to solve the problem that username and password are required to log in to the web page after enabling springboot security

    2, disable security settings or set the corresponding user and password

    You can configure the corresponding user and password in application.properteis

    You can also set the corresponding user Name and password
    spring.security.user.name=user1
    spring.security.user.password=password1

    By disabling

    package com.yq;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication(scanBasePackages = {"com.yq"})
    @EnableAutoConfiguration(exclude = {
    org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
    })
    public class WebSecurityDemoApp {
        private static final Logger log = LoggerFactory.getLogger(WebSecurityDemoApp.class);
    
        public static void main(String[] args) {
            SpringApplication.run(WebSecurityDemoApp.class, args);
        }
    
    }

    parsing

    on the startup main class

    As long as our Spring Boot project references the following dependencies, the security configuration will be enabled by default.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

    If you want to use security but don’t want to enter the user name and password every time, you can disable automatic configuration directly in the Application file

    @EnableAutoConfiguration(exclude = {
    org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
    })

    Or we can also configure the specified user and password, for example
    spring.security.user.name=user1
    spring.security.user.password=password1

    The above is the detailed content of How to solve the problem that username and password are required to log in to the web page after enabling springboot security. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete