【问题标题】:Spring 2 security: 403 Forbidden [duplicate]Spring 2 安全性:403 Forbidden [重复]
【发布时间】:2021-06-21 00:31:02
【问题描述】:

我有一个以前使用 Spring Boot 1 运行测试的应用程序,我已更新到 2.0.9.RELEASE。

Spring Security 现在存在问题。我知道是这种情况,因为如果我删除

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <scope>test</scope>
</dependency>

(以及任何新的 WebSecurityConfigurerAdapter 类)测试仍然成功。测试本质上是转到项目控制器“HomeController”,然后从那里转到服务并使用 RestTemplate 执行各种操作。实际上这是一个不同的应用程序,如果我从头开始编写它,我可能会做一个wiremock,但现在和这个问题的目的是在测试包中有一个控制器来模拟所需的行为。

通过添加这个简单的类,我已经通过了本地控制器上的 403

@TestConfiguration
@EnableWebSecurity
@Order(500)
public class DefaultSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure( HttpSecurity http) throws Exception{
        http.authorizeRequests().antMatchers("/").permitAll();
    }
}

但是,我正在努力克服使用 RestTemplate 的安全性。 restTemplate bean 是使用基本用户和密码创建的(这里稍微简化了一点)

@Bean
public RestTemplate restTemplate( RestTemplateBuilder restTemplateBuilder ) {
    return restTemplateBuilder
            .basicAuthorization( "user", "password" )
            .build();
}

我可以通过调试和查看拦截器来判断这些拦截器正在被设置。

现在我添加了一个新的配置类

@TestConfiguration
@EnableWebSecurity
@Order (50)
public class RestTemplateSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure( AuthenticationManagerBuilder auth) throws Exception {
        PasswordEncoder encoder =
            PasswordEncoderFactories.createDelegatingPasswordEncoder();

        auth.inMemoryAuthentication()
            .withUser("user")
            .password(encoder.encode("password"))
            .roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests().antMatchers("/otherpath/**")
            .authenticated()
            .and()
            .httpBasic();

    }
}

但这似乎对 RestTemplate 调用没有影响,这些调用始终以用户和密码的基本安全性进行调用,并始终返回 403 Forbidden。

测试类是这样注释的

@AutoConfigureMockMvc
@RunWith( SpringRunner.class )
@SpringBootTest( classes = { DefaultSecurityConfiguration.class, ResttemplateSecurityConfiguration.class }, webEnvironment = DEFINED_PORT ) 
@TestPropertySource( "classpath:test.properties" )
public class HomeControllerTest {
...

并使用“标准”mockMvc.perform( ...

使用 DefaultSecurityConfiguration 类,这些触发没有问题,是应用程序代码中使用 restTemplate 的后续调用失败

有什么明显的吗?

【问题讨论】:

  • 感谢您提供有关上一个答案的信息。以为我会阅读所有内容,但错过了这一本。似乎是问题所在。实际上,我似乎根本不需要更复杂的身份验证配置,只需禁用 csrf!谢谢。

标签: java spring-boot spring-security


【解决方案1】:

替换:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests().antMatchers("/otherpath/**")
        .authenticated()
        .and()
        .httpBasic();

}

通过这个:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests().antMatchers("/otherpath/**")
        .authenticated()
        .and()
        .httpBasic();
    http.csrf().disable();
}

【讨论】:

  • 如果以后有人使用它,实际上我只需要添加 and().csrf().disable();到我的 DefaultSecurityConfiguration 类。
猜你喜欢
  • 2016-10-26
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 2019-09-23
相关资源
最近更新 更多