Spring Security:多个HTTP配置不起作用

23 浏览
0 Comments

Spring Security:多个HTTP配置不起作用

我正在尝试使用Spring Security,并且我有一个使用不同的登录页面和不同的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");

}

}

这些类是另一个类MultipleHttpSecurityConfig的内部类,这个类有注解@EnableWebSecurity。

对于admin/**的安全性工作正常,但是consumer/**页面没有被保护,登录页面也没有进行重定向。我已经搜索了其他答案,但没有一个起作用。

0
0 Comments

问题原因:第一个WebSecurityConfigurerAdapter的http.authorizeRequests()方法匹配所有的URL,解决方法是使用antMatcher限制只匹配以/admin开头的URL。

解决方法:

(1)
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/admin/**")  // 使用antMatcher限制只匹配以/admin开头的URL
                .authorizeRequests()
                .antMatchers("/admin/login").permitAll()
                .antMatchers("/admin/**").access("hasRole('BASE_USER')")
                .and()
                ...

Spring Security: 多个HTTP配置不起作用的问题

在Spring Security中,如果我们想要配置多个不同的HTTP配置,有时候可能会遇到配置不起作用的问题。下面我们来看看这个问题出现的原因以及解决方法。

问题的原因是,第一个WebSecurityConfigurerAdapter的http.authorizeRequests()方法匹配了所有的URL。为了解决这个问题,我们可以使用antMatcher来限制只匹配以/admin开头的URL。

具体的解决方法如下所示:

(1)
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{
    protected void configure(HttpSecurity http) throws Exception {
        http
            .antMatcher("/admin/**")  // 使用antMatcher限制只匹配以/admin开头的URL
                .authorizeRequests()
                .antMatchers("/admin/login").permitAll()
                .antMatchers("/admin/**").access("hasRole('BASE_USER')")
                .and()
                ...

通过以上的解决方法,我们可以成功解决Spring Security多个HTTP配置不起作用的问题。希望对大家有所帮助!

0
0 Comments

Spring Security中的Multiple HTTP Config not working问题出现的原因是配置了多个WebSecurityConfigurerAdapter,但只有第一个配置生效,其他的配置被忽略。解决方法是通过使用antMatcher()方法来指定每个配置适用的URL。

在给出的代码中,第一个配置类是MultiHttpSecurityConfig,其中的configureGlobal方法配置了两个用户,一个是user角色,一个是admin角色。第二个配置类是ApiWebSecurityConfigurationAdapter,通过antMatcher("/api/**")指定该配置类适用于以/api/开头的URL,而且要求有ADMIN角色的权限。第三个配置类是FormLoginWebSecurityConfigurerAdapter,该配置类没有指定antMatcher,所以默认适用于所有URL,要求用户进行认证。

问题的原因是第一个配置类没有指定antMatcher,所以它匹配所有的URL,而且只有/admin/**的URL有限制,其他URL都是默认允许的。所以第二个配置类并没有起作用。

解决方法是在第一个配置类中指定antMatcher,将其限制在需要的URL范围内。例如,可以将第一个配置类修改为以下内容:

public void configure(HttpSecurity http) throws Exception {
    http
        .antMatcher("/admin/**")
        .authorizeRequests()
            .anyRequest().hasRole("ADMIN")
            .and()
        .httpBasic();
}

这样就可以确保第一个配置类只适用于以/admin/开头的URL,并且要求有ADMIN角色的权限。其他URL仍然由第二个配置类处理。

0