/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.security.api.filter;

import com.yami.shop.security.api.model.YamiUser;
import com.yami.shop.security.comment.enums.App;
import com.yami.shop.security.comment.filter.YamiAuthenticationProcessingFilter;
import com.yami.shop.security.comment.token.AuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义登陆filter，直接返回一个用户的token，用于进行测试
 * 用于测试登录的token
 *       post:http://127.0.0.1:8086/debugToken
 * @author LGH
 */
@Component
public class DebuggerAuthenticationFilter extends YamiAuthenticationProcessingFilter {

    public DebuggerAuthenticationFilter() {
        super("/debugToken");
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {

        AuthenticationToken authenticationToken = new AuthenticationToken();

        YamiUser loadedUser = new YamiUser();
        loadedUser.setName("测试用户");
        loadedUser.setAppType(App.MINI.value());
        loadedUser.setUserId("1");
        loadedUser.setBizUserId("debuggerOpenId1");
        loadedUser.setEnabled(true);
        return handleAuthenticationToken(authenticationToken,loadedUser);
    }


}
