spring oauth2 的测试代码

错误信息:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is error="invalid_request", error_description="Missing grant type"

错误代码配置:

@Autowired
    protected WebApplicationContext wac;

    @Autowired
    protected FilterChainProxy springSecurityFilterChain;

    protected MockMvc mockMvc;

    @Before
    public void prepare1() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac)
                .addFilter(springSecurityFilterChain).build();
    }

    protected void auth(OAuth2Form oAuth2Form) throws Exception {

        OAuth2Token authToken = JsonUtil.readValue(mockMvc.perform(post(SecurityConstants.DEFAULT_OAUTH_TOKEN_URL)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .header("Authorization", AUTHORIZATION)
                .content(oAuth2Form.toString()))
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString(), new TypeReference<OAuth2Token>() {
        });
        assertNotNull(authToken);
        RegistrationMockJunit.authToken = authToken;
    }

错误原因:该代码使用post并且使用json的格式来认证用户,我的应用代码中有一个过滤器。它的作用是读取post的APPLICATION_JSON_UTF8数据转成APPLICATION_FORM_URLENCODED(oauth2不支持APPLICATION_JSON_UTF8)。当使用junit的时候没有进入那个过滤器,因为过滤器是保存在ApplicationFilterChain中的,不是保存在FilterChainProxy,使用junit的时候ApplicationFilterChain将替换成MockFilterChain,所以有以上的错误。

解决办法将代码采用APPLICATION_FORM_URLENCODED格式提交

protected void auth(OAuth2Form oAuth2Form) throws Exception {

        OAuth2Token authToken = JsonUtil.readValue(mockMvc.perform(post(SecurityConstants.DEFAULT_OAUTH_TOKEN_URL)
                .contentType(MediaType.APPLICATION_FORM_URLENCODED)
                .header("Authorization", AUTHORIZATION)
                .params(oAuth2Form.convert()))
                .andExpect(status().isOk())
                .andReturn().getResponse().getContentAsString(), new TypeReference<OAuth2Token>() {
        });
        assertNotNull(authToken);
        RegistrationMockJunit.authToken = authToken;
    }




    @Getter
    public static class OAuth2Form implements Serializable {
        @JsonProperty("grant_type")
        private String grantType;
        private String username;
        private String password;
        private String scope;

        public OAuth2Form(String grantType, String username, String password, String scope) {
            this.grantType = grantType;
            this.username = username;
            this.password = password;
            this.scope = scope;
        }

        public OAuth2Form(String username, String password) {
            this.grantType = "password";
            this.username = username;
            this.password = password;
            this.scope = "all";
        }

        public String toString() {
            try {
                return JsonUtil.serialize(this);
            } catch (IOException e) {
                return "";
            }
        }

        public MultiValueMap<String, String> convert(){
            MultiValueMap<String, String> result = new LinkedMultiValueMap<>();
            result.add("grant_type", this.grantType);
            result.add("username", this.username);
            result.add("password", this.password);
            result.add("scope", this.scope);
            return result;
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值