Skip to content

Is it possible to get Authentication as json response?

currently it returns String but I’d like to send response as json to client. it api check the jwt’s authentication. it appears email,nickname etc as string now.

 @GetMapping("/api/user/checkJWT") //    
 @ApiImplicitParam(name = "Authorization", paramType = "header", required = true)
    public String list() {

        Authentication user = SecurityContextHolder.getContext().getAuthentication();

        User user2 = (User) user.getPrincipal();
        return user.getAuthorities().toString() + " / " + user2.getEmail() + " / " + user2.getNickname() +  " / " + user2.getEmailVerification().getVerified();
    }

Answer

Simply use org.json.simple.JSONObject create a jsonobject and put everything inside it as key value pair and return it to client as json.

        JSONObject obj = new JSONObject();
        obj.put(key, value);
         .......// more values to add here
        return obj.toJSONString();