gpt4 book ai didi

java - 如何通过 application.properties 禁用 spring-security?

转载 作者:行者123 更新时间:2023-12-01 14:28:57 32 4
gpt4 key购买 nike

我想激活 basic authentication使用密码哈希加密。

@Configuration //gets picked up automatically by spring-boot
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(details).passwordEncoder(new BCryptPasswordEncoder());
}
}

我希望身份验证仅在生产中处于 Activity 状态。因此,我首先尝试将其停用,请使用:
security.basic.enabled=false
结果:应用程序仍然是安全的。我看到了用户名/密码屏幕。

但是我怎么能禁用身份验证的需要呢?

最佳答案

要在不同的环境中做不同的事情,你应该使用 Spring Profiles

假设您仅在生产环境中需要安全配置 bean,那么您应该将其标记为在启用特定配置文件时有条件地加载。

@Configuration //gets picked up automatically by spring-boot
@Profile("Production")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(details).passwordEncoder(new BCryptPasswordEncoder());
}
}

配置文件特定配置在名为 application-{profile}.properties 的属性文件中完成。和 bootstrap-{profile}.properties文件。

现在,由于安全配置 bean 被标记为配置文件 Production只有在 Production 时才会加载配置文件已启用。

要启用配置文件,您必须设置以下属性。
#One or more profiles can be active simultaneously
spring.profiles.active=Production,Dev,Local

该属性可以在通用属性文件 application.properties 中进行编辑。 (总是被加载,不像 application-Production.properties 等有条件加载的配置文件特定属性文件)或者它可以作为环境变量提供,如下所示。
On Windows
set SPRING_PROFILES_ACTIVE=Production
On Unix
export SPRING_PROFILES_ACTIVE=Production

可以使用 Spring 支持的一百万种其他方式来加载属性。有关所有方法的列表,请访问此链接 https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

最后要禁用默认的 Spring security (Basic) Autoconfiguration,您可以使用以下属性。
security.basic.enabled=false
management.security.enabled=false #For actuator

以上应该在您希望禁用 Spring Security 自动配置的配置文件特定属性文件中。

关于java - 如何通过 application.properties 禁用 spring-security?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44783643/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com