【发布时间】:2014-10-29 17:51:02
【问题描述】:
我正在尝试使用基本身份验证设置一个普通的 Spring Boot 环境。
基本上我唯一想要自定义的是用户、受保护的路径和自定义密码编码器。
Spring Boot 文档指出:
在不更改任何其他自动配置的情况下覆盖访问规则 功能添加一个类型为 WebConfigurerAdapter 的 @Bean @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)。
注意:我认为 WebConfigurerAdapter 应该是 WebSecurityConfigurerAdapter。
所以我尝试了以下方法:
protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests()
.antMatchers("/assets/**").permitAll()
.antMatchers("/api/**").hasRole("USER")
.antMatchers("/management/**").hasRole("ADMIN");
// @formatter:on
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// @formatter:off
auth
.inMemoryAuthentication()
.passwordEncoder(passwordEncoder)
.withUser("admin")
.password(passwordEncoder.encode("pwd"))
.roles("USER", "ADMIN")
.and()
.withUser("user")
.password(passwordEncoder.encode("pwd"))
.roles("USER");
// @formatter:on
}
}
默认的启动安全似乎正是我想要的:
security.enable-csrf=false
security.basic.enabled=true
security.sessions=stateless
但是,当我运行应用程序时,基本身份验证不起作用。
当我在我的 WebSecurityConfigurerAdapter 中使用 http.httpBasic() 明确配置它时:
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/assets/**").permitAll()
.antMatchers("/api/**").hasRole("USER")
.antMatchers("/management/**").hasRole("ADMIN");
// @formatter:on
}
那么基本身份验证就起作用了。
所以上面的初始设置似乎没有采用默认配置。
我错过了什么吗?
【问题讨论】:
-
但是你的例子没有使用
@Order...
标签: spring spring-security spring-boot