有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Spring安全web应用程序中的javaajax请求

我正在开发一个应用程序,在其中我成功地集成了Spring安全性,并且我能够根据角色登录/注销和授权用户

现在,在同一个应用程序中,当我触发一些AJAXGET请求时;它将请求重定向到登录页面,并返回我的登录页面HTML页面作为响应。我知道我的AJAX请求没有经过Spring Security的身份验证

现在我的问题是,如何触发经过身份验证的AJAX请求,以便获得正确的响应,而不是登录页面作为响应

我已将Spring Security配置如下:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/register").permitAll()
            .antMatchers("/forgetPassword").permitAll()
            .antMatchers("/resetPassword").permitAll()
            .antMatchers("/changePassword").permitAll()
            .antMatchers("/error/*").permitAll()
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .defaultSuccessUrl("/index")
            .failureUrl("/login?error=true")
            .and()
        .exceptionHandling()
            .accessDeniedPage("/error/403")
            .and()
        .logout()
            .invalidateHttpSession(true)
            .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .logoutSuccessUrl("/login");
}

更新:以下是我的请求标题

Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:optimizelyEndUserId=oeu1496834101340r0.5246134202334098; Idea-6adc618e=45c8074c-a056-43bd-a6bd-2b8e7545231f; optimizelySegments=%7B%223910210135%22%3A%22gc%22%2C%223921780062%22%3A%22direct%22%2C%223922680052%22%3A%22false%22%7D; optimizelyBuckets=%7B%7D; _ga=GA1.1.304304118.1496834102
Host:localhost:8080
Referer:http://localhost:8080/QuickBill/invoice/new
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
X-Requested-With:XMLHttpRequest

成功登录后任何请求的响应标头

Cache-Control:no-cache, no-store, must-revalidate
Content-Language:en-US
Content-Type:text/html;charset=ISO-8859-1
Date:Sat, 29 Jul 2017 06:56:07 GMT
Expires:Thu, 01 Jan 1970 00:00:00 GMT
Pragma:no-cache
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

以下是我的ajax请求代码片段:

$.ajax({
    url: 'url?clientId='+clientId, 
    xhrFields: {
      withCredentials: true
    }
}).done(function(err, data){
    console.log(data);
    console.log(err);
})
}

共 (0) 个答案