2017-08-07 94 views
1

我有春季安全在我的pom.xml,和春季安全使用默認的用戶自動配置和生成的密碼:春季安全:如何更改默認用戶和密碼?

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

如何更改默認的用戶名和密碼?

+0

假設這[帖子](https://stackoverflow.com/questions/37285016/what-is-username-and-password-when-starting-spring-boot-with-tomcat)可能會有幫助,並有一個解決方案爲了你的期望。 –

+0

[使用Tomcat啓動Spring Boot時用戶名和密碼是什麼?](https://stackoverflow.com/questions/37285016/what-is-username-and-password-when-starting-spring-boot-with -tomcat) – Graham

回答

2

這是straight from the docs

創建一個配置類:

@Configuration 
@EnableWebSecurity 
public class HelloWebSecurityConfiguration 
    extends WebSecurityConfigurerAdapter { 

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

Newer Docs

這是略有不同,但效果是一樣的:

@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; 
    } 
} 
+0

2013年7月3日舊文檔:'( – ahmedmess

+0

我剛剛在1.5.6版本中做了這個。 – Brian

+0

此外,增加了更新的文檔引用相同的配置。 – Brian

6

這可以在中輕鬆完成文件:

security.user.name=user # Default user name.

security.user.password= # Password

security.user.role= # A comma separated list of roles

這裏是documentation