我将HDIV与基于Spring Java Configuration的Spring MVC结合使用,这是我从其展示实现中得出的。配置已加载,但是当我尝试访问受保护的URL(即“ / messages / message”)时,总是出现未经授权的访问错误HDIV_PARAMETER_DOES_NOT_EXIST。当我更改配置以从HDIV处理中排除URL时,它可以工作,但这当然不是一个选择

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


hdiv reference documentation
hdiv example showcase configuration code


日志文件错误声明

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());
  }
}

最佳答案

我找到了解决方案。 HDIV要求所有链接在其URL中都添加“ _HDIV_STATE_”参数。为了实现这一点,您必须使用HDIV的taglib,而不是原始的JSTL taglib。

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

例如在您的POM中

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


例如在您的JSP中(请注意taglib语句中的“ www.hdiv.org”)

<%@ 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>实用工具标记呈现URL。这将呈现具有所需HDIV参数的网址,即

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");

10-06 13:54