Suppose you want to create a screen that gets a certain condition from the DB and redirects to the specified screen if the condition is met.
In such a case, if you are using Spring-security, you can use a custom filter. You can define a filter and have the redirect performed within that class.
The timing for inserting a filter with Spring-security is listed below.
Custom-created filters can be defined before or after the above, or as an alternative to the filter itself.
spring-security.xml
<http>
<!--Before-->
<custom-filter before="FORM_LOGIN_FILTER" ref="myFilter" />
<!--As an alternative to itself-->
<custom-filter position="FORM_LOGIN_FILTER" ref="myFilter" />
<!--rear-->
<custom-filter after="FORM_LOGIN_FILTER" ref="myFilter" />
</http>
It seems better to use GenericFilterBean
for Spring filters. Define a class that inherits this class at an appropriate place in the project.
Then, set the namespace of the class and id on Spring in XML. By doing so, it is for use in spring-security.
spring-context.xml
<beans:bean id="myFilter" class="org.baeldung.security.filter.CustomFilter"/>
Writing a filter for the whole project like this gives me the double Dispatch error I used to see in Struts. This:
java.lang.IllegalStateException: Cannot forward after response has been committed
The bottom line is that this happens when you try to redirect a fixed-destination request again with a filter.
As a workaround, see Table 6.1. Standard Filter Aliases and Ordering above. Perform the desired filtering before the destination of the request is decided.
Recommended Posts