Spring WebFlow + Spring Security + File分段上传

时间:2015-06-28 16:24:45

标签: spring security file-upload spring-webflow

请帮助我,我可以解决问题2天:

这是一个流程 "创建-magazine.xml"

<view-state id="createMagazineForm" view="createmagazine" model="magazine">
    <transition on="submit" to="createMagazineAction" />
</view-state>

<action-state id="createMagazineAction">
    <evaluate expression="createMagazineService.justTest(magazine,flowRequestContext)" />   
    <transition on="success" to="createMagazineSuccess"/>
</action-state>

<view-state id="createMagazineSuccess" view="createsuccess" >

</view-state>

这是createmagazine.jsp:

<form:form method="POST" modelAttribute="magazine" enctype="multipart/form-data">
   <div class="form-group">
      <fieldset>
        <p>Your title</p>
        <form:input placeholder="Title here" cssClass="form-control" path="vtitle"  />
        <p>Magazine image</p>
        <input type="file" class="form-control" name="vimage"  />
                </fieldset>                  
        <input type="hidden" name="${_csrf.parameterName}"
            value="${_csrf.token}" />         
       <input type="submit" class="btn btn-success" value="Create" name="_eventId_submit"  /> 
   </div>
 </form:form>

并且createsuccess.jsp:

        <h1 class="jumbotron">Magazine created</h1>
    <p>${magazine.vtitle}</p>
    <p>${magazine.vimage.getName()}</p>

这是我的模型对象:

@XmlRootElement
public class Magazine implements Serializable{
  private Integer id;
  private String vtitle;
  private MultipartFile vimage;

  public Integer getId() {
    return id;
  }
  public MultipartFile getVimage() {
    return vimage;
  }
  public void setVimage(MultipartFile vimage) {
    this.vimage = vimage;
  }

  public void setId(Integer id) {
    this.id = id;
  }
  public String getVtitle() {
    return vtitle;
  }
  public void setVtitle(String vtitle) {
    this.vtitle = vtitle;
  }

}

点击提交按钮后,我得到 405请求方法&#39; POST&#39;不支持

我认为这是因为 Spring Security

更新:这是我的security-config

    <!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
    <intercept-url pattern="/admin**" access="hasRole('Admin')" />
            <intercept-url pattern="/secured**" access="hasRole('User')" />


    <!-- access denied page -->
    <access-denied-handler error-page="/403" />
    <form-login 
        login-page="/login" 
        default-target-url="/secured"
        authentication-failure-url="/login?error" 
        username-parameter="username"
        password-parameter="password" />
    <logout logout-success-url="/login?logout" />
    <!-- enable csrf protection -->
    <csrf />
</http>

<authentication-manager>
    <authentication-provider user-service-ref="myUserDetailsService" >
        <password-encoder hash="bcrypt" />    
    </authentication-provider>
</authentication-manager>

1 个答案:

答案 0 :(得分:0)

谢谢@M。 Deinum

我解决了这个问题。首先我修复了web.xml:

<!-- Spring MVC -->
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/dispatcher-servlet.xml
            /WEB-INF/spring/webflow-config.xml      
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>

    <multipart-config>
        <location>/tmp</location>
        <max-file-size>20848820000</max-file-size>
        <max-request-size>41801884100</max-request-size>
        <file-size-threshold>104857600</file-size-threshold>
    </multipart-config>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


<!-- Spring Security -->
 <filter>
    <filter-name>csrfFilter</filter-name>
    <filter-class>
        org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
    <async-supported>true</async-supported>
</filter>
<filter-mapping>
    <filter-name>csrfFilter</filter-name>
    <url-pattern>/*</url-pattern>
 </filter-mapping>

<filter>
    <description>
        Allows the application to accept multipart file data.
    </description>
    <display-name>springMultipartFilter</display-name>
    <filter-name>springMultipartFilter</filter-name>
    <filter-class>
   org.springframework.web.multipart.support.MultipartFilter</filter-class>
    <!--init-param>
        <param-name>multipartResolverBeanName</param-name>
        <param-value>multipartResolver</param-value>
    </init-param-->
</filter>
<filter>
    <description>
        Secures access to web resources using the Spring Security framework.
    </description>
    <display-name>springSecurityFilterChain</display-name>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
    </filter>


<filter-mapping>
    <filter-name>springMultipartFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>ERROR</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

然后我将方法改为:

    public String justTest(Magazine magazine,RequestContext requestContext){
    ServletExternalContext context = (ServletExternalContext) requestContext.getExternalContext();
    MultipartHttpServletRequest multipartRequest = new StandardMultipartHttpServletRequest((HttpServletRequest)context.getNativeRequest());
    magazine.setFile(multipartRequest.getFile("file"));
    requestContext.getFlowScope().put("magazine", magazine);
    return "success";
}