HDIV + Spring MVC - 始终获取未经授权的访问错误HDIV_PARAMETER_DOES_NOT_EXIST

时间:2017-06-09 08:15:56

标签: java spring-mvc security servlets hdiv

我正在使用基于Spring Java Configuration的Spring MVC的HDIV,这是我从他们的展示实现中获得的。配置加载,但当我尝试访问受保护的URL,即“/ messages / message”时,我总是得到未经授权的访问错误HDIV_PARAMETER_DOES_NOT_EXIST。当我更改配置以从HDIV处理中排除URL时,它可以工作,但这当然不是一个选项

有人知道我错过了什么吗?

日志文件错误声明

10:04:17.304 [http-nio-8080-exec-22] INFO  org.hdiv.logs.Logger - HDIV_PARAMETER_DOES_NOT_EXIST;/spring-security-example/messages/message;_HDIV_STATE_;;;127.0.0.1;127.0.0.1;megloff;

Java配置

@Configuration
@EnableHdivWebSecurity
public class HdivSecurityConfig extends HdivWebSecurityConfigurerAdapter {

  @Override
  public void configure(SecurityConfigBuilder builder) {
  }

  @Override
  public void addExclusions(ExclusionRegistry registry) {
    registry.addUrlExclusions("/").method("GET");
    registry.addUrlExclusions("/login");
    registry.addUrlExclusions("/logout");
    registry.addUrlExclusions("/static/.*");
    registry.addParamExclusions("_csrf");  


    //  registry.addUrlExclusions("/messages/.*"); <-- would allow access, but not an option      
  }

  @Override
  public void configureEditableValidation(ValidationConfigurer validationConfigurer) {

    validationConfigurer.addValidation("/messages/.*");
    validationConfigurer.addValidation("/addUser");
  }
}

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {
    "ch.megloff.spring.security.example.mvc.controller",
    "ch.megloff.spring.security.example.mvc.action",
    "ch.megloff.spring.security.example.repository",
    "ch.megloff.spring.security.example.listener", 
    "ch.megloff.spring.security.example.service"})
public class SpringMVCConfiguration extends WebMvcConfigurerAdapter {

  @Autowired
  @Qualifier("hdivEditableValidator")
  private Validator hdivEditableValidator;

  @Override
  public Validator getValidator() {
    return hdivEditableValidator;
 }

 ...
}


public class SpringWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

   @Override
   protected Class<?>[] getRootConfigClasses() {
    return new Class[] { SpringMVCConfiguration.class, SpringHibernateConfig.class, SpringSecurityConfiguration.class, SpringSecurityJDBCConfiguration.class, SpringWebFlowConfig.class, HdivSecurityConfig.class };
}

   @Override
   protected Class<?>[] getServletConfigClasses() {
    return new Class[] { SpringMVCConfiguration.class };
   }

   @Override
   protected String[] getServletMappings() {
    return new String[] { "/" };
   }

   public void onStartup(ServletContext container) throws ServletException {
    super.onStartup(container);

    container.addFilter("ValidatorFilter", ValidatorFilter.class).addMappingForUrlPatterns(
            EnumSet.of(DispatcherType.REQUEST), false, "/*");

    container.addListener(new InitListener());
  }
}

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。 HDIV要求所有链接都使用&#34; _HDIV_STATE _&#34;其URL中的参数。为了实现这一点,你必须使用HDIV中的taglib而不是原始的JSTL taglib。

另请参阅有关JSTL的HDIV的reference documentation

e.g。在你的POM中

    <dependency>
        <groupId>org.hdiv</groupId>
        <artifactId>hdiv-jstl-taglibs-1.2</artifactId>
        <version>${org.hdiv-version}</version>
    </dependency>

e.g。在您的JSP中(请注意&tag; www.hdiv.org&#39;在taglib语句中)

<%@ taglib prefix="c" uri="http://www.hdiv.org/jsp/jstl/core"%> <c:url value="/messages/messages" var="url" /> <li><a href="${url}">Messages</a></li>

因此,您需要通过<c:url>实用程序标记呈现网址。然后,这将呈现具有所需HDIV参数的URL,即

localhost:8080/spring-security-example/messages/message?_HDIV_STATE_=26-0-830046F08D66980D1B35F52F2D6677E0

另一种选择可能是使用HDIV中的实用程序类 请参阅hdiv

的github存储库中的类LinkUrlProcessor
LinkUrlProcessor urlProcessor = HDIVUtil.getLinkUrlProcessor(servletContext);
String processUrl = urlProcessor.processUrl(request, "/messages/messages");