当前位置: 首页 > 知识库问答 >
问题:

Spring2安全:403禁止[重复]

长孙雅志
2023-03-14

我有一个以前使用 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>

测试仍然成功。测试基本上是去一个项目控制器'Home计数器',然后从那里去一个服务,并使用一个雷斯特模板来执行各种操作。实际上,这是一个不同的应用程序,如果我从头开始编写它,我可能会做一个wiremck,但现在这个问题的目的是在测试包中有一个控制器模拟所需的行为。

通过添加这个简单的类,我已经越过了本地控制器上的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();
}

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

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

java prettyprint-override">@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 禁止。

测试类是这样注释的

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

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

有了DefaultSecurityConfiguration类,这些调用就不会出现问题,失败的是应用程序代码中使用restTemplate的后续调用

有什么明显的吗?

共有1个答案

梁浩
2023-03-14

取代:

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

}

通过这个:

java prettyprint-override">@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests().antMatchers("/otherpath/**")
        .authenticated()
        .and()
        .httpBasic();
    http.csrf().disable();
}
 类似资料:
  • 我正在尝试使用Spring security保护我的网站,但我一直收到

  • 我在Spring实现了WebSocket。一切正常,但最近我决定实施SpringSecurity。 我的MessageBroker看起来像: 我的JS客户看起来像这样: Spring Security配置: 通过我的JS客户端订阅后,我收到: 所以我决定在安全配置中添加此代码: 但在那之后我收到这种错误: 我不知道如何定义这样的bean,所以我创建了以下类: 但在编译期间,我收到这种错误: 我真的

  • 我想暂时禁用整个应用程序的Spring Security性,但我总是被403禁止 删除控制器中的@PreAuthorize注释不会给出任何结果。未使用此注释标记的endpoint也会丢弃我 403 禁止 我不需要基本身份验证 我不需要身份验证 我的Spring Security配置:(/,/api/**,/**,**不工作,我总是得到403禁止) 我的一个控制者:

  • 我有我的spring boot应用程序,我正在尝试添加Spring Security性,但当我通过postman发出请求时,我不断收到一个403 Forbbiden,联机时我发现我应该在我的配置中添加:“.csrf().disable()”,但它不起作用(如果我在permitAll()中放置路径为:“person/**”的方法,则所有操作都有效) 这是我的代码: 我的用户控制器: My perso

  • 我已经将我的一个api配置为受保护,当我试图访问它时,它给我一个拒绝访问的错误消息,我不知道是什么原因。请注意,我正在传递有效的访问令牌。 我的场景:基本上我已经在授权服务器中创建了logout rest api,我希望允许带有有效令牌的请求访问这个api。 请求: 回应: 我发现下面的方法返回false,因此它引发了访问拒绝错误。 下面是我通过框架调试捕获的屏幕截图。还请检查评论中提到的图像。

  • 以下代码禁止使用403,尽管“https://jsonplaceholder.typicode.com/posts/1”在postman中有效 错误: 如果有人能建议,我需要在代码中添加什么