Spring Boot控制台密码

3
我使用Spring Boot,我已经阅读过默认日志文件或控制台应该打印出的内容 - 但我的日志里没有任何东西。我使用putty连接,连接很好,但我不知道凭据(密码)。有什么提示我可以做来让它工作吗?
[编辑] 我还将以下内容添加到我的application.properties文件:
security.user.name=user
security.user.password=secret

但是没有效果。

日志文件截图:

enter image description here

这些是我的依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency> 
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>           
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.smartinnotec.accounting</groupId>
        <artifactId>smartinnotec-accounting-frontend</artifactId>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.10.4</version>
    </dependency>
</dependencies>

[编辑]

如果我删除这两个注释,那么密码会在启动时打印出来。 我的问题是,我需要在这个类中定义的Bean。 实际上,我不知道该怎么办? 是否可能同时启用Web安全性并打印出密码?

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

private static final String[] restEndpointsToSecure;
@Autowired
private XAuthTokenConfigurer xAuthTokenConfigurer;

static {
    restEndpointsToSecure = new String[7];
    ...
}

@Autowired(required = true)
private UserService userService;

public WebSecurityConfig() {
}

@Override
protected void configure(final HttpSecurity http) throws Exception {

    final String[] restEndpointsToSecure = WebSecurityConfig.restEndpointsToSecure;
    for (final String endpoint : restEndpointsToSecure) {
        http.authorizeRequests().antMatchers("/" + endpoint + "/**").hasRole(UserRoleEnum.USER.toString());
    }

    http.csrf().disable();
    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
    http.addFilterAfter(csrfTokenResponseHeaderBindingFilter(), CsrfFilter.class);

    xAuthTokenConfigurer.setDetailsService(userDetailsServiceBean());
    final SecurityConfigurer<DefaultSecurityFilterChain, HttpSecurity> securityConfigurerAdapter = xAuthTokenConfigurer;
    http.apply(securityConfigurerAdapter);
}

@Override
protected void configure(final AuthenticationManagerBuilder authManagerBuilder) throws Exception {
    authManagerBuilder.userDetailsService(new CustomUserDetailsService(userService));
}

@Bean
public CsrfTokenResponseHeaderBindingFilter csrfTokenResponseHeaderBindingFilter() {
    return new CsrfTokenResponseHeaderBindingFilter();
}

@Bean
@Override
public UserDetailsService userDetailsServiceBean() throws Exception {
    return super.userDetailsServiceBean();
}

@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

@Override
public String toString() {
    return "[WebSecurityConfig]";
}
}

你使用Putty连接到什么?你不知道哪些凭据?你如何实际使用Spring Boot? - eis
2
你的pom/classpath中是否有Spring Security?例如,你可以手动设置security.user.name=admin和security.user.password=secret。这样可以避免查找凭据的需要。 - lawal
这可能会有所帮助 入门指南:Spring Security - Ekansh Rastogi
请展示您的pom.xml文件。您是否有spring-boot-starter-security依赖项? - alexbt
如果您在application.properties中添加了安全凭据,则默认密码将不会在控制台中打印,您可以使用这些凭据。 - Derrick
显示剩余3条评论
2个回答

2
如果您将'Spring Security'添加到项目中,所有通过HTTP公开的敏感端点都将受到保护。默认情况下,将使用用户名'user'和生成的密码(应用程序启动时在控制台上打印)进行'basic'身份验证,就像以下内容所示:

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

然后,您可以使用此密码登录。
但是,您需要确保将org.springframework.boot.autoconfigure.security类别设置为记录INFO消息,否则默认密码将不会被打印。
或者,您可以通过提供security.user.name作为用户名和security.user.password作为密码来更改密码,然后默认密码将不会在控制台上打印。

0
我使用putty进行连接。
我认为你正在寻找一个远程shell连接到执行器端点。如果是这样,你应该将以下内容添加到你的依赖树中。
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-remote-shell</artifactId>
 </dependency>

Spring 会在控制台中打印默认的远程 shell 密码:

Using default password for shell access: 9b1abe6e-4ec0-4dca-a97a-fed2dadf1837

如果要覆盖默认的用户名/密码,请将以下内容添加到您的application.properties文件或yml中。

management.shell.auth.simple.user.name=username
management.shell.auth.simple.user.password=password

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接