在使用Tomcat启动Spring Boot时,用户名和密码是什么?

20 浏览
0 Comments

在使用Tomcat启动Spring Boot时,用户名和密码是什么?

当我通过Spring Boot部署我的Spring应用并访问localhost:8080时,我需要进行身份验证,但是用户名和密码是什么或者我如何设置它?我尝试将这个添加到我的tomcat-users文件中,但是它没有运行:

    

这是应用程序的起始点:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

这是Tomcat依赖项:

    org.springframework.boot
    spring-boot-starter-tomcat
    provided

我如何在localhost:8080上进行身份验证?

admin 更改状态以发布 2023年5月24日
0
0 Comments

如果在类路径中添加了spring-security的jar包,并且它是一个spring-boot应用程序,则所有HTTP端点默认都将由安全配置类SecurityAutoConfiguration保护。

这会导致浏览器弹出要求输入凭据的窗口。

每次应用程序重启时,密码会更改,并可在控制台中找到。

Using default security password: 78fa095d-3f4c-48b1-ad50-e24c31d5cf35

如果要在默认值之前添加自己的应用程序安全层,

@EnableWebSecurity
public class SecurityConfig {
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

或者如果您只想更改密码,可以使用以下方式覆盖默认值:

application.xml

security.user.password=new_password

或者

application.properties

spring.security.user.name=<>
spring.security.user.password=<>

0
0 Comments

我认为你的类路径上有Spring Security,然后Spring Security会自动配置一个默认用户和生成密码。

请查看你的pom.xml文件:


    org.springframework.boot
    spring-boot-starter-security

如果你的pom中有这个,那么你应该会在控制台看到如下的日志信息:

Using default security password: ce6c3d39-8f20-4a41-8e01-803166bb99b6

在浏览器提示中,你需要输入用户名user和在控制台输出的密码。

如果你想配置Spring Security,可以看一下Spring Boot secured example

它在Spring Boot参考文档的Security部分有详细说明:

The default AuthenticationManager has a single user (‘user’ username and random password, printed at `INFO` level when the application starts up)
Using default security password: 78fa095d-3f4c-48b1-ad50-e24c31d5cf35

0