【问题标题】:Sending JWT Token in the body of response Java Spring在响应 Java Spring 的正文中发送 JWT 令牌
【发布时间】:2019-01-30 15:32:02
【问题描述】:

我在Header中发送JWT的token, 但客户需要它在响应的正文中, 我怎样才能把它放在响应中:

@覆盖 受保护的无效成功认证( HttpServletRequest 请求、HttpServletResponse 响应、FilterChain 链、Authentication authResult) 抛出 IOException,ServletException { User springUser = (User) authResult.getPrincipal(); String jwt = Jwts.builder() .setSubject(springUser.getUsername()) .setExpiration(new Date(System.currentTimeMillis()+SecurityConstants.EXPIRATION_TIME)) .signWith(SignatureAlgorithm.HS256, SecurityConstants.SECRET) .claim("roles",springUser.getAuthorities()) .compact(); response.addHeader(SecurityConstants.HEADER_STRING, SecurityConstants.TOKEN_PREFIX+jwt); }

我需要将令牌放入响应中

【问题讨论】:

    标签: java spring-boot spring-security jwt


    【解决方案1】:

    如果我理解正确,您只需要创建一个响应正文

    response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(
                "{\"" + SecurityConstants.HEADER_STRING + "\":\"" + SecurityConstants.TOKEN_PREFIX+jwt + "\"}"
        );
    

    看看How do you return a JSON object from a Java Servlet

    【讨论】:

    • 这个回复让我大开眼界......经过长时间的研究但没有成功,我终于找到了这个解决方案,它对我来说效果很好。我需要在我的响应正文中返回令牌,这就是我所做的(遵循这个解决方案)。非常感谢
    • 你好,我在我的网络应用程序中做同样的事情。我只想知道在响应正文上共享令牌是否安全?
    猜你喜欢
    • 2016-08-03
    • 2018-09-06
    • 2018-06-08
    • 2021-03-05
    • 2018-08-03
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2014-09-02
    相关资源
    最近更新 更多