Set custom response in Spring interceptor - spring

Here is my interceptor method where i want to set custom response to tell the UI what happened
#Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
HttpSession session = request.getSession(false);
if (session != null)
return true;
else{
response.sendError(HttpServletResponse.SC_REQUEST_TIMEOUT)
return false;
}
}
And in web.xml
<error-page>
<error-code>408</error-code>
<location>/error.html</location>
</error-page>
spring-servlet.xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/login" />
<bean class="com.example.CustomInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
When the session is timed out its not sending any response after return false.
Even the below is not working
response.sendRedirect("http://localhost:8080/home");

You can try very simple thing. Change your mvc:interceptros structure to
<mvc:interceptors>
<bean class="com.example.CustomInterceptor" />
</mvc:interceptors>
This essentially mean apply the interceptor to all applicable requests. I will come in a moment to why I say applicable in a moment. If above works then the issue is with your mapping.
Now as you know interceptors are configured at the level of HandlerMapping and it will be RequestMappingHandlerMapping (Spring 3.1+ with mvc:annotation-driven) or DefaultAnnotationHandlerMapping in your case.
Now as you have use <mvc:mapping path="/**" /> will map to all requests (including subpaths) as long as they are valid mappings. So lets say you have controller
#RequestMapping(value="/home", method = RequestMethod.GET)
public String welcome() {
return "welcome";
}
you cannot hit http://localhost:8080/yourProjectName/home/test and expect it to hit the interceptor. So you have to hit http://localhost:8080/yourProjectName/home as that is a valid HandlerMapping.
As to to response first debug if your interceptor is getting hit for any requests. If it does work then
response.sendError(HttpServletResponse.SC_REQUEST_TIMEOUT);
should redirect you to error.html as you have used
<error-page>
<error-code>408</error-code>
<location>/error.html</location>
</error-page>

Related

Spring MVC XML result chunked, missing Content-length header

I have a bunch of web services that return some content, sometimes > 100kb.
Unfortunately for the bigger results, I get the partial response with Transfer-encoding: Chunked.
Is there any way to disable chunking?
That's my method:
#RequestMapping(value = "/form/{repository}/{objectId}", method = RequestMethod.GET, headers="()")
#ResponseBody
public FormHelper getFormConfig(HttpServletRequest req, HttpServletResponse resp, #PathVariable String repository,
#PathVariable("objectId") String objectId) throws Exception
And that's the Spring XML config:
<import resource="classpath*:context-aaa.xml" />
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="useDefaultSuffixPattern" value="false" />
</bean>
I had the same issue with Jersey library so I rewrote the project into Spring MVC, but it's still there...
Thanks in advance for any help.
Mariusz
I was able to make that work by adding the filter below:
<filter>
<filter-name>bufferFilter</filter-name>
<filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>bufferFilter</filter-name>
<url-pattern>/services/*</url-pattern>
</filter-mapping>
https://jira.spring.io/browse/SPR-11948

Spring Security Error Pages using Forward

I want to display a custom error page on authentication denial (I am using a pre-authenticated secnario) and also access denial from Spring Security 3.0.X
Understand we can use the following to perform this:
<beans:bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<beans:property name="loginFormUrl" value="/error.jsp"/>
</beans:bean>
<beans:bean id="accessDeniedHandler" class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
<beans:property name="errorPage" value="/error.jsp"/>
</beans:bean>
but doing this results in a redirect and not a forward to the error page. Anyway to perform forward to an error page (so that we can set some attributes in the request)
Thanks
In my security-context.xml, for authentication failure I do like this (pay attention to the authentication-failure-url attribute):
<security:form-login login-page="/auth/login"
authentication-failure-url="/auth/login?error=true"
default-target-url="/mvc/home"
always-use-default-target="true" />
And for access denied I use this:
<security:access-denied-handler error-page="/auth/access-denied"/>
Both tags inside <security:http use-expressions="true">. For me works like a charm, I don't know why you are trying to configure it in the way you are doing when Spring provides such nice tags easy to use.
I don't know if it answers your question, I hope it helps.
EDIT:
Using the configuration provided above, means that you are using the default authentication failure handler (SimpleUrlAuthenticationFailureHandler) at the background. You can change the default behavior (which as default performs a redirect when a failed authentication is produced) by changing the attribute forwardToDestination value. This is what SimpleUrlAuthenticationFailureHandler does:
/**
* Performs the redirect or forward to the {#code defaultFailureUrl} if set, otherwise returns a 401 error code.
* <p>
* If redirecting or forwarding, {#code saveException} will be called to cache the exception for use in
* the target view.
*/
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
if (defaultFailureUrl == null) {
logger.debug("No failure URL set, sending 401 Unauthorized error");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed: " + exception.getMessage());
} else {
saveException(request, exception);
if (forwardToDestination) {
logger.debug("Forwarding to " + defaultFailureUrl);
request.getRequestDispatcher(defaultFailureUrl).forward(request, response);
} else {
logger.debug("Redirecting to " + defaultFailureUrl);
redirectStrategy.sendRedirect(request, response, defaultFailureUrl);
}
}
}
So I guess that I you declare your SimpleUrlAuthenticationFailureHandler in your security-context.xml and set the mentioned property value using the setUseForward(boolean forwardToDestination) method it should work. Could be something like:
<bean id="simpleUrlAuthenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
<property name="useForward" value="true">
</bean>
And then:
<security:form-login login-page="/auth/login"
authentication-failure-handler-ref="simpleUrlAuthenticationFailureHandler"
default-target-url="/mvc/home"
always-use-default-target="true" />
Good luck.

Spring 3.0 unable to forward request from HandlerInterceptorAdapter

I want to redirect to home page if session get invalid.
My spring-servlet.xml is
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.xxx.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Interceptor :
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if ((null == request.getSession(false))
|| (null == request.getSession(false).getAttribute(
"user"))) {
System.out.println("user logged out...");
RequestDispatcher rd = request.getRequestDispatcher("loginForm.htm");
rd.forward(request, response);
return false;
}
return super.preHandle(request, response, handler);
}
But its not working...
Whenever application get started, the message get printed multiple times and at the end it gives stack overflow..
Thank You.
It seems like the problem is in your mapping path. Since its mapped with /** your loginForm.htm is also getting intercepted. You have two solutions available to resolve this problem.
Either define <mvc:resources location="/resources/" mapping="/resources/**" /> so that the *.htm requests will not be intercepted. Replace the location and mapping values as per your path where the *.htm files are.
And another option is to change your mapping in intercepter with something like /*.do or something else.
Hope this helps you. Cheers.

Spring Web Flow and Url Rewrite Filter - cleaning URLs

I'm working on Spring MVC + Web Flow web application. I have mapped /home to MVC Controller and /test to Flow Controller. To remove /app from URLs i'm trying to use Url Rewrite Filter.
Mappings in MVC Controllers (#Controller) works good with that:
http://localhost:8080/spring/home -> render home view.
But when a request goes to WebFlow Controller something is wrong resulting in Error 404:
http://localhost:8080/spring/test -> redirect to http://localhost:8080/spring/app/test?execution=e1s1 -> page not found.
How to remove /app from URLs and got everything working ?
urlrewrite.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.0//EN" "urlrewrite3.0.dtd">
<urlrewrite default-match-type="wildcard">
<!-- to remove /app -->
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>
Dispatcher servlet mapping:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Simple controller:
#Controller
public class MainController {
#RequestMapping(value={"/home", "/"})
public String index(Model model) {
return "index";
}
}
Some logs:
DEBUG [FlowHandlerMapping:108] : Mapping request with URI '/spring/app/test' to flow with id 'test'
DEBUG [FlowExecutorImpl:135] : Launching new execution of flow 'test' with input null
DEBUG [FlowDefinitionRegistryImpl:59] : Getting FlowDefinition with id 'test'
DEBUG [FlowExecutionImplFactory:78] : Creating new execution of 'test'
...
DEBUG [FlowExecutionImpl:417] : Assigned key e2s1
DEBUG [FlowHandlerAdapter:367] : Sending flow execution redirect to '/spring/app/test?execution=e2s1'
DEBUG [DispatcherServlet:824] : Null ModelAndView returned to DispatcherServlet with name 'spring': assuming HandlerAdapter completed request handling
DEBUG [DispatcherServlet:674] : Successfully completed request
DEBUG [DispatcherServlet:693] : DispatcherServlet with name 'spring' processing GET request for [/spring/app/app/test]
DEBUG [FlowHandlerMapping:114] : No flow mapping found for request with URI '/spring/app/app/test'
WARN [PageNotFound:947] : No mapping found for HTTP request with URI [/spring/app/app/test] in DispatcherServlet with name 'spring'
Temporarily i did it as here using customized FlowHandler. It works, but i think it must be a simpler solution...
package utils;
public class PrettyFlowUrlHandler extends DefaultFlowUrlHandler {
#Override
public String createFlowDefinitionUrl(String flowId, AttributeMap input, HttpServletRequest request) {
return cleanUrl(super.createFlowDefinitionUrl(flowId, input, request), request);
}
#Override
public String createFlowExecutionUrl(String flowId, String flowExecutionKey, HttpServletRequest request) {
return cleanUrl(super.createFlowExecutionUrl(flowId, flowExecutionKey, request), request);
}
protected String cleanUrl(String url, HttpServletRequest request) {
String pattern = request.getServletPath().substring(1) + "/";
return url.replaceFirst(pattern, "");
}
}
config:
<bean id="flowMappings" class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="flowRegistry" ref="flowRegistry"/>
<property name="flowUrlHandler">
<bean class="utils.PrettyFlowUrlHandler"/>
</property>
<property name="order" value="0" />
</bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor"/>
<property name="flowUrlHandler">
<bean class="utils.PrettyFlowUrlHandler"/>
</property>
</bean>
Edit
Not working with custom flow handler like below:
#Component("test")
public class DataHandler extends AbstractFlowHandler {
#Override
public String handleExecutionOutcome(FlowExecutionOutcome outcome,
HttpServletRequest request, HttpServletResponse response) {
// ... checking outcome ...
return "/home"; // redirect to '404 page not found', because of rewrite to `/app/app/home`
}
}
Are you you trying to remove app from your url for eg
http://www.mydomain.com/app/home.html and change it to
http://www.mydomain.com/home.html
If yes then you should be configuring server.xml and your application should be deployed as ROOT instead of app in public_html/ or your tomcat directory.
This will work for you

Catch Spring MVC No Mapping Error

I configured below exception resolver in my web configuration file but I am not sure why it cannot handle
errors such as this 'No Matching error found for servlet request: path '/etc'
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">
exception
</prop>
</props>
</property>
</bean>
My app relies on Ajax and there are cases that I change the target url based on some user interactions.
My question is, is it possible for me to catch the error in my Spring MVC and forward it to my exception.jsp so that my user wont get
a nasty 404.
SimpleMappingExceptionResolver (and the HandlerExceptionResolver framework in general) will only be invoked to handle exceptions generated by the request handler (i.e. your controllers). If no handler is configured to handle the request, then it won't get that far, and your resolver won't be invoked.
The simplest thing for you to do is to configure a 404-handling page in your web.xml, e.g.
<error-page>
<error-code>404</error-code>
<location>/error.html</location>
</error-page>
You could set up a catch-all #RequestMapping and throw a custom exception if it is executed. Then you could handle that exception with the SimpleMappingExceptionResolver or an #ExceptionHandler method (maybe in a #ControllerAdvice class).
The catch-all controller:
#RequestMapping(value = "/**")
public ModelAndView noHandlerMappingFound() throws HandlerNotFoundException {
throw new HandlerNotFoundException("No handler mapping found.");
}
Here HandlerNotFoundException is your custom exception type.
The controller advice:
#ExceptionHandler(NoSuchEntityException.class)
#ResponseStatus(HttpStatus.NOT_FOUND)
public ModelAndView handleNoSuchEntityException(final HttpServletRequest req, final Exception ex) {
return new ModelAndView("error-page");
}

Resources