Spring Boot - 如何在Spring Security中覆盖默认的用户名和密码

5 浏览
0 Comments

Spring Boot - 如何在Spring Security中覆盖默认的用户名和密码

我有一个Spring Boot应用程序,我正在尝试实现Spring Security来覆盖Spring生成的默认用户名和密码。但是它并没有起作用,Spring仍然使用默认的用户凭据。

这是我的Spring Boot主类:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, SecurityAutoConfiguration.class })

@EnableAsync

@EntityScan({ "com.demo.entity" })

@EnableJpaRepositories(basePackages = { "com.demo.repositories" })

@ComponentScan({ "com.demo.controller", "com.demo.service" })

@Import({ SwaggerConfig.class })

public class UdeManagerServiceApplication extends SpringBootServletInitializer {

@Override

protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {

return application.sources(UdeManagerServiceApplication.class);

}

public static void main(String[] args) {

SpringApplication.run(UdeManagerServiceApplication.class, args);

}

}

这是日志,它显示了生成的密码:

22:26:48.537 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : []

22:26:48.540 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for restart : [/spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/, /spring-boot/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter/target/classes/]

22:26:48.540 [main] DEBUG org.springframework.boot.devtools.restart.ChangeableUrls - Matching URLs for reloading : [file:/C:/Work/Spring/UdeManagerService/target/classes/]

2018-10-18 22:26:48.797 DEBUG 7652 --- [ restartedMain] o.s.w.c.s.StandardServletEnvironment - Adding PropertySource 'configurationProperties' with highest search precedence

...

请注意,这里省略了一部分日志内容。

0
0 Comments

问题的原因是需要在Spring Security中重写默认的用户名和密码。解决方法是注册一个UserDetailsService bean,并将InMemoryUserDetailsManager公开为一个bean。具体操作是在WebSecurityConfiguration.class中将UserDetailsService暴露为一个bean,代码如下:

// 将UserDetailsService作为一个bean暴露出来
public UserDetailsService userDetailsServiceBean() throws Exception {
    return super.userDetailsServiceBean();
}

通过以上操作,可以重写默认的用户名和密码。

0
0 Comments

在Spring Boot中,使用Spring Security来进行身份认证和授权是非常常见的。默认情况下,Spring Security会使用一组默认的用户名和密码来验证用户。

然而,在某些情况下,我们可能需要自定义用户名和密码,以提高安全性或满足特定需求。解决这个问题的方法是在application.properties文件中添加以下代码:

spring.security.user.name = root -- 这是您的用户名

spring.security.user.password = root -- 这是您的密码

spring.security.user.roles = user -- 这是您的角色

通过在application.properties文件中添加以上代码,我们可以轻松地覆盖默认的用户名和密码,并自定义自己的凭证信息。这样,当用户进行身份验证时,系统将使用我们指定的用户名和密码进行验证。

需要注意的是,这种方法只适用于开发和测试环境。在生产环境中,我们应该使用更安全的方式来管理用户名和密码,例如使用环境变量或配置文件。

总结起来,通过在application.properties文件中添加自定义的用户名和密码,我们可以轻松地覆盖Spring Security的默认凭证信息,从而实现自定义身份认证和授权的需求。这为我们提供了更大的灵活性和安全性。

0
0 Comments

Spring Boot中的Spring Security模块提供了默认的用户名和密码用于身份验证。然而,有时候我们希望自定义这些默认的用户名和密码。为了实现这一点,我们可以在application.properties或application.yaml文件中使用以下属性:

- spring.security.user.name:用于指定自定义的用户名。

- spring.security.user.password:用于指定自定义的密码。

- spring.security.user.roles:用于指定自定义用户的角色。

通过设置这些属性,我们可以覆盖默认的用户名和密码。在application.properties或application.yaml文件中添加这些属性后,Spring Security将使用指定的用户名和密码进行身份验证。

我们可以参考Spring Boot的官方文档中的这个链接,了解更多关于这些属性的详细信息。

使用这些属性,我们可以轻松地在Spring Boot中自定义默认的用户名和密码,以满足我们的特定需求。

0