Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring安全性:多个HTTP配置不工作_Java_Spring_Security_Spring Mvc_Spring Security - Fatal编程技术网

Java Spring安全性:多个HTTP配置不工作

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

我正在尝试使用Spring安全性,我有一个用例,我希望不同的登录页面和不同的URL集得到安全保护

以下是我的配置:

@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

3
http.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()

                ...