Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:18:40+00:00 2026-05-29T05:18:40+00:00

Is there a way in spring security 3 to redirect the user to a

  • 0

Is there a way in spring security 3 to redirect the user to a different page rather than the user account page if cookies are disabled on the browser?

Just like gmail does. As it redirects the user to a different page than the user account page and force user to enable cookies.

What I want is to force the user to enable cookies before he lands on his account page.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:18:41+00:00Added an answer on May 29, 2026 at 5:18 am

    To handle cookies you can use a handler servlet for viewing cookie details. To do this you need to prepare your login form manually and if condition matches with your requirements than forward to /j_spring_security_check. I have just used simple validation on embedded cookie in the request. In the below example servlet, I have checked if request contains any cookie if not I have forwarded page to cookieDisabled.jsp

    package com.udb.servlets;
    
    import java.io.IOException;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.logging.Log;
    
    /**
     * Servlet implementation class cookieHandler
     */
    public class cookieHandler extends HttpServlet
    {
        private static final long serialVersionUID = 1L;
        private static final String cookieDisabled = "/cookieDisabled.jsp";
        private static final String cookieEnabled = "/j_spring_security_check";
        RequestDispatcher dispatcher = null;
    
        /**
         * @see HttpServlet#HttpServlet()
         */
        public cookieHandler() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
        public void doPost(HttpServletRequest request, HttpServletResponse response)
        {
    
            if (request.getCookies() == null) {
                System.out.println("cookie disabled!");
                dispatcher = getServletContext().getRequestDispatcher(
                        cookieDisabled);
                try {
                    dispatcher.forward(request, response);
                } catch (ServletException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
    
                    dispatcher = getServletContext().getRequestDispatcher(
                            cookieEnabled);
                    System.out.println("Cookies active!");
                    try {
                        dispatcher.forward(request, response);
                    } catch (ServletException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
    
            }
        }
    
        public void doGet(HttpServletRequest req, HttpServletResponse res)
        {
            doPost(req, res);
        }
    
    }
    

    web-xml for handler:

    <servlet>
            <description>   </description>
            <display-name>cookieHandler</display-name>
            <servlet-name>cookieHandler</servlet-name>
            <servlet-class>com.udb.servlets</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>cookieHandler</servlet-name>
            <url-pattern>/cookieHandler</url-pattern>
        </servlet-mapping>
    

    if you have secured all urls then you need to add below tag into security.xml as below:

    <security:intercept-url pattern="/cookieDisabled*"
                filters="none" />
    

    And in your login form you need to post request to cookieHandler instead of j_spring_security_check:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <head>
    <title>Login Page</title>
    <style>
    .errorblock {
        color: #ff0000;
        background-color: #ffEEEE;
        border: 3px solid #ff0000;
        padding: 8px;
        margin: 16px;
    }
    </style>
    </head>
    <body onload='document.f.j_username.focus();'>
        <h3>Login with Username and Password (Custom Page)</h3>
    
        <c:if test="${not empty error}">
            <div class="errorblock">
                Your login attempt was not successful, try again.<br /> Caused :
                ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
            </div>
        </c:if>
    
        <form name='f' action="<c:url value='cookieHandler' />"
            method='POST'>
    
            <table>
                <tr>
                    <td>User:</td>
                    <td><input type='text' name='j_username' value=''>
                    </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type='password' name='j_password' />
                    </td>
                </tr>
                <tr>
                    <td colspan='2'><input name="submit" type="submit"
                        value="submit" />
                    </td>
                </tr>
                <tr>
                    <td colspan='2'><input name="reset" type="reset" />
                    </td>
                </tr>
            </table>
    
        </form>
    </body>
    </html>
    

    Process quite similar with JSF (if you are using) as you handle login via servlet dispatcher.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a way to configure spring security to have a default user (with
Just wondering if there is a way in Spring to have a parent controller:
Is there any way to format a string by name rather than position in
Is there any way to secure Web Methods in c# like Spring Security Annotations?
Is there a way to tell Spring to find the user's role in a
I just got done walking through the install instructions on this page: http://grails.org/plugin/spring-security-facebook I
Is there any way to validate spring context xml files in eclipse? Features like:
Is there a way to obtain the post data itself? I know spring handles
In Spring 2.0, is there a quick way to have a list of the
I have implemented spring security's remember me feature in our app, the way i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.