How to get the user in the @RestController?

时间:2018-04-18 17:50:34

标签: spring rest spring-boot spring-security jwt

I am creating a jwt based authentication. That is working as a charm. I have created the sign up and login methods in the @restcontroller via the post.

Now, i need to create the update user method. For updating the informations of the logged users itself.

Before JWT we used to take the id of the logged user from the session. How to do this with JWT?

@PostMapping("/user/profile")
public ResponseEntity<?> saveProfile(@Valid @RequestBody UserProfileDTO userProfile) {

    /*  Of course the userPofileForm does not contain a hidden field 
        with the ID of the user because it woud allow the user to mofify 
         it and update another user.
    */

    return null;
}

2 个答案:

答案 0 :(得分:0)

尝试将user id添加到jwt的正文中,并接受每个经过身份验证的请求的标头中的jwt标记。

  1. 一旦你的控制器能够获得jwt令牌,解密它并获得用户ID。
  2. 在验证jwt令牌时,将身份验证添加到SecurityContextHolder并获取用户身份验证应用程序中的任何位置

答案 1 :(得分:0)

这是一个如何解决这个问题的例子:

<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
        <meta name="viewport" content="width=device-width,user-scalable=no"/>
        <style>
        body {
            margin: 0px;
            padding: 0px;
            display: flex;
            flex-direction: column;
            overflow: hidden;
            height: 100%;
        }

        #root {
            height: 100%;
            max-height: 100%;
            display: flex;
            flex-direction: column;
            border: red 2px solid;
            flex-grow: 1;
        }

        #content {
            display: flex;
            flex-direction: column;
            flex-grow: 1;
            border: blue 2px solid;
        }

        #title {
            font-weight: bold;
            padding: 10px;
            background-color: lightgrey;
        }

        #list {
            display: flex;
            flex-direction: column;
            flex-grow: 1;
            border: orange 2px solid;
            overflow-y: auto;
        }

        .item {
            padding:15px;
            border-bottom: grey thin solid;
        }
    </style>
    </head>
<body>
    <div id="root">
        <div id="content">
            <div id="title">TITRE DE LISTE</div>
            <div id="list">
                <div class="item">
                    Item 1
                </div>
                <div class="item">
                    Item 2
                </div>
                <div class="item">
                    Item 3
                </div>
                <div class="item">
                    Item 4
                </div>
                <div class="item">
                    Item 5
                </div>
                <div class="item">
                    Item 6
                </div>
                <div class="item">
                    Item 7
                </div>
                <div class="item">
                    Item 8
                </div>
            </div>
        </div>
    </div>
</body>
</html>