请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

请问老师,app Token模式的登录接口,如何设置支持`Content-Type`为json的Post请求

下面是使用form方式发送请求的正常状态:


<!-- 生产环境版本,优化了尺寸和速度 -->
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<!-- 引入组件库 -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.bootcss.com/qs/6.5.2/qs.min.js"></script>
<script src="https://cdn.bootcss.com/Base64/1.0.1/base64.min.js"></script>
<script src="/js/common.js"></script>

<script>
    new Vue({
        el: '#app',
        data: function () {
            return {
                username: 'admin',
                password: 'a11111',
                mobile: '15388888888',
                show: false,
                sendSmsCodeBtnStatus: false
            }
        },
        computed: {
            rememberVal: function () {
                return this.remember
            }
        },
        methods: {
            onSubmit: function (e) {
                this.$vp.post('/authentication/form', Qs.stringify({
                    username: this.username,
                    password: this.password
                }), {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                        'Authorization': `Basic ${btoa('immoc:immocsecret')}`
                    }
                }).then(function (response) {
                    console.log(`token 授权登录完成->`, response)
                }).catch(function (err) {
                    alert(`请求短信验证码出错${JSON.stringify(err.response)}`)
                })
            },

如果我使用默认的json格式发送请求数据,就会出错:

{
    "content": "Cannot pass null or empty values to constructor"
}

下面是堆栈:


2018-09-12 15:28:05.682  INFO 11519 --- [nio-8080-exec-5] a.c.a.CustomAuthenctiationFailureHandler : 登录失败
2018-09-12 15:30:49.420  INFO 11519 --- [nio-8080-exec-1] c.j.s.w.c.DemoUserDetailsService         : 准备登陆的用户 admin
2018-09-12 15:30:49.572  INFO 11519 --- [nio-8080-exec-1] a.c.a.CustomAuthenticationSuccessHandler : 身份认证(登录 Token)成功
2018-09-12 15:33:46.849  INFO 11519 --- [nio-8080-exec-2] c.j.s.w.c.DemoUserDetailsService         : 准备登陆的用户 
2018-09-12 15:33:46.928 ERROR 11519 --- [nio-8080-exec-2] w.a.UsernamePasswordAuthenticationFilter : An internal error occurred while trying to authenticate the user.

org.springframework.security.authentication.InternalAuthenticationServiceException: Cannot pass null or empty values to constructor
	at org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:126) ~[spring-security-core-4.2.7.RELEASE.jar:4.2.7.RELEASE]
	

正在回答 回答被采纳积分+3

1回答

JoJo 2018-09-20 10:00:12

额,没太看明白,前端不太懂。但是默认表单验证的数据是不能用json发送的,只能用键值对,因为spring的默认实现是按键值对取的。

登录后可查看完整回答登录/注册
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信