TokenController.java 5.47 KB
package com.xkl.controller;

import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.*;
import com.xkl.authorization.manager.ITokenManager;
import com.xkl.authorization.model.TokenModel;
import com.xkl.config.Constants;
import com.xkl.config.ResultStatus;
import com.xkl.domain.User;
import com.xkl.domain.XklAdminEntity;
import com.xkl.model.ResultModel;
import com.xkl.security.AntiXSS;
import com.xkl.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取和删除token的请求地址,在Restful设计中其实就对应着登录和退出登录的资源映射
 */
@RestController
@RequestMapping("/token")
public class TokenController {
    @Autowired
    private ILoginService loginService;
    @Autowired
    private ITokenManager tokenManager;

    @LogAnnotation
    @AntiXSS
    @Sign
    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "用户登录接口")
    public ResponseEntity<ResultModel> login(HttpServletRequest request, @RequestParam String username, @RequestParam String password,
                                             @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);

        Assert.notNull(username, "username can not be empty");
        Assert.notNull(password, "password can not be empty");

        User user = loginService.check(username, password);
        if (user == null) {//用户,密码错误
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
        }
        //生成一个token,保存用户登录状态
        TokenModel model = tokenManager.createToken(String.valueOf(user.getId()));
        model.setUsername(user.getLoginAccount());
        return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(method = RequestMethod.DELETE)
    @ApiOperation(value = "退出登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> logout(HttpServletRequest request, @CurrentUser User user,
                                              @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);

        tokenManager.deleteToken(String.valueOf(user.getId()));
        return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_LOGOUT), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Sign
    @RequestMapping(value = "/admin", method = RequestMethod.POST)
    @ApiOperation(value = "管理员登录接口")
    public ResponseEntity<ResultModel> adminLogin(HttpServletRequest request, @RequestParam String username, @RequestParam String password,
                                             @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);

        Assert.notNull(username, "username can not be empty");
        Assert.notNull(password, "password can not be empty");

        XklAdminEntity admin = loginService.checkAdmin(username, password);
        if (admin == null) {//用户,密码错误
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
        }
        //生成一个token,保存用户登录状态
        TokenModel model = tokenManager.createToken(Constants.ADMIN_TOKEN_PREFIX + String.valueOf(admin.getId()));
        return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
    }

    @LogAnnotation
    @AntiXSS
    @Authorization
    @Sign
    @RequestMapping(value = "/admin",method = RequestMethod.DELETE)
    @ApiOperation(value = "Admin logout")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> logout(HttpServletRequest request, @CurrentAdmin XklAdminEntity admin,
                                              @RequestParam String sign, @RequestParam long t, @RequestParam int type) {
        if (!(boolean) request.getAttribute("signAspect"))
            return new ResponseEntity<>(ResultModel.error(ResultStatus.SIGN_ERROR), HttpStatus.OK);
        tokenManager.deleteToken(Constants.ADMIN_TOKEN_PREFIX + admin.getId());
        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
    }

}