java – 通过@Profile启用WebSecurityConfigurer不起作用
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 通过@Profile启用WebSecurityConfigurer不起作用,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3602字,纯文字阅读大概需要6分钟。
内容图文
![java – 通过@Profile启用WebSecurityConfigurer不起作用](/upload/InfoBanner/zyjiaocheng/765/869cb65f92fd458295c4c8f2522225a4.jpg)
我认为,我有一个非常简单和基本的设置,用于本地运行带有一些身份验证的Spring Boot webapp.
我希望当我通过Spring Boot运行此应用程序时,我指定本地配置文件时,我的自定义安全设置将覆盖默认行为.
mvn -Dspring.profiles.active =“local”spring-boot:run
也许我正在指定profiles.active错误,但是当应用程序运行时,它仍会吐出生成的密码以供使用,并且似乎不允许在没有所述身份验证的情况下访问/ login路径.
我也没有看到/ env下的活动配置文件,这可能有点说.
我有一个WebSecurityConfigurer被覆盖,如下所示:
@Configuration
@EnableWebSecurity
@Profile("local")
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().fullyAuthenticated().and().formLogin().permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN", "USER")
.and().withUser("user").password("user").roles("USER");
}
}
我的主要@Configuration类是您标准的Spring Java风格的基本配置:
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
解决方法:
第二次尝试提供更好的安全设置控制.什么是控制安全自动配置的高级选项:
>完全永久地关闭安全性:
>从类路径中删除Spring Security
>或排除安全性自动配置 – @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
>通过设置security.basic.enabled = false来关闭默认的基本身份验证安全性
如果您完全控制安全设置,安全自动配置和弹簧配置文件的使用方式,则可以非常轻松地控制不同的安全设置.
@Configuration
@ComponentScan
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
@Configuration
public class WebSecurityConfig {
@Configuration
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
@ConditionalOnExpression("!${my.security.enabled:false}")
protected static class DefaultWebSecurityConfig {
}
@Configuration
@EnableAutoConfiguration
@EnableWebMvcSecurity
@Profile("local")
@ConditionalOnExpression("${my.security.enabled:false}")
protected static class LocalWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.anyRequest().authenticated();
http
.formLogin().loginPage("/login").permitAll().and()
.logout().permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
}
在上面的类中,我基本上从Application类顺序中删除了@EnableAutoConfiguration以有条件地使用它.创建了两个配置类,DefaultWebSecurityConfig和LocalWebSecurityConfig,它们由my.security.enabled标志使用Boot @ConditionalOnExpression选择.
如果我的安全性未启用,则第一个配置只会排除SecurityAutoConfiguration.第二个启用安全性并使用本地配置文件.通过使用不同的配置文件创建另一个配置,您可以控制不同配置文件发生的情况.然后,您可以选择是否启用安全性以及使用哪个配置文件:
#java -jar build/libs/gs-securing-web-0.1.0.jar
#java -jar build/libs/gs-securing-web-0.1.0.jar --spring.profiles.active=local --my.security.enabled=true
如果您可以选择使用application.yml,则每个配置文件可以自动应用不同的设置,但仍定义默认值.如果您只想禁用默认安全自动配置启用的默认基本身份验证,这将是一件好事.
security:
basic:
enabled: false
---
spring:
profiles: local
security:
basic:
enabled: true
---
可能有一百万种不同的方法来执行这些操作,并且始终根据具体情况最适合当前用例.
内容总结
以上是互联网集市为您收集整理的java – 通过@Profile启用WebSecurityConfigurer不起作用全部内容,希望文章能够帮你解决java – 通过@Profile启用WebSecurityConfigurer不起作用所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。