2

我在露天执行 wbesciptr 时遇到问题。我不知道发生了什么,但是每次我运行用 java 编写的 webscript 时,我都会收到 401 错误代码。我在 javascript 中有类似的 webscript,它运行正常。

地址: http://localhost:8080/proxy/alfresco/api/repository/person/my-captcha?type=validate&token=MILUR1445505693188&word=EWGVNB

错误日志:

2015-10-23 09:32:35,030 信息 [web.site.EditionInterceptor] [http-bio-8080-exec-7] 无法从 Alfresco 检索许可证信息:401

我在xml中的bean:

<bean id="webscript.pl.[...].ca.guest.my-captcha.get"
    class="pl.[...].repo.web.scripts.ca.MyCaptcha" 
    parent="webscript">
</bean>

xml 中的属性:

<webscript>
    <shortname>My captcha</shortname>
    <description>Get captcha</description>
    <url>/api/ca/guest/my-captcha</url>
    <format default="json"/>
    <family>[...]</family>
    <authentication runas="admin">none</authentication>
</webscript>

超光速:

{
    "token": <#if token?exists>"${token}"<#else>null</#if>,
    "response": <#if response?exists>"${response}"<#else>null</#if>,
    "image": <#if image?exists>"${image}"<#else>null</#if>
}

和 JAVA 类:

public class MyCaptcha extends DeclarativeWebScript {

    private static final Logger LOGGER = Logger.getLogger(MyCaptcha.class);
    private static final String CAPTCHA_VALIDATE = "validate";
    private static final String CAPTCHA_CREATE = "create";
    private static final String PARAMETER_TYPE = "type";
    private static final String PARAMETER_TOKEN = "token";
    private static final String PARAMETER_WORD = "word";
    private static final String PARAMETER_IMAGE = "image";
    private static final String PARAMETER_RESPONSE = "response";
    private static Map<String, String> TOKENCACHE = new HashMap<>();

    @Override
    protected Map<String, Object> executeImpl(WebScriptRequest req,
            Status status, Cache cache) {
        Map<String, Object> result;
        String request = req.getParameter(PARAMETER_TYPE);
        if (request != null && request.equals(CAPTCHA_CREATE)) {
            result = createCaptcha();
            return result;
        } else if (request != null && request.equals(CAPTCHA_VALIDATE)) {
            String token = req.getParameter(PARAMETER_TOKEN);
            String word = req.getParameter(PARAMETER_WORD);
            result = new HashMap<>();
            result.put(PARAMETER_RESPONSE, token != null && word != null
                    && TOKENCACHE.get(token).equalsIgnoreCase(word)
                    ? "true"
                    : "false");
            return result;
        } else {
            result = new HashMap<>();
            result.put(PARAMETER_RESPONSE, "false");
            return result;
        }
    }

    public Map<String, Object> createCaptcha() {
        int width = 150;
        int height = 50;
        BufferedImage bufferedImage = new BufferedImage(150, 50,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = bufferedImage.createGraphics();
        g2d.setFont(new Font("Georgia", Font.BOLD, 18));

        RenderingHints renderingHints = new RenderingHints(
                RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        renderingHints.put(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        g2d.setRenderingHints(renderingHints);

        // drawing gradient
        GradientPaint gp = new GradientPaint(0, 0, Color.lightGray, 0,
                height / 1, Color.white, true);

        g2d.setPaint(gp);
        g2d.fillRect(0, 0, width, height);

        // setting font's color
        g2d.setColor(new Color(0, 0, 0));

        // creating chain of random letters
        char captchaWord[] = new char[6];
        String captcha = "";
        Random r = new Random();
        for (int i = 0; i < 6; i++) {
            captchaWord[i] = (char) (65 + Math.abs(r.nextInt()) % 24);
            captcha += captchaWord[i];
        }

        // drawing chain of random letters on image
        int x = 0;
        for (int i = 0; i < captchaWord.length; i++) {
            x += 10 + (Math.abs(r.nextInt()) % 15);
            g2d.drawChars(captchaWord, i, 1, x + 5,
                    20 + Math.abs(r.nextInt()) % 20);
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // OutputStream b64 = new Base64.OutputStream(baos);
        Map<String, Object> result = new HashMap<>();
        try {
            ImageIO.write(bufferedImage, "png", baos);
            byte[] bytes = baos.toByteArray();
            String base64bytes = Base64.encodeBytes(bytes);
            String src = "data:image/png;base64," + base64bytes;
            String token = createToken(captcha);
            result.put(PARAMETER_IMAGE, src); // b64.toString("UTF-8"));
            result.put(PARAMETER_TOKEN, token);
            TOKENCACHE.put(token, captcha);
            return result;
        } catch (IOException e) {
            LOGGER.error("Exception : can't write image into base64 ::", e);
            // ? e.printStackTrace();
            result.put("error", "can't write image into base64");
            return result;
        }
    }

    // this function creates unique (hope so) token to recognize captcha
    public String createToken(String captcha) {
        StringBuffer token = new StringBuffer();
        Date date = new Date();
        Random r = new Random();
        token.append((char) (65 + Math.abs(r.nextInt()) % 24))
                .append((char) (65 + Math.abs(r.nextInt()) % 24))
                .append((char) (65 + Math.abs(r.nextInt()) % 24))
                .append((char) (65 + Math.abs(r.nextInt()) % 24))
                .append((char) (65 + Math.abs(r.nextInt()) % 24))
                .append(date.getTime());
        return token.toString();
    }
}
4

1 回答 1

1

我解决了它(幸运的是)。地址有问题。我正在使用代理调用我的 webscript,而不是直接连接到一个/service/.../my-captcha

于 2015-10-23T07:43:13.967 回答