2016-05-17 500 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

相关问题