[JAVA] How to redirect after user login with Spring-security

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.

Filter types defined in Spring-security

The timing for inserting a filter with Spring-security is listed below.

Things that are likely to be used

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>

How to define the filter itself

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"/>

To prevent double dispatch

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

How to redirect after user login with Spring-security
[Rails] How to deal with URL changes after render
How to number (number) with html.erb
How to update with activerecord-import
How to scroll horizontally with ScrollView
How to get started with slim
uitabbarcontroller reload after login with FirebaseAuth
How to enclose any character with "~"
How to use mssql-tools with alpine
How to get along with Rails
How to run a job with docker login in AWS batch
How to crop an image with libGDX
How to adjustTextPosition with iOS Keyboard Extension
How to share files with Docker Toolbox
How to compile Java with VsCode & Ant
[Java] How to compare with equals method
[Android] How to deal with dark themes
How to use BootStrap with Play Framework
[Rails] How to use rails console with docker
How to switch thumbnail images with JavaScript
How to do API-based control with cancancan
How to achieve file download with Feign
How to update related models with accepts_nested_attributes_for
[Rails] How to get the user information currently logged in with devise
How to set JAVA_HOME with Maven appassembler-maven-plugin
How to implement TextInputLayout with validation function
How to handle sign-in errors with devise
How to delete data with foreign key
How to test private scope with JUnit
How to monitor nginx with docker-compose with datadog
How to deal with Precompiling assets failed.
How to achieve file upload with Feign
How to build Rails 6 environment with Docker
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
How to get the ID of a user authenticated with Firebase in Swift
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
How to download Oracle JDK 8 rpm with curl
[Java] How to test for null with JUnit
How to mock each case with Mockito 1x
How to disable user operations during asynchronous processing
How to mock each case with PowerMock + Mockito1x
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to save to multiple tables with one input
I tried to manage login information with JMX
How to test interrupts during Thread.sleep with JUnit
How to use built-in h2db with spring boot
How to search multiple columns with gem ransack
[Swift] How to implement the LINE login function
How to use Java framework with AWS Lambda! ??
Error with bundle install after upgrading to Catalina
[Swift] How to link the app with Firebase
Try to implement login function with Spring Boot
How to create multiple pull-down menus with ActiveHash
How to use Java API with lambda expression
[Validation] rails How to specify after today's date
How to get started with Eclipse Micro Profile
How to give your image to someone with docker
How to insert all at once with MyBatis
How to monitor SPA site transitions with WKWebView
[Rails] How to easily implement numbers with pull-down
How to build API with GraphQL and Rails