Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当使用AngularJS登录后授权标头不存在时,Spring Security不会引发错误_Javascript_Angularjs_Spring_Spring Mvc_Spring Security - Fatal编程技术网

Javascript 当使用AngularJS登录后授权标头不存在时,Spring Security不会引发错误

Javascript 当使用AngularJS登录后授权标头不存在时,Spring Security不会引发错误,javascript,angularjs,spring,spring-mvc,spring-security,Javascript,Angularjs,Spring,Spring Mvc,Spring Security,我遵循HTTP基本身份验证,向$HTTP头添加授权头: const headers = { authorization: "Basic " + btoa(this.login + ":" + this.password) }; this._$http.get("user", { headers: headers, }) .then( this.showUser.bind(this), this.showError.

我遵循HTTP基本身份验证,向$HTTP头添加授权头:

const headers = {
    authorization: "Basic " + btoa(this.login + ":" + this.password)
};

this._$http.get("user",
    {
        headers: headers,
    })
    .then(
        this.showUser.bind(this),
        this.showError.bind(this)
    );
showUser
内部,我重定向到带有
$location
作业
组件:

this._$location.path("jobs");
作业
组件中,我加载可用作业:

public $onInit() {
    this._$http.get("jobs").then(function(response) {
        this.jobs = response.data;
    }.bind(this));
    this.authenticated = this._loginService.isLogged();
}
故意未经授权的标题,以证明一切正常。我认为它应该被
Spring-Security
http401unauthorized
之类的东西抛弃,但是它在没有授权头的情况下工作。当我从另一个浏览器窗口注销并重新加载作业时,一切正常,作业不会加载。但我认为(我希望)授权数据(HTTP Basic)应该出现在每个请求中。这是我的安全配置:

protected void configure(HttpSecurity http) throws Exception {
    http
        .formLogin()
        .successHandler(
            new DifferentRoleBasedStartingURLsAuthenticationSuccessHandler()
        )
        .and()
        .logout()
        .logoutUrl("/logout")
        .and()
        .httpBasic()
        .and()
        .authorizeRequests()
        .antMatchers("/jobs/**").authenticated()
        .antMatchers("/interviews/**").authenticated()
        .anyRequest().permitAll()
        .and()
        .csrf()
        .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
    ;
也许我在这里犯了个错误。我认为规则
.antMatchers(“/jobs/**”).authenticated()
应该使
jobs/
也经过身份验证。你能帮助我吗?先谢谢你


更新2016-07-31
也许在Spring中的每个请求都不需要授权头?我的回复在这里:,密码是
test
,针对每个创建的用户。

这是由于您配置安全性的顺序,Spring安全性按照给定的顺序应用规则,这意味着您最初限制了模式,然后使用
.anyRequest().permitAll()
再次打开它。我已经在下面的代码中修复了顺序

http
    .formLogin()
    .successHandler(
        new DifferentRoleBasedStartingURLsAuthenticationSuccessHandler()
    )
    .and()
    .logout()
    .logoutUrl("/logout")
    .and()
    .httpBasic()
    .and()
    .authorizeRequests()
    .anyRequest().permitAll()
    .antMatchers("/jobs/**").authenticated()
    .antMatchers("/interviews/**").authenticated()
    .and()
    .csrf()
    .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
;

如果您使用的是基本身份验证,那么在spring中配置表单登录也没有多大意义。使用基本身份验证时,需要为服务器上需要身份验证的每个请求提供http授权标头。如果不存在受保护资源的授权标头,Spring将返回401响应

您的设置在我看来是正确的(是的,“/jobs/**”匹配“/jobs”),但我猜您验证安全约束是否有效的实验失败了,因为服务器还设置了一个可用于验证您身份的jsessionid cookie。因此,如果您在没有授权标头的情况下请求受保护的资源,但使用了有效的jsessionid cookie,则服务器仍可以对您进行身份验证

通过将会话创建策略设置为无状态,可以告诉spring security不要与http会话交互

请尝试以下安全配置:

protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic()
            .and()
            .authorizeRequests()
            .antMatchers("/jobs/**").authenticated()
            .antMatchers("/interviews/**").authenticated()
            .anyRequest().permitAll()
            .and()
            .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
            .and()
            .sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and();
}

我将订单更改为
anyRequest().permitAll().antMatchers(“/jobs/**”).authenticated().antMatchers(“/interfaces/**”).authenticated()
,我感到困惑-它仍然不需要HTTP Basic。总而言之——也许,有了Spring安全性和Angular,它不需要每个请求都有授权数据,例如只需要使用first,然后Spring就会记住它们?我肯定不是专家,但也许
Cookie:JSESSIONID…..
X-XSRF-TOKEN
就足够了?我真的对它的工作方式感到困惑。我的回购协议在这里:正确的分支:建立了多角度弹簧安全,它的工作非常完美。我会看一看你的代码,让你知道。这是错误的。请求匹配器确实按顺序考虑,但只使用第一个匹配请求机。因此,通过将anyRequest()指定为第一个请求匹配器,将不会使用其他匹配项。javadoc中明确提到了这一点:当您尝试使用http基本身份验证时,为什么会有表单登录?还要注意的是,spring中的表单登录是基于cookie的,所以这可能就是为什么您没有得到预期的结果。您的authorizeRequests()部分是正确的。您正在引用的教程也没有配置表单登录,所以我不确定它为什么会出现。@Pieter,如果我删除它,它会工作吗?很抱歉,我现在不能检查它。我颠倒了先前匹配程序的顺序(
ant….ant….anyRequest().permitAll()
),将
sessionCreationPolicy
设置为
sessionCreationPolicy。决不
(这就是你在代码中的意思吗?你写的
总是
),它成功了-非常感谢!事实上,示例代码中的SessionCreationPolicy.ALWAYS不是我想要编写的:)我已经更新了代码段。我已经更新了示例,使其使用SessionCreationPolicy.STATELESS而不是SessionCreationPolicy.NEVER。如果http会话存在,spring仍然使用从不策略与http会话交互。看见