登录成功之后,响应给前台的数据“未能自动”清除登录密码

来源:8-1 课程总结

jiiiiiin

2018-10-15

请问老师,使用browser模式登录完成之后 为何前端响应的json数据中还存在密码呢?

我的成功处理器注册的组件:

@Slf4j
public class BrowserAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

    @Autowired
    ObjectMapper objectMapper;

    /**
     * 登录成功之后被调用
     * <p>
     *
     * @param request
     * @param response
     * @param authentication 封装了登录用户的认证信息(发起认证的UserDetails、ip...)
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.debug("身份认证(登录)成功");
        final Device currentDevice = HttpUtils.resolveDevice(request);
        // 根据渠道返回不同的响应数据
        // 还有一种做法是根据客户端程序配置来指定响应数据格式:https://coding.imooc.com/lesson/134.html#mid=6866
        if (!currentDevice.isNormal()) {
            respJson(response, authentication);
        } else {
            // 默认是做重定向到登录之前的【期望访问资源】接口
            super.onAuthenticationSuccess(request, response, authentication);
        }
    }

    protected void respJson(HttpServletResponse response, Authentication authentication) throws IOException {
        response.setContentType(CommonConstants.CONTENT_TYPE_JSON);
        // 将authentication转换成json str输出
        response.getWriter().write(objectMapper.writeValueAsString(authentication));
    }
}

我期望的应该是按课上说的是框架会自动吧密码而设置为空的:

* authentication内容:
     * <p>
     * {
     * // 用户具有的权限
     * "authorities": [
     * {
     * "authority": "admin"
     * }
     * ],
     * // 包含认证请求的信息
     * "details": {
     * // 发送请求的客户端ip
     * "remoteAddress": "0:0:0:0:0:0:0:1",
     * // sessionId
     * "sessionId": "3020A8B92661126C8D63E8C92242EA00"
     * },
     * // 标识用户信息是否已经通过了身份认证
     * "authenticated": true,
     * // 该对象就是`UserDetailsService`返回的`UserDetails`具有的内容,不同的实现将会返回不同的数据,下面是默认的用户名密码登录的`UserDetails`
     * "principal": {
     * "password": null,
     * "username": "admin",
     * "authorities": [
     * {
     * "authority": "admin"
     * }
     * ],
     * "accountNonExpired": true,
     * "accountNonLocked": true,
     * "credentialsNonExpired": true,
     * "enabled": true
     * },
     * // 用户录入的密码(默认不会返回到前台)
     * "credentials": null,
     * // 用户名
     * "name": "admin"
     * }

但是现在前端得到的结果还是如下:

期望老师指点一下,问题可能出在哪里?

写回答

1回答

JoJo

2018-10-22

这是你自己返回的,不是框架返回的啊,所以你要自己处理。而且返回的也是密文,应该不影响吧。

0
2
jiiiiiin
某些应用会受到比如银监的代码审核,返回密码这个是过不了审核的哇
2018-10-22
共2条回复

Spring Security技术栈开发企业级认证与授权

Spring Security技术栈,REST风格开发常见接口,独立开发认证授权模块保证REST服务安全

2662 学习 · 1561 问题

查看课程