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 7566119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:19:51+00:00 2026-05-30T14:19:51+00:00

I have some kind of controller: @Controller public class DefaultController { @RequestMapping(value=/index.html, method=RequestMethod.GET) public

  • 0

I have some kind of controller:

    @Controller
    public class DefaultController {

    @RequestMapping(value="/index.html", method=RequestMethod.GET)
        public String indexView(){
            return "index";
        }

    @RequestMapping(value="/some.action", method=RequestMethod.POST)
    @ResponseBody
        public MyObject indexView(some parametrs.... ){
            MyObject o= daoService.getO(id);
                    return o; 
        }
}

I’m using Spring Security:

<security:global-method-security secured-annotations="enabled" />
<security:http  auto-config="true" access-denied-page="/accessDenied.jsp">
  <security:form-login login-page="/login.html" login-processing-url="/login" authentication-failure-url="/login.html?login_error=1" default-target-url="/"/> 
  <security:http-basic/>
    <security:intercept-url pattern='/**' access='ROLE_USER' />
  <security:logout logout-url="/logout" logout-success-url="/"/>
  <security:remember-me services-ref="rememberMeServices"/>
    </security:http>

Now, my problem is th following:

when accessing /some.action using AJAX with none authenticated user Spring Security returns 301 command (Redirect to access denied page).

What I need is even if user is not authenticated to return 200 OK and send authentication error message to the client or event or in worst case to return 400 something error.

I understand that I need to create custom authentication success handler, but can I do it and how can I apply this handler on *.action URIs?

  • 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-30T14:19:52+00:00Added an answer on May 30, 2026 at 2:19 pm

    For AJAX authentication, I added a custom security entry point ref to check to see if the user is authenticated. If they are not, I send them a 4xx error code. Then on my Ajax call, I check to see if an error is returned and if so I redirect them to my login page.

    Here’s a snippet of my security-config.

    <security:http entry-point-ref="myAuthenticationEntryPoint" auto-config="true" use-expressions="true">
    ...
    ...
    </security:http>
    <bean id="myAuthenticationEntryPoint" class="com.security.AjaxAuthenticationEntryPoint" >
            <property name="loginFormUrl" value="/login"/>
    </bean>
    

    Here’s my custom entry point:

    public class AjaxAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint
    {   
        @Override
        /**
         * This method only gets call when the user logs out or when the session is invalid
         * 
         * It checks to see if the request is an ajax request
         * if so then return an error
         * else then do the natural check 
         */
        public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
                throws IOException, ServletException 
        {                       
            if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) 
            {
                if (request.getSession() != null)
                {
                    Object targetUrl = request.getSession().getAttribute(WebAttributes.SAVED_REQUEST);
                    if (targetUrl != null)
                    {                   
                        response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED);                                      
                    }
                }   
            }
            else
            {
                super.commence(request, response, authException);
            }
    
        }
    }
    

    Here’s a snippet of my JQuery call, reload causes the login page to appear.

    error: function (xhr, textStatus, errorThrown) 
                   {    
                        // 417 is sent from the server to indicate that
                        // page needs to be reloaded
                        //
                        if (xhr.status == 417)
                        {
                            xhr = null;
                            window.location.reload();                       
                        }
                   }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some actions: public partial class MyController : Controller { public ActionResult Action1()
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I have some kind of test data and want to create a unit test
I have some kind of problem with jQuery selectors. Let's say i want to
Most program languages have some kind of exception handling; some languages have return codes,
I want to have some kind of bounce effect in my animation plugin but
I want to have some kind of single list that is initialized in a
Is it possible in Eclipse to have some kind of packages (sets of projects)
Is there a way to have some kind of default constructor (like a C++
I have a question about reflection I am trying to have some kind of

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.