如何使用基于java的配置创建两个http安全配置?
在XML配置中,我可以创建以下内容:如何使用基于java的配置创建两个http安全配置?,java,spring-security,Java,Spring Security,在XML配置中,我可以创建以下内容: 这将允许对/api/**的所有调用在没有会话的情况下不尝试对用户进行身份验证 如何使用基于Java的配置创建相同的配置 我的WebSecurityConfigurerAdapter#configure(HttpSecurity)方法如下所示: @Override protected void configure(HttpSecurity http) throws Exception { http.addFilter(switchUserFilte
这将允许对/api/**
的所有调用在没有会话的情况下不尝试对用户进行身份验证
如何使用基于Java的配置创建相同的配置
我的WebSecurityConfigurerAdapter#configure(HttpSecurity)
方法如下所示:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilter(switchUserFilter())
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/static/**").permitAll()
.anyRequest().authenticated()
.and().formLogin()
.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/")
.and().logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/");
}
这在Spring安全参考手册中是明确的,您使用
@Order()
注释放置了尽可能多的@Configuration
注释的内部类,以指定首先检查哪些类。在您的示例中,它可能看起来像:
@Order(1)
@Configuration
private static class ApiSecurityConfigurationAdapter
extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**")
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
.and.httpBasic().authenticationEntryPoint(xBasicAuthenticationEntryPoint)
.and.authorizeRequests()
.anyRequest().authenticated();
}
}
@Configuration
private static class NormalSecurityConfigurationAdapter
extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilter(switchUserFilter())
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/static/**").permitAll()
.anyRequest().authenticated()
.and().formLogin()
.loginPage("/login")
.permitAll()
.defaultSuccessUrl("/")
.and().logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/");
}
}
谢谢,这是正确的,我不知道为什么我没有看到答案,但就在我发布后,我重新检查了文档并找到了解决方案。虽然我确信上次我看的时候没有,也许是3.2.x版的新版本?我不知道如何注册我的
UserDetailsService
服务,尽管我已经求助于从外部创建bean,然后自动连接到每个配置中,并覆盖UserDetailsService()。谢谢