2016-05-17 12 views
40

當我通過Spring Boot部署我的Spring應用程序並訪問localhost:8080時,我必須進行身份驗證,但是用戶名和密碼是什麼或者如何設置?我試圖把它添加到我的tomcat-users文件,但它沒有工作:使用Tomcat啓動Spring Boot時,用戶名和密碼是什麼?

<role rolename="manager-gui"/> 
    <user username="admin" password="admin" roles="manager-gui"/> 

這是應用程序的起點:

@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的依賴性:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-tomcat</artifactId> 
    <scope>provided</scope> 
</dependency> 

如何在localhost:8080上進行驗證?

+0

通過設置[彈簧引導起動的安全性](https://spring.io/guides/gs/securing-web/)。 –

+0

您需要進行身份驗證=您想要進行身份驗證?因爲在spring-boot-starter-tomcat/-web中沒有身份驗證,也沒有用戶名和密碼。如果你看到一些,它可能是一個不同的應用程序:8080 – zapl

+1

它啓動時打印在控制檯上。 – chrylis

回答

90

我認爲你有Spring Security的類路徑,然後春季安全自動使用默認的用戶配置和生成的密碼

請看看你的pom.xml文件:

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

如果你有一個在你的POM比你應該有這樣的日誌控制檯消息:

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

在瀏覽器提示符下,您將導入用戶user以及在控制檯中打印的密碼。

或者,如果你想配置Spring Security,你可以看看Spring Boot secured example

它的春天引導參考文檔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 
+10

如果您很好地調整了日誌記錄配置,請確保'org.springframework.boot.autoconfigure.security'類別設置爲記錄INFO消息,否則將不會打印默認密碼。 – Zeeshan

+0

美麗。一切都默認爲某種東西。雙刃劍。 –

5

如果spring-security jars被添加到類路徑中,並且如果它是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。密碼= NEW_PASSWORD

1

您也可以索要憑證的用戶,並動態地設置他們在服務器啓動之後(非常有效的,當您需要發佈在客戶環境中的解決方案):

@EnableWebSecurity 
public class SecurityConfig { 

    private static final Logger log = LogManager.getLogger(); 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     log.info("Setting in-memory security using the user input..."); 

     Scanner scanner = new Scanner(System.in); 
     String inputUser = null; 
     String inputPassword = null; 
     System.out.println("\nPlease set the admin credentials for this web application"); 
     while (true) { 
      System.out.print("user: "); 
      inputUser = scanner.nextLine(); 
      System.out.print("password: "); 
      inputPassword = scanner.nextLine(); 
      System.out.print("confirm password: "); 
      String inputPasswordConfirm = scanner.nextLine(); 

      if (inputUser.isEmpty()) { 
       System.out.println("Error: user must be set - please try again"); 
      } else if (inputPassword.isEmpty()) { 
       System.out.println("Error: password must be set - please try again"); 
      } else if (!inputPassword.equals(inputPasswordConfirm)) { 
       System.out.println("Error: password and password confirm do not match - please try again"); 
      } else { 
       log.info("Setting the in-memory security using the provided credentials..."); 
       break; 
      } 
      System.out.println(""); 
     } 
     scanner.close(); 

     if (inputUser != null && inputPassword != null) { 
      auth.inMemoryAuthentication() 
       .withUser(inputUser) 
       .password(inputPassword) 
       .roles("USER"); 
     } 
    } 
} 
3

因此通過添加彈簧引導安全啓動器依賴項,基本安全性已經默認配置。

enter image description here

我們可以通過編寫我們自己的認證和授權自定義的安全配置。爲此創建一個新的類SecurityConfig,它擴展了WebSecurityConfigurerAdapter並覆蓋了它的方法。

@Override 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("javainuse") 
       .password("javainuse").roles("USER"); 
    } 

價 - Spring Boot Security Example

+0

解釋有幫助 – Rameez

相關問題