PublicController.java 2.72 KB
Newer Older
liqin's avatar
liqin committed
1 2 3 4 5 6 7 8 9 10
package cn.wisenergy.chnmuseum.party.web.controller;

import cn.wisenergy.chnmuseum.party.web.controller.base.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
liqin's avatar
liqin committed
11 12 13
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
liqin's avatar
liqin committed
14 15 16 17 18

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
liqin's avatar
liqin committed
19
import java.nio.charset.StandardCharsets;
liqin's avatar
liqin committed
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
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 公共方法接口
 * </p>
 *
 * @author 杨建龙
 * @since 2018-10-11
 */
@Api(tags = "公共方法接口")
@RestController
@RequestMapping("/public")
public class PublicController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(PublicController.class);

    /**
     * 解密
     *
     * @param src byte[]
     * @param key String
     * @return byte[]
     * @throws Exception
     */
    private static byte[] decrypt(byte[] src, String key) throws Exception {
        // DES算法要求有一个可信任的随机数源
        SecureRandom random = new SecureRandom();
        // 创建一个DESKeySpec对象
liqin's avatar
liqin committed
51
        DESKeySpec desKey = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
liqin's avatar
liqin committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        // 创建一个密匙工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 将DESKeySpec对象转换成SecretKey对象
        SecretKey securekey = keyFactory.generateSecret(desKey);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance("DES");
        // 用密匙初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, random);
        // 真正开始解密操作
        return cipher.doFinal(src);
    }

    /**
     * 验证token
     *
     * @return
     */
    @ApiOperation(value = "验证token")
    @GetMapping(value = "/authToken")
    @RequiresPermissions("/public/authToken")
    public ResponseEntity<Map<Object, String>> authToken() {
        try {
            Map<Object, String> map = new HashMap<>();
            map.put("status", "200");
            map.put("message", "验证成功");
            return ResponseEntity.status(HttpStatus.OK).body(map);
        } catch (Exception e) {
            logger.error("服务器错误!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

}