java Spring Security:如何更改默认用户和密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45556821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:45:45  来源:igfitidea点击:

Spring Security: How to change default user and password?

javaspringspring-boot

提问by ahmedmess

I have Spring Security in my pom.xml, and Spring Security is automatically configured with a default user and generated password:

我的 pom.xml 中有 Spring Security,并且 Spring Security 自动配置了默认用户和生成的密码:

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

How do I change the default user and password?

如何更改默认用户和密码?

回答by Juan Carlos Mendoza

This can be easly done in your application.propertiesfile:

这可以在您的application.properties文件中轻松完成:

spring.security.user.name=user # Default user name.
spring.security.user.password= # Password
spring.security.user.role= # A comma separated list of roles

Here is the documentation.

这是文档

回答by Brian

This is straight from the docs:

这是直接来自文档

Create a configuration class:

创建一个配置类:

@Configuration
@EnableWebSecurity
public class HelloWebSecurityConfiguration
   extends WebSecurityConfigurerAdapter {

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

Newer Docs

较新的文档

This is slightly different, but the effect would be the same:

这略有不同,但效果是一样的:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService() throws Exception {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        manager.createUser(User.withUsername("user").password("password").roles("USER").build());
        return manager;
    }
}

回答by Santosh G P

#add these lines in application.properties
    spring.security.user.name=username
    spring.security.user.password=password

回答by sonal dadarya

Add below properties in application.properties

添加以下属性 application.properties

spring.security.user.name= user_name
spring.security.user.password= user_password

where "user_name" will be the user and "user_password" will be the password.

其中“ user_name”将是用户,“ user_password”将是密码。

回答by nkalra0123

These will not work with old version of spring boot, I was using 1.5.11.RELEASE and these properties were not working, After moving to 2.1.8.RELEASE, these properties work fine.

这些不适用于旧版本的 spring boot,我使用的是 1.5.11.RELEASE 并且这些属性不起作用,移动到 2.1.8.RELEASE 后,这些属性工作正常。

check your pom.xml

检查你的 pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

spring.security.user.name=username
spring.security.user.password=password