有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring MVC应用程序过滤URL中的HTML这是安全问题吗?

我现有的SpringWebMVC应用程序在控制器中具有以下处理程序映射

    @RequestMapping(method = RequestMethod.GET, value = "/welcome")

我触发下面的请求http://www.example.com/welcome,这可以正常工作

问题是

http://www.example.com/welcome.check.blah 

同样有效

此外,尽管未通过授权,但将重新显示带有脚本标记的应用程序的HTTP GET请求URL

示例http://www.example.com/welcome<script>alert("hi")</script>在浏览器窗口中重新显示,由于我的授权逻辑,显示“未授权”消息

我想知道这是否是一个安全问题,我是否需要在代码中进行任何编码/过滤


共 (5) 个答案

  1. # 1 楼答案

    您也可以在web中对此进行限制。通过提及url模式来创建xml。您可以在web中提及“/.htm”,而不是给出“/”。xml

    差不多

    <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/application/*.htm</url-pattern>
        </servlet-mapping>
    
  2. # 2 楼答案

    在当前的Spring Java配置中,有一种更简单的方法来配置相同的东西:

    @Configuration
    public class DispatcherConfig extends WebMvcConfigurationSupport {
    
        @Override
        protected void configurePathMatch(PathMatchConfigurer configurer) {
            configurer.setUseSuffixPatternMatch(false);
        }
    
    }
    
  3. # 4 楼答案

    当您使用Spring请求该类型的映射(即“/anywhere”)时,Spring实际上会将您的控制器映射到多个URL:

    /欢迎
    /欢迎光临*
    /欢迎光临/

    为了防止这种情况,请在请求映射(即/welcome.htm)时更加具体,或者在Xml配置中将URL手动映射到控制器:

    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/welcome">YourControllerBean</prop>
                </props>
            </property>
    </bean>
    


    干杯,皮特

  4. # 5 楼答案

    这种行为是由于useSuffixPatternMatch选项造成的,在RequestMappingHandlerMapping中默认为true(我假设您使用的是SpringMVC3.1)

    useSuffixPatternMatch : Whether to use suffix pattern match (".*") when matching patterns to requests. If enabled a method mapped to "/users" also matches to "/users.*". The default value is "true".

    要将useSuffixPatternMatch设置为false,最简单的方法是使用@Configuration

    @Configuration
    @EnableWebMvc
    public class Api extends WebMvcConfigurationSupport {
    
        @Override
        public RequestMappingHandlerMapping requestMappingHandlerMapping() {
            RequestMappingHandlerMapping mapping = super.requestMappingHandlerMapping();
            mapping.setUseSuffixPatternMatch(false);
            return mapping;
        }
    
    }