|
|
|
@ -32,13 +32,15 @@ import org.springframework.util.ReflectionUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* A <code>Filter</code> which populates the <code>ServletRequest</code> with a new request wrapper.<p>Several |
|
|
|
* A <code>Filter</code> which populates the <code>ServletRequest</code> with a new request wrapper. |
|
|
|
* request wrappers are included with the framework. The simplest version is {@link |
|
|
|
* Several request wrappers are included with the framework. The simplest version is {@link |
|
|
|
* SecurityContextHolderAwareRequestWrapper}. A more complex and powerful request wrapper is {@link |
|
|
|
* SecurityContextHolderAwareRequestWrapper}. A more complex and powerful request wrapper is |
|
|
|
* org.springframework.security.wrapper.SavedRequestAwareWrapper}. The latter is also the default.</p> |
|
|
|
* {@link SavedRequestAwareWrapper}. The latter is also the default. |
|
|
|
* <p>To modify the wrapper used, call {@link #setWrapperClass(Class)}.</p> |
|
|
|
* <p> |
|
|
|
* <p>Any request wrapper configured for instantiation by this class must provide a public constructor that |
|
|
|
* To modify the wrapper used, call {@link #setWrapperClass(Class)}. |
|
|
|
* accepts two arguments, being a <code>HttpServletRequest</code> and a <code>PortResolver</code>.</p> |
|
|
|
* <p> |
|
|
|
|
|
|
|
* Any request wrapper configured for instantiation by this class must provide a public constructor that |
|
|
|
|
|
|
|
* accepts two arguments, being a <code>HttpServletRequest</code> and a <code>PortResolver</code>. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Orlando Garcia Carmona |
|
|
|
* @author Orlando Garcia Carmona |
|
|
|
* @author Ben Alex |
|
|
|
* @author Ben Alex |
|
|
|
@ -47,8 +49,8 @@ import org.springframework.util.ReflectionUtils; |
|
|
|
public class SecurityContextHolderAwareRequestFilter extends SpringSecurityFilter { |
|
|
|
public class SecurityContextHolderAwareRequestFilter extends SpringSecurityFilter { |
|
|
|
//~ Instance fields ================================================================================================
|
|
|
|
//~ Instance fields ================================================================================================
|
|
|
|
|
|
|
|
|
|
|
|
private Class wrapperClass = SavedRequestAwareWrapper.class; |
|
|
|
private Class<? extends HttpServletRequest> wrapperClass = SavedRequestAwareWrapper.class; |
|
|
|
private Constructor constructor; |
|
|
|
private Constructor<? extends HttpServletRequest> constructor; |
|
|
|
private PortResolver portResolver = new PortResolverImpl(); |
|
|
|
private PortResolver portResolver = new PortResolverImpl(); |
|
|
|
private String rolePrefix; |
|
|
|
private String rolePrefix; |
|
|
|
|
|
|
|
|
|
|
|
@ -59,6 +61,7 @@ public class SecurityContextHolderAwareRequestFilter extends SpringSecurityFilte |
|
|
|
this.portResolver = portResolver; |
|
|
|
this.portResolver = portResolver; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
public void setWrapperClass(Class wrapperClass) { |
|
|
|
public void setWrapperClass(Class wrapperClass) { |
|
|
|
Assert.notNull(wrapperClass, "WrapperClass required"); |
|
|
|
Assert.notNull(wrapperClass, "WrapperClass required"); |
|
|
|
Assert.isTrue(HttpServletRequest.class.isAssignableFrom(wrapperClass), "Wrapper must be a HttpServletRequest"); |
|
|
|
Assert.isTrue(HttpServletRequest.class.isAssignableFrom(wrapperClass), "Wrapper must be a HttpServletRequest"); |
|
|
|
@ -70,28 +73,23 @@ public class SecurityContextHolderAwareRequestFilter extends SpringSecurityFilte |
|
|
|
this.rolePrefix = rolePrefix.trim(); |
|
|
|
this.rolePrefix = rolePrefix.trim(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
|
|
|
protected void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException { |
|
|
|
if (!wrapperClass.isAssignableFrom(request.getClass())) { |
|
|
|
if (!wrapperClass.isAssignableFrom(request.getClass())) { |
|
|
|
if (constructor == null) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
if (constructor == null) { |
|
|
|
constructor = wrapperClass.getConstructor( |
|
|
|
constructor = wrapperClass.getConstructor(HttpServletRequest.class, PortResolver.class, String.class); |
|
|
|
new Class[] {HttpServletRequest.class, PortResolver.class, String.class}); |
|
|
|
|
|
|
|
} catch (Exception ex) { |
|
|
|
|
|
|
|
ReflectionUtils.handleReflectionException(ex); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
request = constructor.newInstance(request, portResolver, rolePrefix); |
|
|
|
request = (HttpServletRequest) constructor.newInstance(new Object[] {request, portResolver, rolePrefix}); |
|
|
|
|
|
|
|
} catch (Exception ex) { |
|
|
|
} catch (Exception ex) { |
|
|
|
ReflectionUtils.handleReflectionException(ex); |
|
|
|
ReflectionUtils.handleReflectionException(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
chain.doFilter(request, response); |
|
|
|
chain.doFilter(request, response); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int getOrder() { |
|
|
|
public int getOrder() { |
|
|
|
return FilterChainOrder.SERVLET_API_SUPPORT_FILTER; |
|
|
|
return FilterChainOrder.SERVLET_API_SUPPORT_FILTER; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|