RSA With Swagger


package com.winplan365.othink.rest.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winplan365.othink.rest.dto.CryptoDto;
import com.winplan365.othink.rest.model.response.Response;
import com.winplan365.othink.rest.properties.RSAProperties;
import com.winplan365.othink.rest.tool.RSAUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Api(value = "RSA", description = "RSA加密和解密")
@RequestMapping("/rsa")
@RestController
public class RSAController {

    private RSAProperties rsaProperties;

    public RSAController(RSAProperties rsaProperties) {
        this.rsaProperties = rsaProperties;
    }

    @ApiOperation(value = "Encode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "str", dataType = "String", required = true, value = "str"),
            @ApiImplicitParam(paramType = "query", name = "apptoken", dataType = "String", required = true, value = "token令牌"),
            @ApiImplicitParam(paramType = "query", name = "appkey", dataType = "String", required = true, value = "key")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 400, message = "Token过期"),
            @ApiResponse(code = 401, message = "请求权限不足"),
            @ApiResponse(code = 402, message = "RSA解码错误"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 503, message = "目前无法使用服务器")
    })
    @RequestMapping(value = "/encode", method = RequestMethod.POST)
    public Response encode(@RequestBody String str, @RequestParam String apptoken, @RequestParam String appkey) {

        Response response = new Response();
        try {

            response.setResult("401");

            CryptoDto cryptoDto = new CryptoDto(str, System.currentTimeMillis() + 30000L);

            ObjectMapper objectMapper = new ObjectMapper();

            response.setDesc(RSAUtil.encrypt(objectMapper.writeValueAsString(cryptoDto), rsaProperties.getPublicKeyStr()));
            response.setResult("200");

        } catch (IOException e) {
            e.printStackTrace();
            response.setResult("401");
        } catch (Exception e) {
            e.printStackTrace();
            response.setResult("402");
        }

        return response;

    }

    @ApiOperation(value = "encodeList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "strList", dataType = "List", required = true, value = "要加密的List"),
            @ApiImplicitParam(paramType = "query", name = "apptoken", dataType = "String", required = true, value = "token令牌"),
            @ApiImplicitParam(paramType = "query", name = "appkey", dataType = "String", required = true, value = "key")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 400, message = "Token过期"),
            @ApiResponse(code = 401, message = "请求权限不足"),
            @ApiResponse(code = 402, message = "RSA解码错误"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 503, message = "目前无法使用服务器")
    })
    @RequestMapping(value = "/encodeList", method = RequestMethod.POST)
    public Response encodeList(@RequestBody List<String> strList, @RequestParam String apptoken, @RequestParam String appkey) {

        Response response = new Response();
        try {

            response.setResult("401");

            Collections.sort(strList);

            ObjectMapper objectMapper = new ObjectMapper();

            CryptoDto cryptoDto = new CryptoDto(objectMapper.writeValueAsString(strList), System.currentTimeMillis() + 30000L);

            response.setDesc(RSAUtil.encrypt(objectMapper.writeValueAsString(cryptoDto), rsaProperties.getPublicKeyStr()));
            response.setResult("200");

        } catch (IOException e) {
            e.printStackTrace();
            response.setResult("401");
        } catch (Exception e) {
            e.printStackTrace();
            response.setResult("402");
        }

        return response;

    }

    @ApiOperation(value = "encodeArguments")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "arguments", dataType = "String", required = true, value = "要加密的参数"),
            @ApiImplicitParam(paramType = "query", name = "apptoken", dataType = "String", required = true, value = "token令牌"),
            @ApiImplicitParam(paramType = "query", name = "appkey", dataType = "String", required = true, value = "key")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 400, message = "Token过期"),
            @ApiResponse(code = 401, message = "请求权限不足"),
            @ApiResponse(code = 402, message = "RSA解码错误"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 503, message = "目前无法使用服务器")
    })
    @RequestMapping(value = "/encodeArguments", method = RequestMethod.POST)
    public Response encodeArguments(@RequestBody String arguments, @RequestParam String apptoken, @RequestParam String appkey) {

        Response response = new Response();
        try {

            response.setResult("401");

            String[] strArr = arguments.split("&");
            Arrays.sort(strArr);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < strArr.length; i++) {
                sb.append(strArr[i]);
                if (i != strArr.length - 1) {
                    sb.append("&");
                }
            }

            String sortedStr = sb.toString();

            ObjectMapper objectMapper = new ObjectMapper();

            CryptoDto cryptoDto = new CryptoDto(sortedStr, System.currentTimeMillis() + 30000L);

            response.setDesc(RSAUtil.encrypt(objectMapper.writeValueAsString(cryptoDto), rsaProperties.getPublicKeyStr()));
            response.setResult("200");

        } catch (IOException e) {
            e.printStackTrace();
            response.setResult("401");
        } catch (Exception e) {
            e.printStackTrace();
            response.setResult("402");
        }

        return response;

    }

    //    @ApiIgnore
    @ApiOperation(value = "Decode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "str", dataType = "String", required = true, value = "CrytoDto"),
            @ApiImplicitParam(paramType = "query", name = "apptoken", dataType = "String", required = true, value = "token令牌"),
            @ApiImplicitParam(paramType = "query", name = "appkey", dataType = "String", required = true, value = "key")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "success"),
            @ApiResponse(code = 400, message = "Token过期"),
            @ApiResponse(code = 401, message = "请求权限不足"),
            @ApiResponse(code = 402, message = "RSA解码错误"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 503, message = "目前无法使用服务器")
    })
    @RequestMapping(value = "/decode", method = RequestMethod.POST)
    public Response decode(@RequestBody String str, @RequestParam String apptoken, @RequestParam String appkey) {

        Response response = new Response();
        try {

            response.setResult("401");

            ObjectMapper objectMapper = new ObjectMapper();
            CryptoDto cryptoDto = objectMapper.readValue(RSAUtil.decrypt(str, rsaProperties.getPrivateKeyStr()), CryptoDto.class);
            Long expires = cryptoDto.getExpires();
            if (expires < System.currentTimeMillis()) {
                response.setResult("400");
                return response;
            }

            response.setDesc(cryptoDto.getData());
            response.setResult("200");

        } catch (IOException e) {
            e.printStackTrace();
            response.setResult("401");
        } catch (Exception e) {
            e.printStackTrace();
            response.setResult("402");
        }

        return response;

    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值