Encryption in JSP/JSTL

I want to encrypt my value of href tag using encryptionutils i.e.

<b><a href="${urlBase}?currentPath=${document.path}"><div class="directory"></div></a></b>

Here in GET method i need to encrypt currentPath that is ${document.path} with spring encryptionutils.

There is another ways but I need to done it with encryptionutils.

Answers


Encrypt currentPath in the controller and then add it as a model attribute.

model.addAttribute("currentPath", encodedPath);

Now you can use the currentPath attribute in your JSP without changes.

<a href="${urlBase}?currentPath=${currentPath}"></a>

When your next call goes to the controller with currentPath as a request parameter then decode currentPath in your controller before use.


Write a custom EL function to encrypt your url. Here are the basic steps:

  1. Create mytagfunctions.tld under WEB-INF:

    <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    
    <tlib-version>2.0</tlib-version>
    <uri>/WEB-INF/mytagfunctions.tld</uri>
    <function>
        <name>encrypt</name>
        <function-class>example.Encrypt</function-class>
        <function-signature>java.lang.String encrypt(java.lang.String)</function-signature>
    </function>
    </taglib>
    

Create Encrypt class in example package:

package example;

public class Encrypt {

    public static String encrypt(java.lang.String value) {
        String encrypted;
        //do your encryption here
        return encrypted;
    }

}

Include your tagfunctions in your JSP and start using it:

<%@ page language="java" pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>

<%@ taglib prefix="string" uri="/WEB-INF/myfunctions.tld"%>

<b><a href="${urlBase}?currentPath=${string:encrypt(document.path)}"><div class="directory"></div></a></b>

Need Your Help

WPF bar chart in DataGrid

wpf mvvm datagrid bar-chart

I am looking for a WPF MVVM friendly way of achieving this.

How to pass the string containing apostrophe to query in android?

android

I am working on android application. Actually in my app I am getting the data from sqlite database which I stored previously. But one of my db column contains strings with Apostrophes. When I pass ...