Login.java 2.17 KB
package com.huaheng.api;

import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.controller.BaseController;
import com.huaheng.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/api")
@Api(tags = {"Login"}, description = "登陆接口")
public class Login  extends BaseController {

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String username, String password, String warehouse, Boolean rememberMe)
    {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
        {
            return error("用户名和密码不能为空");
        }
        if (StringUtils.isEmpty(warehouse))
        {
            return error("请选择仓库");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return error(msg);
        }
        String[] warehouseArray = warehouse.split(",");
        ShiroUtils.getUser().setWarehouseId(Integer.valueOf(warehouseArray[0]));
        ShiroUtils.getUser().setWarehouseCode(warehouseArray[1]);
        return success();
    }


    @PostMapping("/heartbeat")
    @ApiOperation("心跳接口,用于延长cookie有效期")
    public AjaxResult heartbeat()
    {
        return AjaxResult.success("success");
    }
}