Blame view

src/main/java/com/xkl/controller/uspih/AdminAccountController.java 5.51 KB
zhaoyue authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package com.xkl.controller.uspih;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiImplicitParam;
import com.wordnik.swagger.annotations.ApiImplicitParams;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xkl.authorization.annotation.Authorization;
import com.xkl.authorization.annotation.CurrentAdmin;
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.XklAdminEntity;
import com.xkl.model.ResultModel;
import com.xkl.repository.AMPMachineRepository;
import com.xkl.repository.AdminRepository;
import com.xkl.security.AntiXSS;
import com.xkl.security.SecurityTool;
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;

/**
 * USPIH软件登录及退出接口
 * 获取和删除token的请求地址,在Restful设计中其实就对应着登录和退出登录的资源映射
 */
@RestController
@Api("USPIH admin account login and logout")
@RequestMapping("/uspih/account")
public class AdminAccountController {

    @Autowired
    private AdminRepository adminRepository;

    @Autowired
    private ITokenManager tokenManager;

    public static final String USPIH_TOKEN_PREFIX = "USPIHTOKEN";


    @AntiXSS
    @RequestMapping(method = RequestMethod.POST)
    @ApiOperation(value = "USPIH Login", notes = "login")
    public ResponseEntity<ResultModel> login(@RequestParam String account, @RequestParam String password) {
        Assert.notNull(account, "account can not be empty");
        Assert.notNull(password, "password can not be empty");


        XklAdminEntity admin = adminRepository.findByAccountAndStatus(account, Constants.STATUS_OK);
        //未注册
        if (admin == null) {
            //提示用户名或密码错误
zhaoyue authored
58
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
zhaoyue authored
59 60
        }
        String salt = admin.getSalt();
zhaoyue authored
61
        String adminType = Integer.toString(admin.getType());
zhaoyue authored
62 63 64
        String str = account + password + adminType + salt; // 构建待加密字符串
        String calcuPass = SecurityTool.encode(SecurityTool.ALGORITHM_MD5, str);
zhaoyue authored
65
        String pass_in_db = admin.getPwd();
zhaoyue authored
66 67

//        String calcuPass = SecurityTool.getPassword(account, password, salt);
zhaoyue authored
68 69 70
        if (!calcuPass.equals(pass_in_db) ||//密码错误
                admin.getStatus() != 1) {//用户无效
            //提示用户名或密码错误
zhaoyue authored
71
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
zhaoyue authored
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        }

        //生成一个token,保存用户登录状态
        TokenModel model = tokenManager.createToken(USPIH_TOKEN_PREFIX + admin.getId());
        return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    @Authorization
    @ApiOperation(value = "USPIH logout")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "authorization", value = "Input id and login token: userId_tokens", required = true, dataType = "string", paramType = "header"),
    })
    public ResponseEntity<ResultModel> logout(@CurrentAdmin XklAdminEntity admin) {
        tokenManager.deleteToken(USPIH_TOKEN_PREFIX + admin.getId());
        return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
    }
zhaoyue authored
89 90 91 92


    @AntiXSS
    @RequestMapping(method = RequestMethod.PUT)
zhaoyue authored
93
    @ApiOperation(value = "USPIH Login and modify password")
zhaoyue authored
94 95 96 97 98 99 100 101 102 103
    public ResponseEntity<ResultModel> loginModPwd(@RequestParam String account, @RequestParam String password, @RequestParam String newpwd) {
        Assert.notNull(account, "account can not be empty");
        Assert.notNull(password, "password can not be empty");

        XklAdminEntity admin = adminRepository.findByAccountAndStatus(account, Constants.STATUS_OK);
        //未注册
        if (admin == null) {
            //提示用户名或密码错误
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
        }
zhaoyue authored
104
zhaoyue authored
105
        String salt = admin.getSalt();
zhaoyue authored
106 107 108 109
        String adminType = Integer.toString(admin.getType());
        String str = account + password + adminType + salt; // 构建待加密字符串
        String calcuPass = SecurityTool.encode(SecurityTool.ALGORITHM_MD5, str);
zhaoyue authored
110 111 112 113 114 115 116
        String pass_in_db = admin.getPwd();
        if (!calcuPass.equals(pass_in_db) ||//密码错误
                admin.getStatus() != 1) {//用户无效
            //提示用户名或密码错误
            return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.OK);
        }
        salt = SecurityTool.genSalt();
zhaoyue authored
117 118
        str = account + newpwd + adminType + salt; // 构建待加密字符串
        String pass2Db = SecurityTool.encode(SecurityTool.ALGORITHM_MD5, str);
zhaoyue authored
119 120 121 122 123 124 125 126
        admin.setPwd(pass2Db);
        admin.setSalt(salt);
        adminRepository.save(admin);
        //生成一个token,保存用户登录状态
        TokenModel model = tokenManager.createToken(USPIH_TOKEN_PREFIX + admin.getId());
        return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
    }
zhaoyue authored
127
}