如何在Spring MVC中对URL中的字段实体进行编码?

时间:2018-01-16 12:10:03

标签: java spring url

我通过Spring MVC制作了一个表单提交应用程序。提交表单后,输入字段的所有值都在URL中可见。

让我们假设提交生成的网址后是:

SpringTuto / successA名称= FirstUser和放大器;名称=学生和放大器;国家= XYZ和放大器; DOB = 2018%2F01%2F16和放大器;技能=滑翔及放大器; address.street_name =大道+路和放大器; address.city =新+城市&安培; address.district =新+区&安培; address.pin_code = 322343

从上面的网址中,我想在一些加密代码中对名称,名称,国家和所有其他参数进行编码。

在阅读了几篇文章之后,我开始理解我将使用URIeditor(org.springframework.beans.propertyeditors.URIEditor)进行编码。但我不知道如何使用它。如果有人有其他方法可以做到这一点,请分享。 提前谢谢。

这是我的控制器类。

@Controller 公共类SpgController {

@ModelAttribute("header")
public Model addHeader(Model view) {
    int a = 10;
    return view.addAttribute(a);

}
@InitBinder
public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd");
    binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor(dateFormat, false));
    binder.registerCustomEditor(String.class, "name" , new NamepropertyEditor());
}



@RequestMapping("/main")
public ModelAndView go() {
    ModelAndView view = new ModelAndView("main");
    return view;
}


@RequestMapping(value = "/successA", method = RequestMethod.GET)
public ModelAndView method(@Valid @ModelAttribute("bean") Bean bean, BindingResult result ) {
    if (result.hasErrors()) {
        ModelAndView view = new ModelAndView("main");
        return view;
    }

    ModelAndView view = new ModelAndView("successAnother");
    return view;

}

}

这是successAnother.jsp页面

     <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form >
        Name: ${bean.name}<br/>
        Designation: ${bean.designation}<br/>
        DoB: ${bean.dob}<br/>
        Skills: ${bean.skills}<br/>
        Street: ${bean.address.street_name}<br/>
        City: ${bean.address.city}<br/>
        District: ${bean.address.district}<br/>
        PinCode: ${bean.address.pin_code}<br/>
        Country: ${bean.country}<br/>
    </form>
</body>
</html>

这是main.jsp(申请表)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
    <%@ taglib uri="http://www.springframework.org/tags" prefix = "spring" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring</title>
</head>
<body>

    <a href = "/SpringTuto/main?language=en">English</a>|<a href = "/SpringTuto/main?language=fr">French</a>|<a href = "/SpringTuto/main?language=hi">Hindi</a>

    <h3>${msg}</h3>
    <form:errors path = "bean.*"/>
    <form action="successA" method="get">
        <table>
            <tr>
                <td><spring:message code = "label.userName"/><input type="text" name="name">
                </td>
            </tr>
            <tr>
                <td><spring:message code = "label.userDesignation"/><input type="text" name="designation">
                </td>
            </tr>
            <tr>
                <td><spring:message code = "label.userCountry"/><input type="text" name="country">
                </td>
            </tr>
            <tr>
                <td>
                    <spring:message code = "label.userDoB"/><input type= "text" name = "dob"  >
                </td>
            </tr>
            <tr>
                <td>
                    <spring:message code = "label.Skils"/><select name = "skills" multiple >
                        <option value= "driving">Driving</option>
                        <option value = "diving">Diving</option>
                        <option value = "swimming">Swimming</option>
                        <option value  = "paragliding">Paragliding</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <h5><spring:message code = "label.userAddress"/></h5>
                        <spring:message code = "label.userStreetname"/><input type = "text" name = "address.street_name">
                        <spring:message code = "label.userCity"/> <input type = "text" name = "address.city">
                        <spring:message code = "label.userDistrict"/><input type = "text" name  = "address.district">
                        <spring:message code = "label.userPin"/><input type  = "text" name = "address.pin_code">
                </td>
            </tr>
            <tr>
                <td>

                </td>
            </tr>
            <tr>
                <td><input type="submit" value=<spring:message code = "label.userSubmit"/>></td>
            </tr>
        </table>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果您的应用程序支持HTTPS,那么可以通过执行以下配置来轻松实现Spring安全性。

<http>
    <intercept-url pattern="/**" access="ROLE_USER" requires-channel="https"/>
...
</http>

将弹簧安全性与spring mvc集成非常简单。