Home > Java > javaTutorial > 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
Release: 2023-05-16 22:02:18
forward
1793 people have browsed it

    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);
        }
    
    }
    Copy after login

    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>
    Copy after login

    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
    })
    Copy after login

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template