程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring security 5 测试 @WebMvcTest 测试不断给出 0302 响应大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring security 5 测试 @WebMvcTest 测试不断给出 0302 响应?

开发过程中遇到Spring security 5 测试 @WebMvcTest 测试不断给出 0302 响应的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring security 5 测试 @WebMvcTest 测试不断给出 0302 响应的解决方法建议,希望对你解决Spring security 5 测试 @WebMvcTest 测试不断给出 0302 响应有所启发或帮助;

我正在编写一个示例 spring security 5 程序。我使用 AWS cognito 作为 oauth2 提供商。我使用的流程是授权授予。 当我在本地运行时,一切正常。我的其余端点得到了适当的保护,当我输入凭据时,我可以访问这些端点。

现在我想使用切片测试来测试这个控制器。

我为此编写了一个 @WebMvcTest 测试,但是,我不断收到 302 响应。在日志中,我看到请求被重定向到 cognito。我的理解是在测试期间不会将请求发送到 AWS。 请有人解释为什么一定会发生这种情况或我哪里出错了。

BirdController.java

@RestController
@requestMapPing(path = "/")
public class BirdController {

    @autowired
    Birdservice service;

    @GetMapPing(produces = MediaType.APPliCATION_JsON_value)
    List<Bird> getBirds(){
        return service.getAllBirds();
    }

WebSecurityConfiguration.class

@Configuration
    public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        private final String clIEntID;
        private final String logoutUrl;
    
        public WebSecurityConfiguration(@Value("${spring.security.oauth2.clIEnt.registration.cognito.clIEntID}") String clIEntID,@Value("${Cognito.logoutUrl}") String logoutUrl) {
            this.CLIENtID = clIEntID;
            this.logoutUrl = logoutUrl;
        }
    
        @OverrIDe
        protected voID configure(httpSecurity http) throws Exception {
            http
                    .csrf()
                    .and()
                    .authorizerequests(authorize ->
                            authorize.mvcMatchers("/").permitAll()
                                    .anyrequest().authenticated())
                    .oauth2Login()
                    .and()
                    .logout();
                    
        }
    }

BirdControllerTest.java

@RunWith(springrunner.class)
@WebMvcTest(controllers = BirdController.class)
public class BirdControllerTest {

    @autowired
    private mockR_793_11845@vc mockR_793_11845@vc;

    @mockBean
    private Birdservice service;

    @Test
    @WithAnonymousUser
    public voID testGetAllBirds() throws Exception {
        this.mockR_793_11845@vc
                .perform(get("/"))
                .andExpect(status().isUnauthorized());
    }

我希望测试通过,但用户未通过身份验证,但我收到了 302 响应。

日志片段

mockhttpServletrequest:
      http Method = GET
      request URI = /
       Parameters = {}
          headers = []
             Body = <no character enCoding set>
    Session Attrs = {SPRING_Security_SAVED_requEST=DefaultSavedrequest[http://localhost/]}

Handler:
             Type = null

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndVIEw:
        VIEw name = null
             VIEw = null
            Model = null

FlashMap:
       Attributes = null

mockhttpServletResponse:
           Status = 302
    Error message = null
          headers = [X-Content-Type-Options:"nosniff",X-XSS-Protection:"1; mode=block",Cache-Control:"no-cache,no-store,max-age=0,must-revalIDate",Pragma:"no-cache",Expires:"0",x-frame-options:"DENY",LOCATIOn:"http://localhost/oauth2/authorization/cognito"]
     Content type = null
             Body = 
    ForWARDed URL = null
   Redirected URL = http://localhost/oauth2/authorization/cognito
          cookies = []

java.lang.AssertionError: Status 
Expected :401
Actual   :302
 <Click to see difference>
    at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
    at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
    at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:627)
    at org.springframework.test.web.servlet.mockR_793_11845@vc$1.andExpect(mockR_793_11845@vc.java:196)

AWS Cognito 配置

spring:
    security:
        oauth2:
          clIEnt:
            registration:
              cognito:
                clIEntID: XXXX
                clIEntSecret: XXXXX
                scope: openID
                redirectUriTemplate: http://localhost:8080/login/oauth2/code/cognito
                clIEntname: XXXX
            provIDer:
              cognito:
                issuerUri: XXXX
                user-name-attribute: cognito:username

pom.xml

 <parent>
        <groupID>org.springframework.boot</groupID>
        <artifactID>spring-boot-starter-parent</artifactID>
        <version>2.3.0.RELEASE</version>
        <relativePath/>
    </parent>
 
        <dependency>
            <groupID>org.springframework.boot</groupID>
            <artifactID>spring-boot-starter-test</artifactID>
            <exclusions>
                <exclusion>
                    <groupID>org.junit.vintage</groupID>
                    <artifactID>junit-vintage-ENGIne</artifactID>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupID>org.springframework.security</groupID>
            <artifactID>spring-security-test</artifactID>
            <scope>test</scope>
        </dependency>

我已经花了 3 天的时间把头发撕成碎片。已经查过其他问题但没有帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的Spring security 5 测试 @WebMvcTest 测试不断给出 0302 响应全部内容,希望文章能够帮你解决Spring security 5 测试 @WebMvcTest 测试不断给出 0302 响应所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。