Java Spring安全性:多个HTTP配置不工作
我正在尝试使用Spring安全性,我有一个用例,我希望不同的登录页面和不同的URL集得到安全保护 以下是我的配置:Java Spring安全性:多个HTTP配置不工作,java,spring,security,spring-mvc,spring-security,Java,Spring,Security,Spring Mvc,Spring Security,我正在尝试使用Spring安全性,我有一个用例,我希望不同的登录页面和不同的URL集得到安全保护 以下是我的配置: @Configuration @Order(1) public static class ProviderSecurity extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { htt
@Configuration
@Order(1)
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/home").permitAll()
.antMatchers("/admin/login").permitAll()
.antMatchers("/admin/**").access("hasRole('BASE_USER')")
.and()
.formLogin()
.loginPage("/admin/login").permitAll()
.defaultSuccessUrl("/admin/home")
.failureUrl("/admin/login?error=true").permitAll()
.usernameParameter("username")
.passwordParameter("password")
.and()
.csrf()
.and()
.exceptionHandling().accessDeniedPage("/Access_Denied");
}
}
@Configuration
@Order(2)
public static class ConsumerSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/consumer/login").permitAll()
.antMatchers("/consumer/**").access("hasRole('BASE_USER')")
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/consumer/login").permitAll()
.defaultSuccessUrl("/consumer/home")
.failureUrl("/consumer/login?error=true").permitAll()
.usernameParameter("username")
.passwordParameter("password")
.and().csrf()
.and()
.exceptionHandling().accessDeniedPage("/Access_Denied");
}
}
这些类是另一个类的内部类,该类具有注释@EnableWebSecurity
admin/**
的安全性工作正常,但是consumer/**
页面没有安全性,登录页面没有重定向。我搜索了其他答案,但没有一个有效。请看:
1将身份验证配置为正常
2创建包含@Order
的websecurityConfigureAdapter
实例,以指定应首先考虑哪个websecurityConfigureAdapter
3http.antMatcher
声明此HttpSecurity
仅适用于以/api/
开头的URL
4创建websecurityConfigureAdapter
的另一个实例。如果URL不是以/api/
开头,则将使用此配置。此配置在apiWebSecurity配置适配器
之后考虑,因为它在1
之后有一个@Order
值(无@Order
默认为最后一个)
您的第二个配置未被使用,因为您的第一个配置与
/**
匹配(无antMatcher
配置)。并且您的第一个配置仅限制/admin/**
,默认情况下允许所有其他URL。您的第一个Web安全配置适配器
http
.authorizeRequests()
匹配所有URL,使用antMatcher将其限制为仅以/admin
开头的URL:
@Configuration
@Order(1)
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/admin/**")
.authorizeRequests()
.antMatchers("/admin/login").permitAll()
.antMatchers("/admin/**").access("hasRole('BASE_USER')")
.and()
...
换句话说,您可以指定多个websecurityConfigureAdapter
s,它们按@order
注释中指定的优先级顺序考虑。第一个匹配.requestMatchers().antMatchers(“/matcher/**”
)的将被使用,其他的将被丢弃。例如,websecurityConfigureAdapter
s不堆叠。
@Configuration
@Order(1)
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/admin/**")
.authorizeRequests()
.antMatchers("/admin/login").permitAll()
.antMatchers("/admin/**").access("hasRole('BASE_USER')")
.and()
...